This blog is about learning Numpy with python.
You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
Welcome to see my homepage and contact me: NicholasYe’s Homepage.
Recommendation:
ArrayAn array is a central data structure of the NumPy library:
dtype.# We can initialize an array with python list:
a = np.array([1, 2, 3, 4, 5, 6])
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# If we want to get the first element of the array:
print(a[0]) # Output: 1
print(type(a[0])) # Output:
print(b[0]) # Output: [1 2 3 4]
print(type(b[0])) # Output:
# Please notice: b[0] is not the same as python list, it is an array.
ArrayCreate array with all elements are 0.
# We can create zero array with np.zeros
a = np.zeros(2)
# Here is how arrays look like:
print(a)
# Output:
# [0. 0.]
Create array with all elements are 1.
np.ones() function
np.zeros() function, I will escape illustration.Create array with recursive numbers
np.arange() function, in the following example:
0 means the start of the array10 means the end of the array2 means the step of the arraya = np.arange(0, 10, 2)
print(a) # Output: [0 2 4 6 8]
Create array with random float number vary from 0 to 1
np.random.random() function, in the following example:
(2,3) means the structure of array:
a = np.random.random(3)
b = np.random.random((2, 3))
print(a)
# Output: [0.55022573 0.91890014 0.27154755]
print(b)
# Output:
# [[0.19379429 0.14326045 0.08705205]
# [0.04354501 0.82007126 0.90535807]]
np.random.random((2, 3, 4)) result here for you to compare:c = np.random.random((2, 3, 4))
print(c)
# Output:
# [[[0.34256251 0.10954162 0.59984443 0.57395688]
# [0.8414659 0.81810578 0.83390859 0.53966319]
# [0.56616342 0.17938677 0.41628244 0.28977822]]
# [[0.99204515 0.38727915 0.42309386 0.95354766]
# [0.19958058 0.0175912 0.91000997 0.54684459]
# [0.14314477 0.60824123 0.82409755 0.57325684]]]
Create array with random int number
np.random.randint() function, in the following example:
0 means the start range of the number9 means the end range of the numbersize=(3,4) means the structure of the arraya = np.random.randint(0, 9, size=(3,4))
print(a)
# Output:
# [[4 0 5 6]
# [6 8 0 7]
# [6 7 4 1]]
ArraySpecifying or Checking your array type
dtype=np.{type} to specify it.a = np.array([1, 2, 3], dtype=np.float64)
b = np.array([1, 2, 3], dtype=np.complex64)
print(a) # Output: [1. 2. 3.]
print(b) # Output: [1.+0.j 2.+0.j 3.+0.j]
a = np.array(([1, 2, 3, 4], [5, 6, 7, 8]), dtype=np.in32)
print(a.dtype) # Output: int32
a = a.astype(np.float64)
print(a.dtype) # Output: float64
| Data type | Description |
|---|---|
| bool | Bool type (True/False) |
| int64 | Integer |
| uint64 | No symbol integer (>0) |
| float64 | Float type number |
| complex64 | Complex numbers |
Specifying or Checking your array dimension and shape
In former period, we have a glance at array shape when using np.random.random(), here we will explain more with array.ndim and array.shape:
np.random.random((2,3,4,5,6)), guess how many dimension this array has?np.ndim to verify:a = np.random.random((2, 3, 4, 5, 6))
print(a.shape) # Output: (2, 3, 4, 5, 6)
print(a.ndim) # Output: 5
If you want to get more information about array, there are several guidances:
Here is a trick:
array.size:a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(a.size) # Output: 12
Here is another trick:
array.flatten():a = np.array([[1,2,3],[4,5,6]])
print(a) # Output: [[1 2 3] [4 5 6]]
print(a.shape) # Output: (2, 3)
b = a.flatten()
print(b) # Output: [1 2 3 4 5 6]
print(b.shape) # Output: (6,)
Reshaping or Resizing your array
In last chapter we have a clear view of array structure, but whether we can change array shape without changing the data in it? The answer is yes! We will use two functions here:
array.reshape()array.resizeYou may wonder what are the differences of two functions? Actually they have the same feature but different ways to use, see the example here:
a = np.random.randint(0,10,size=(3,4))
print(a)
# [[5 8 2 4]
# [5 0 0 1]
# [3 5 2 6]]
b = a.reshape((2,6))
print(b)
# [[5 8 2 4 5 0]
# [0 1 3 5 2 6]]
a.resize(2,6)
print(a)
# [[5 8 2 4 5 0]
# [0 1 3 5 2 6]]
Yes! You may find that array.reshape() return a new array while the array.resize() directly change the shape on original array, so be careful with resize which will may destroy your array.
I also want to remind you that when you use the reshape method, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements. (Quote from here)
Add new axis(dimension) to an array
Here comes another question: How can we extent the dimension of any array? We use np.newaxis or np.expand_dims to solve the problem, see example here:
np.newaxis to create new dimension:a = np.array([1, 2, 3, 4, 5, 6])
print(a.shape) # Output: (6,)
row_vector = a[np.newaxis, :]
print(row_vector.shape) # Output: (1, 6)
col_vector = a[:, np.newaxis]
print(col_vector.shape) # Output: (6, 1)
mul_vector = a[np.newaxis, :, np.newaxis]
print(mul_vector.shape) # Output: (1, 6, 1)
np.expand_dims() to create new dimension, axis={number} means the order of dimension you want to expand.a = np.array([1, 2, 3, 4, 5, 6])
print(a.shape) # Output: (6)
b = np.expand_dims(a, axis=1)
print(b.shape) # Output: (6, 1)
c = np.expand_dims(a, axis=[0,2,3])
print(c.shape) # Output: (1, 6, 1, 1)
If you want to get more information about expanding axis, there are several guidances:
ArrayIf we have a array like this: a = np.array([1, 2, 3, 4, 5]):
a = np.array([1, 2, 3, 4, 5])
print(a[1]) # Output: 2 Choose the 2 order element.
print(a[0:2]) # Output: [1 2] Choose the 1 and 2 element
print(a[2:]) # Output: [3 4 5] Choose the element after 2
print(a[:2]) # Output: [1 2] Choose the element before 2
print(a[-2:]) # Output: [4 5] Choose the element after last 2
print(a[:-2]) # Output: [1 2 3] Choose the elements before last 2
print(a[::2]) # Output: [1 3 5] Choose all the elements with step 2

If we have a more complex array like this:
a = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]],[[25, 26, 27, 28], [29, 30, 31, 32], [33, 34, 35, 36]]])
Ok, right now you may say: wtf, why you give me an example like this? But the more serious problems come, please answer the outcome of these:
print(a[0])
print(a[0,2])
print(a[0:2])
print(a[[0,2]])
print(a[[0,2],1])
No worry, I will go though with you. First, you should have a view of how this array constructs in below picture, then we will check every kinds of indexing and slicing array mentioned above:

a[0]: The first dimension of the array.
print(a[0])
# Output:
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
print(a[0].shape)
# Output: (3, 4)
a[0,2]: The first dimension and its third element:
print(a[0,2]) # Output: [ 9 10 11 12]
print(a[0,2].shape) # Output: (4,)
a[0:2]: The first dimension to the second dimension:
print(a[0:2])
# Output:
# [[[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# [[13 14 15 16]
# [17 18 19 20]
# [21 22 23 24]]]
print(a[0:2].shape)
# Output: (2, 3, 4)
a[[0,2]]: The first and the third dimension:
print(a[[0,2]])
# Output:
# [[[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# [[25 26 27 28]
# [29 30 31 32]
# [33 34 35 36]]]
print(a[0,2].shape)
# Output: (2, 3, 4)
a[[0,2],1]: The first and third dimensions and their second elements:
print(a[[0,2],1])
# Output:
# [[ 5 6 7 8]
# [29 30 31 32]]
print(a[[0,2],1].shape)
# Output:
# (2, 4)
Some tricks of getting array numbers:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])Get numbers with some conditions:
print(a[a<5]) # Output: [1 2 3 4]
five_up = (a >= 5)
print(a[five_up]) # Output: [ 5 6 7 8 9 10 11 12]
two_up_ten_down = (a > 2) & (a < 10)
print(a[two_up_ten_down]) # Output: [3 4 5 6 7 8 9]
divisible_by_2 = (a % 2 == 0)
print(a[divisible_by_2]) # Output: [ 2 4 6 8 10 12]
Bool index:
range_condition = (a > 9) | (a <= 3)
print(range_condition)
# Output:
# [[ True True True False]
# [False False False False]
# [False True True True]]
ArrayAddition, Subtraction, Multiplication, Division
a = np.array([4,6], dtype=np.float32)
b = np.array([2,5], dtype=np.float32)
print(a+b) # Output: [ 6. 11.]
print(a-b) # Output: [2. 1.]
print(a*b) # Output: [ 8. 30.]
print(a/b) # Output: [2. 1.2]
print(a*2) # Output: [ 8. 12.]
print(b/2) # Output: [1. 2.5]
More useful array operations
a = np.random.random((2, 3, 4))
print(a)
# Output:
# [[[0.74613068 0.50713338 0.85054524 0.94298687]
# [0.77740829 0.02997798 0.63936557 0.87512259]
# [0.94362238 0.69907517 0.98721314 0.15294181]]
# [[0.13813987 0.62780067 0.03182895 0.77030893]
# [0.37040885 0.27097483 0.06284363 0.9514591 ]
# [0.06638285 0.88425577 0.34459018 0.44372085]]]
print(a.sum()) # Output: 13.114237592310422
print(a.min()) # Output: 0.029977983465746427
print(a.max()) # Output: 0.9872131446108788
Calculate array with mathematical formula
In neural network training, we often want to calculate the mean loss of neural network output compared with true data. We can directly calculate these array with Numpy mathematical functions.
y_pred = np.array([0.1, 0.2, 0.3, 0.4, 0.5], dtype=np.float32)
y_true = np.array([0., 0., 0., 1., 1.], dtype=np.float32)
mse_loss = np.mean(np.sum(np.square(y_pred - y_true)))
print(mse_loss) # Output: 0.75
Array1D array
import matplotlib.pyplot as plt
a = np.array([2, 1, 5, 7, 4, 6, 8, 14, 10, 9, 18, 20, 22])
plt.plot(a)
plt.show()

2D array
x = np.linspace(0, 5, 20)
y = np.linspace(0, 10, 20)
plt.plot(x, y, 'purple') # line
plt.plot(x, y, 'o') # dots
plt.show()

3D array
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
X = np.arange(-5, 5, 0.15)
Y = np.arange(-5, 5, 0.15)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis')
plt.show()

ArraySave array to .npy file
a = np.array([1, 2, 3, 4, 5, 6], dtype=np.float32)
np.save('test', a)
b = np.load('test.npy')
print(b) # [1. 2. 3. 4. 5. 6.]
Save array to .csv file
csv_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
np.savetxt('new_file.csv', csv_arr)
load_csv_arr = np.loadtxt('new_file.csv')
print(load_csv_arr) # [1. 2. 3. 4. 5. 6. 7. 8.]
Save array with pd.DataFrame
pandasimport pandas as pd
a = np.array([[-2.58289208, 0.43014843, -1.24082018, 1.59572603],
[ 0.99027828, 1.17150989, 0.94125714, -0.14692469],
[ 0.76989341, 0.81299683, -0.95068423, 0.11769564],
[ 0.20484034, 0.34784527, 1.96979195, 0.51992837]])
df = pd.DataFrame(a) # Change array form into dataframe.
df.to_csv('pd.csv', index=False) # Store dataframe in csv without index.
data = pd.read_csv('pd.csv') # Read csv file data.
arr = data.to_numpy() # Change dataframe form to array.
print(data)
# Output:
# 0 1 2 3
# 0 -2.582892 0.430148 -1.240820 1.595726
# 1 0.990278 1.171510 0.941257 -0.146925
# 2 0.769893 0.812997 -0.950684 0.117696
# 3 0.204840 0.347845 1.969792 0.519928
print(arr)
# Output:
# [[-2.58289208 0.43014843 -1.24082018 1.59572603]
# [ 0.99027828 1.17150989 0.94125714 -0.14692469]
# [ 0.76989341 0.81299683 -0.95068423 0.11769564]
# [ 0.20484034 0.34784527 1.96979195 0.51992837]]
Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!