Using Python NumPy Library, Numerical Matrices and AI Architecture
With the NumPy library, data science, mathematical equations and functions can be used very quickly in Python. We can say that even the road to artificial intelligence architecture starts with the NumPy library.

I think the title “Artificial Intelligence Design with Python NumPy Library” is too ambitious. Actually, “Introduction to Artificial Intelligence Design with NumPy” would be a more meaningful title. One way or another, AI design has to start somewhere.
When working with Python, we see mathematical matrices being used in many areas, from bigdata parsing, image processing and even artificial intelligence architecture. Python has many libraries and classes that handle number matrices. The most widely used of these is undoubtedly the NumPy library. Let’s take a look at what we can do with the Python NumPy library.
GitHub Link to the NumPy Library BootCamp Project:
https://github.com/omersahintr/BootCampEdu/tree/main/NumPy
Index
NumPy Library Setup
In order to work with Python NumPy, we first need to install it. After the installation process, we can take advantage of the NumPy library in the code line by importing it.
Let’s go to the terminal screen and load the NumPy module by typing the following command line:
- pip install numpy (For the Python core platform)
- conda install numpy (For the Anaconda platform)

NumPy Documentation: https://numpy.org/doc/stable/
You will use the NumPy library a lot in other libraries as well. Especially if you have matrices and determinants, NumPy will come to your aid.
Python List Variable to NumPy Array Variable
In the following line of code, a list variable is converted to a numpy array variable and printed to the screen. The screen output is appended with the symbol“# : ” as a comment line.
import numpy as np
my_list = [10,20,30,40,50] #python list variable
my_array = np.array(my_list) #list variable convert to numpy array type
print(type(my_list)) # : <class 'list'>
print(type(my_array)) # : <class 'numpy.ndarray'>
print(my_array) # : [10 20 30 40 50]
print(my_array[0]) # : 10
print(my_array[-2]) # : 40
my_array[1] = 200
print(my_array) # : [ 10 200 30 40 50]
print(my_array.max()) # : 200 maximum value
print(my_array.min()) # : 10 minimum value
print(my_array.mean()) # : 66.0 average value
list_python = [[1,0,0],[0,1,0],[0,0,1],[0,0,0]] # python list variable
print(list_python[1][1]) # : 1
## Np Matrix
np_matrix = np.array(list_python) # NumPy matrix variable
print(np_matrix) # :
# [1 0 0]
# [0 1 0]
# [0 0 1]
# [0 0 0]
print(np_matrix[0]) # : [1,0,0]
print(np_matrix[0][0]) # : 1
print(np_matrix.shape) # : (4,3) array size 4-rows and 3-columns
## Arrange ##
new_matrix = np.arange(0,10) # : [0 1 2 3 4 5 6 7 8 9]
new_matrix = np.arange(0,100,10) # : [ 0 10 20 30 40 50 60 70 80 90]
print(new_matrix)
print(np.zeros((3,3)))
# : [0. 0. 0.]
# [0. 0. 0.]
# [0. 0. 0.]
print(np.ones((4,3)))
#: [1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]
## linspace:
print(np.linspace(0,10,10))
# [ 0. 1.11111111 2.22222222 3.33333333 4.44444444 5.55555556
# 6.66666667 7.77777778 8.88888889 10. ]
## random:
print(np.random.randint(1,100,5)) # : [75 82 83 26 56] 1 to 100 random 5 numbers
Python- np.array(list): converts array variable to numpy matrix variable type.
- np_matrix[i][j]: i-row index and j-column index
- np_matrix.shape: gives the size of the matrix (rows, cols)
Matrix Construction with Np.zeros(i,j) and Np.ones(i,j)
np.zeros(3,3) | [0. 0. 0.] [0. 0. 0.] [0. 0. 0.] | Create a 0-zero matrix of size 3×3. |
np.ones(4,3) | [1. 1. 1.] [1. 1. 1.] [1. 1. 1.] [1. 1. 1.] | Create a 1-to-1 matrix of size 4×3. |
Np.linespace(start,stop,num) and Np.random.randint(low,high,size) with Matrix Creation
np.linespace(0,10,10) | [ 0. 1.11111111 2.22222222 3.33333333 4.44444444 5.55555556 # 6.66666667 7.77777778 8.88888889 10. ] | Generate linear numbers with specific intervals from 0 to 10 |
np.random.randint(1,100,5) | [87 70 31 45 73] | Generate 5 random integers from 1 to 100. |
Slicing Operations with NumPy
import numpy as np
np_array = np.arange(0,10) # : [0 1 2 3 4 5 6 7 8 9]
print(np_array)
## Slicing
print(np_array[2:6]) # : [2 3 4 5 ]
print(np_array[0:10:2]) # : [0 2 4 6 8]
np_array[2:5] = -1 # : [ 0 1 -1 -1 -1 5 6 7 8 9]
print(np_array)
Python- To get an array defined as np_array, we can write np_array[start_index, end_index, step].
- Any value can be assigned with np_array[i:j:x] = -1. We could not do this assignment in lists.
Python NumPy Create Affected Arrays with Slicing in Arrays
- np.arange(0,10) Let’s create an np array with
- The np_array variable will contain [0 1 2 3 4 5 6 7 8 9].
- With mid_array = np_array[3:7], let’s take a piece from this array and assign it to a new variable.
- Let’s make all elements of this new array 10 with mid_array[:] = 10.
- print[mid_array] –> [10 10 10 10 10].
- However, the interesting thing is
print(np_array) –> [ 0 1 -1 10 10 10 10 10 10 7 8 9] even though we did not assign a value to the first defined array, the numbers between indexes 3 and 7 were changed to 10.
This slicing method only works this way in NumPy arrays. In Python’s standard list structure, slicing cannot be used for bulk data assignment. It can be modified individually, but the elements in the master list that are sliced are not changed.
To get rid of this slicing method, we need to slice the array differently.
- If you create an independent copy array with np_copy_array = np_array.copy() and then slice a chunk of it and assign it a value, none of the data in np_array will change.
import numpy as np
# Slicing Copy Arrays Method
a_array = np.arange(0,10) # : [0 1 2 3 4 5 6 7 8 9] --> [0 1 2 3 4 5 6 7 8 9]
b_array = a_array.copy() # : [0 1 2 3 4 5 6 7 8 9] --> [0 1 2 3 99 99 99 99 99 9]
c_array = b_array[4:9] # : [4 5 6 7 8]
c_array[:] = 99 # : [99 99 99 99 99]
print(f"a_array={a_array}\nb_array={b_array}\nc_array={c_array}")
Python
NumPy Array Operations
import numpy as np
x_array = np.random.randint(1,100,20)
final_array = x_array > 50 # : [ True True True True False False True False True True False False
# False False False True False False False True]
print((x_array[final_array])) # : >50 listing --> [58 66 56 93 62 67 98 56 77 93 52 61]
PythonThis is how you can use the greater than (>), less than (<), equal to (=) or unequal to (!=) operators in numpy arrays.
We can even shorten it by 1 line without creating the array named final_array.
import numpy as np
x_array = np.random.randint(1,100,20)
print(x_array[x_array > 50]) # : >50 listing --> [58 66 56 93 62 67 98 56 77 93 52 61]
PythonNumPy Mathematical Operations on Arrays
Addition, Subtraction, Division and Multiplication (Four Operations)
You can add, subtract, multiply and divide NumPy arrays, which are quite advanced in structure from standard python arrays. NumPy performs these operations based on index.
import numpy as np
end_array = np.arange(1,10)
print(end_array) # : [1 2 3 4 5 6 7 8 9]
print(end_array + end_array) # : [ 2 4 6 8 10 12 14 16 18]
print(end_array - end_array) # : [0 0 0 0 0 0 0 0 0]
print(end_array * end_array) # : [ 1 4 9 16 25 36 49 64 81]
print(end_array / end_array) # : [1. 1. 1. 1. 1. 1. 1. 1. 1.]
PythonWhen doing mathematical 4 operations with Python NumPy arrays, the size (number of elements) of the two arrays must be the same.
For example;
import numpy as np
end_array = np.arange(1,10)
and_array = np.arange(2,8)
print(end_array + end_array)
PythonIf you try to sum two different sized numpy arrays, you will get ValueError: operands could not be broadcast together with shapes (9,) (6,). In short, it says that the first array has 9 elements and the second array has 6 elements and I can’t add them.
Max(), Min(), Mean(), Sqrt() Methods
Methods to get the maximum, minimum and average of elements in a numpy array.
You can do this in two different ways;
- this_array.max() or np.max(this_array) both return the same result. However, the common usage is np.max(this_array).
import numpy as np
this_array = np.arange(1,10)
print(this_array.max()) # : 9
print(np.max(this_array)) # : 9
print(this_array.min()) # : 1
print(this_array.mean()) # : 5.0
print(np.sqrt(this_array[2])) # : 1.7320508075688772 karekök
Python