About numpy 2020.3.14
These days I have finished the video learning of python from av37566782, besides, I learnt a useful package called numpy.
So today I want to share some basic skills of the package numpy.
1.Import the package numpy
step1. Install the numpy package from Project Interpreter
step2. Type “import numpy as np” in the pyfile.
import numpy as np
2.Create an array with numpy
There are three ways of it which are as bellow.
t1 =np.array([1,2,3,4,5])
t2= np.array(range(1,6))
t3=np.arange(1,6)
t1 =np.array([1,2,3,4,5])
t2= np.array(range(1,6))
t3=np.arange(1,6)
print(t1)
print(t2)
print(t3)
the result is:
ps: If we want to create an array with random numbers,
we could type t4=np.random.random((rownum1,colnum2))
t4= np.random.random((3,3))
result:
3.Change the shape of an array
Change the shape of an array.
Format: array.reshape(num1,num2)
For example: t4 (from 3 rows,3 columns to 1 row, 9 columns)
print(t4.reshape(1,9))
result:
4. Basic calculation of numpy array
4.1 Addition: c = a +b
4.2 Subtraction:d = a -b
4.3 Multiplication:
np.dot(array1, array2) or array1.dot(array2)
4.4 Maximum & Minimum:
Format:np.min(array) & np.max(array)
Especially, if we want to get the maximum of each row or each column, we could type like this: np.min(a,axis=1) or np.max(a,axis=0) (axis=1represents each row and axis=0 represents each column)
4.5 mean & median & average:
Calculate the mean, median, average of an array.
Format:np.mean(array) &np.median(array) & np.average(array)
4.6 cumsum :
Calculate the accumulated value of each row of an array.
Format:np.cumsum(array)
4.7 diff:
Calculate the cumulative value of each row of an array.
Format:np.diff(array)
4.8 transpose:
Calculate the transpose of an array.
Format: np.transpose(array) or array.T
4.9 clip:
Change the number that less than set_lowestnum into set_lowestnum and change the number that greater than set_biggestnum into set_biggestnum.
Format:np.clip(array, set_lowestnum, set_biggestnum)
4.10 flatten:
Reduce the dimension of an array to one dimension.
Format: array.flatten() #by row , a.flatten(‘A’)#by row, a.flatten(‘F’) #by column
a =np.array([[3,5],
[6,8]])
b = np.arange(4).reshape((2,2))
c = a +b
d = a -b
e = np.dot(a,b)
f = a.dot(b)
print(np.min(a,axis=1))
print(np.max(a,axis=0))
print(np.average(a))
print(np.mean(a))
print(np.median(a))
print(np.cumsum(a))
print(np.diff(a))
tt=np.clip(a,5,6)
print(a.flatten())
print(a.flatten('F'))
print(c)
print(d)
print(e)
print(f)
print(tt)
5.Mergence and split of arrays
5.1Mergence of arrays
5.1.1 vertical stack
Merge two or more arrays vertically.
np.vstack((array1,array2))
np.concatenate((array1, array2, array3…),axis=0)
5.1.2 horizontal stack
Merge two or more arrays horizontally.
np.hstack((array1,array2))
np.concatenate((array1, array2, array3…),axis=1)
print(np.vstack((a,b))) #vertical stack
print(np.hstack((a,b))) #horizontal stack
print(np.concatenate((a,b,a),axis=0)) #vertical stack
print(np.concatenate((a,b,a),axis=1)) #horizontal stack
5.2 Split of an array
5.2.1 vertical split
Split an array into equal or unequal columns .
np.split(array,2,axis=0)
np.array_split(array,3,axis=0) (for unequal partition)
np.vsplit(a,2)
5.2.2 horizontal split
Split an array into equal or unequal rows.
print(np.split(array,2,axis=1))
print(np.array_split(array,3,axis=1)) (for unequal partition)
print(np.hsplit(array,2))
print(np.split(a,2,axis=0))
print(np.array_split(a,3,axis=0))
print(np.vsplit(a,2))
print(np.split(a,2,axis=1))
print(np.array_split(a,3,axis=1))
print(np.hsplit(a,2))
6.Copy & deep copy in numpy
6.1 Copy
Associate array2 and array1, if we change the number in array1, the same number in array2 will also be changed.
Format:array2 = array1
arr1=np.array([1,2,3,4,5])
arr2=arr1
arr1[0]=6
print(arr2)
result:
6.2 Deep copy
Assign the value of array1 to array2, if we change the number in array1, the same number in array2 will not change.
Format: array2=array1.copy()
arr1=np.array([1,2,3,4,5])
arr2=arr1.copy()
arr1[0]=6
print(arr2)
result:
7.Conclusion
Above I shared the most basic operations of numpy, including downloading numpy and creating ,transforming, calculating, merging, splitting, copying arrays.
After a short-term study, personally, I think the most important is the refinement and summary of methods. The same purpose can be achieved by multiple methods as I showed above. Choosing the most simple and appropriate method requires long-term accumulation and thinking.
I wish there would be of some help for you.
Thank you for reading.
Resources:
https://www.bilibili.com/video/av37566782?
https://www.bilibili.com/video/av57023791?
https://www.bilibili.com/video/av16378934?