三、numpy的属性
1.array的创建
import numpy as np
array = np.array([[2, 23, 4],
[2, 32, 4]]
2.array的形状、数量、维度数
print('number of dim', array.ndim) #number of dim 2
print('number of shape', array.shape) #number of shape (2, 3)
print('size', array.size) #size 6
四、numpy的创建
1.numpy是什么型 (int32,int64,float等等)
a = np.array([2,23,4],dtype=np.int) #int32
a = np.array([2,23,4],dtype=np.int64) #int64
2.定义numpy的矩阵
b = np.array([[2,23,4],
[2,32,4]]) #[[ 2 23 4]
# [ 2 32 4]]
c = np.zeros((3,4)) # [[0. 0. 0. 0.]
# ones # [0. 0. 0. 0.]
# empty(接近零) # [0. 0. 0. 0.]]
e = np.arange(10,20,2) #[10 12 14 16 18](有头无尾)
f = np.arange(12).reshape((3,4))
#[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]](有头无尾)
g = np.linspace(1,10,20)
#[ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105
# 3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632
# 6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158
# 9.52631579 10. ]
h = np.linspace(1,10,6).reshape((2,3))
#[[ 1. 2.8 4.6]
# [ 6.4 8.2 10. ]]
五、 numpy的运算
1.加减乘除平方sin
a =np.array([10,20,30,40])
b = np.arange(4)
print (a,b) # [10 20 30 40] [0 1 2 3]
c = a-b
print(c) # [10 19 28 37]
d = b**2
print(d) #[0 1 4 9]
e = 10*np.sin(a)
print(e) #[-5.44021111 9.12945251 -9.88031624 7.4511316 ]
f = np.arange(4)
print(f<3) #[ True True True False]
print(f==3) #[False False False True]
g =np.array([[1,1],
[0,1]])
h =np.arange(4).reshape((2,2))
print(g,h)
#[[1 1]
# [0 1]] [[0 1]
# [2 3]]
i =g*h #点乘
#[[2 4]
# [2 3]]
i_dot = np.dot(g,h) #叉乘
#[[0 1]
# [0 3]]
j = np.random.random((2,4))
#[[0.80611351 0.64377618 0.45498117 0.45544791]
# [0.55785206 0.02234942 0.0204626 0.7681106 ]]
print(np.sum(j,axis = 1)) #使用1值表示沿着每一行或者列标签模向执行对应的方法
#[1.4145513 2.03974314]
print(np.min(j,axis = 0)) #使用0值表示沿着每一列或行标签\索引值向下执行方法
# [0.22468377 0.77770998 0.15556007 0.22027182]
六、numpy的运算(下)
a = np.arange(2,14).reshape((3,4))
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
print(np.argmin(a)) #找出最小值的索引
# 0
print(np.mean(a)) #print(np.average(a))平均值
# 7.5
print(np.median(a)) #中位数
#7.5
print(np.cumsum(a)) #累加
#[ 2 5 9 14 20 27 35 44 54 65 77 90]
print(np.diff(a)) #累差
#[[1 1 1]
# [1 1 1]
# [1 1 1]]
print(np.nonzero(a)) #前一个是非0输出值的行数,后一个是非0输出行的列数
#(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
print(np.sort(a)) #排序从小到大
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
print(np.transpose(a)) #转置
print(a.T)
#[[ 2 6 10]
# [ 3 7 11]
# [ 4 8 12]
# [ 5 9 13]]
print((a.T).dot(a)) #差乘
#[[140 158 176 194]
# [158 179 200 221]
# [176 200 224 248]
# [194 221 248 275]]
print(np.clip(a,5,9)) #5是最小值,9是最大值,所有小于5的数都让它等于5,所有大于9的数都让它等于9。
#[[5 5 5 5]
# [6 7 8 9]
# [9 9 9 9]]
print(np.mean(a,axis=0)) #沿着每一列的平均值
#[6. 7. 8. 9.]
七、numpy的索引
a=np.arange(3,15) #[ 3 4 5 6 7 8 9 10 11 12 13 14]
print(a[3]) #6
a = np.arange(3,15).reshape((3,4))
#[[ 3 4 5 6]
# [ 7 8 9 10]
# [11 12 13 14]]
print(a[2]) #第三行
#[11 12 13 14]
print(a[1][1]) #8
print(a[1,1])
print(a[2,:]) # :代表所有
#[11 12 13 14]
print(a[1,1:3])#顾头不顾尾
#[8 9]
for row in a:
print(row)
#[3 4 5 6]
#[ 7 8 9 10]
#[11 12 13 14]
for column in a.T: #对于每一列按照列的顺序进行打印
print(column)
#[ 3 7 11]
#[ 4 8 12]
#[ 5 9 13]
#[ 6 10 14]
print(a.flatten())
#[ 3 4 5 6 7 8 9 10 11 12 13 14]
for item in a.flat:
print(item)
3
4
5
6
7
8
9
10
11
12
13
14
八、numpy的array的合并
a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
print(np.vstack((a,b))) #vertical stack 竖的 c
#[[1 1 1]
# [2 2 2]]
print(a.shape, c.shape)
#(3,) (2, 3) 三列,两行三列
d = np.hstack((a, b)) #横着
#[1 1 1 2 2 2]
print(a.shape, d.shape)
#(3,) (6,)
print(a[np.newaxis, :].shape) #行上加了一个维度
#(1, 3)
print(a[:, np.newaxis].shape)
#(3, 1)
print(a[:, np.newaxis])
#[[1]
# [1]
# [1]]
print(a[ np.newaxis])
#[[1 1 1]]
d = np.concatenate((a, b, b, a))
# [1 1 1 2 2 2 2 2 2 1 1 ]
九、numpy的array的分割
a = np.arange(12).reshape((3, 4))
print(a)
#[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.split(a, 2, axis=1)) #分成两部分,两等列,三列不能整除,不可以
#[array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]
print(np.array_split(a, 3, axis=1)) #分成三份不等列
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
print(np.vsplit(a,3)) #横向分成三块
#[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
print(np.hsplit) #横向纵向分成两块
#[array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]
Process finished with exit code 0
Process finished with exit code 0
十、numpy_copy&deep copy
浅拷贝
#[11 1 2 3]
d[1:3] = [22,33]
print(d)
#[11 22 33 3]
b = a.copy() #deep copy
print(a) #[11 22 33 3]
print(b) #[11 22 33 3]
a[3] = 44
print(a) #[11 22 33 44]
print(b) #[11 22 33 3]
print(np.eye(5))
#[[1. 0. 0. 0. 0.]
# [0. 1. 0. 0. 0.]
# [0. 0. 1. 0. 0.]
# [0. 0. 0. 1. 0.]
# [0. 0. 0. 0. 1.]]
data =np.arange(100, step=10)
[ 0 10 20 30 40 50 60 70 80 90]
print(data[2])
20
print(data[2:5])
[20 30 40]
print(data[:3])
[ 0 10 20]
print(data[5:])
[50 60 70 80 90]
data[5:-1]
print(data)
[ 0 10 20 30 40 50 60 70 80 90]
data = np.arange(16).reshape(4,4)
print(data)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
print(data[1])
[4 5 6 7]
print(data[1:3])
[[ 4 5 6 7]
[ 8 9 10 11]]
print(data[:, 2:4])
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
print(data[[1,3],[2,3]])
[ 6 15]
print(data[1,2],data[3,3])
6 15
print(data>10)
[[False False False False]
[False False False False]
[False False False True]
[ True True True True]]
x=np.arange(1,5).reshape(2,2,)
y = np.arange(5,9).reshape(2,2)
print(x/y)
[[0.2 0.33333333]
[0.42857143 0.5 ]]