About numpy 2020.3.14

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值