学习python的numpy库的一些笔记
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
numpy的属性
- array.ndim
- array.shape
- array.size
array = np.array([[1,2,3],
[4,5,6]])
print(array)
[[1 2 3]
[4 5 6]]
print('number of dim:',array.ndim)
print('shape:',array.shape)
print('size:',array.size)
number of dim: 2
shape: (2, 3)
size: 6
numpy创建array
- np.array([],dtype = ?)
- np.zeros()
- np.ones()
- np.empty()
- np.linspace()
- np.arange()
a = np.array([1,122,34,24,325,43,67],dtype = np.int64)
print(a)
[ 1 122 34 24 325 43 67]
a = np.array([12,2134,2,453,6,45],dtype = np.float64)
print(a)
print(a.dtype)
[1.200e+01 2.134e+03 2.000e+00 4.530e+02 6.000e+00 4.500e+01]
float64
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
a = np.zeros((3,4),dtype = np.int64)
a
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
a = np.ones((3,4))
a
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
a = np.empty((3,4))
print(a)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
a = np.arange(10,20,2)
a
array([10, 12, 14, 16, 18])
a = np.arange(12).reshape((3,4))
a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
a = np.linspace(1,10,10,dtype = np.int64)
a
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
a = np.linspace(1,10,10,dtype = np.int64).reshape((2,5))
print(a)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
numpy基础运算
- 数学函数调用
- c[c
a = np.array([10,20,30,40])
b = np.arange(4)
c = a-b
print(a,b,c)
[10 20 30 40] [0 1 2 3] [10 19 28 37]
c = 10*np.sin(a)
c
array([-5.44021111, 9.12945251, -9.88031624, 7.4511316 ])
print(c<5)
d = c<5
c[d]
[ True False True False]
array([-5.44021111, -9.88031624])
select = np.arange(25).reshape((5,5))
a = np.ones(25).reshape((5,5))
select = select>4
print(a[select])
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
a = np.array([[1,0],[0,1]])
b = np.arange(4).reshape((2,2))
c = a*b
print(c)
c = np.dot(a,b)
print(c)
a.dot(b)
[[0 0]
[0 3]]
[[0 1]
[2 3]]
array([[0, 1],
[2, 3]])
a = np.random.random((4,4))
a[a>0.5]
array([0.75071659, 0.54349795, 0.53320956, 0.64108411, 0.86867602,
0.74984389, 0.77493445, 0.8542434 , 0.66811161, 0.72019505,
0.98319772])
print(np.sum(a),np.min(a),np.max(a))
a.reshape(2,8)
print(np.sum(a,axis = 0))
print(np.sum(a,axis = 1))
9.532522256454564 0.014624980614642125 0.9831977219683379
[2.27119303 2.96623915 1.84672342 2.44836666]
[1.95086702 2.79281358 2.40271229 2.38612936]
a = a.reshape(2,8)
print(np.sum(a,axis = 1))
print(np.sum(a,axis = 0))
[4.7436806 4.78884165]
[1.06987186 1.60495999 0.96342242 0.71532505 1.20132117 1.36127916
0.883301 1.73304161]
A = np.arange(2,14).reshape(3,4)
print(A)
print(np.argmin(A),A.min())
print(np.argmax(A),A.max())
print(np.mean(A),A.mean(),np.average(A))
print(np.median(A))
print(np.cumsum(A))
print(np.diff(A))
np.nonzero(A)
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
0 2
11 13
7.5 7.5 7.5
7.5
[ 2 5 9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
[1 1 1]
[1 1 1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]),
array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
A = np.arange(14,2,-1).reshape(3,4)
print(A)
print(np.sort(A))
print(np.transpose(A))
A.T.dot(A)
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 3]]
[[11 12 13 14]
[ 7 8 9 10]
[ 3 4 5 6]]
[[14 10 6]
[13 9 5]
[12 8 4]
[11 7 3]]
array([[332, 302, 272, 242],
[302, 275, 248, 221],
[272, 248, 224, 200],
[242, 221, 200, 179]])
print(np.clip(A,5,9))
print(np.mean(A,axis = 0))
[[9 9 9 9]
[9 9 8 7]
[6 5 5 5]]
[10. 9. 8. 7.]
numpy索引
- [:]
- flat and flatten()
A = np.arange(3,15).reshape(3,4)
print(A)
print('\n',A[2][1],'\n',A[2,1])
print('\n',A[:][1])
print(A[1,1:3])
for row in A:
print(row)
for column in A.T:
print(column)
for item in A.flat:
print(item)
print(A.flat)
print(A.flatten())
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
12
12
[ 7 8 9 10]
[8 9]
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
3
4
5
6
7
8
9
10
11
12
13
14
<numpy.flatiter object at 0x7f987c80ca00>
[ 3 4 5 6 7 8 9 10 11 12 13 14]
</numpy.flatiter>
numpy array 合并
- vstack and hstack
- concatenate
A = np.array([1,1,1])
B = np.array([2,2,2])
print(np.vstack((A,B)))
print(np.hstack((A,B)))
[[1 1 1]
[2 2 2]]
[1 1 1 2 2 2]
print(A.T.shape)
A.T
A.reshape(3,1)
print(A[:,np.newaxis])
(3,)
[[1]
[1]
[1]]
A = np.array([1,1,1])
A = A[:,np.newaxis]
B = np.array([2,2,2])
B = B[:,np.newaxis]
print(np.hstack((A,B)))
[[1 2]
[1 2]
[1 2]]
A = np.array([1,1,1]).reshape(3,1)
B = np.array([2,2,2]).reshape(3,1)
C = np.concatenate((A,B,A,B),axis = 1)
C
array([[1, 2, 1, 2],
[1, 2, 1, 2],
[1, 2, 1, 2]])
array分割
- split(,axis = ?)
- vsplit and hsplit
A = np.arange(12).reshape(3,4)
print(A)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
np.split(A,2,axis = 1)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
np.split(A,3,axis = 0)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
a,b,c = np.array_split(A,3,axis = 1)
np.vsplit(A,3)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
np.hsplit(A,2)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
copy & deep copy
deep copy需要依靠copy方法,甚至于切片的方法也是不够的
A = np.arange(3)
B = A
A[2] = 1
B
array([0, 1, 1])
assert B is A
B[1:3] = [22,33]
A
array([ 0, 22, 33])
B = A[:]
B[1:3] = [0,1]
print(A)
B
[0 0 1]
array([0, 0, 1])
B = A.copy()
B[:] = [1,2,3]
print(A)
B
[0 0 1]
array([1, 2, 3])
Original: https://blog.csdn.net/weixin_45955424/article/details/122610261
Author: NKUer_there
Title: bilibili视频学习-numpy
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/760285/
转载文章受原作者版权保护。转载请注明原作者出处!