1.基本结构
1.数组的建立
numbers = np.array([1,2,3,4])#建立数组
print(numbers)
numbers.dtype类型
输出
[1 2 3 4]
dtype('int32')
#数组每个元素的类型都是一样的,int->float->char
2.numpy索引取值,与列表一样
例一
vector = numpy.array([5,10,15,20])
print(vector[0:3])
输出
[ 5 10 15]
例二
matrix = np.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
print(matrix[:,1])
输出
[10 25 40]
例三
matrix=np.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
print(matrix[:,0:2])
输出
[[ 5 10]
[20 25]
[35 40]]
2.判断
例一
vector = np.array([5,10,15,20])
vector==10
输出
array([False, True, False, False])
例二
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
matrix == 25
输出
array([[False, False, False],
[False, True, False],
[False, False, False]])
例三
vector = numpy.array([5,10,15,20])
equal_to_ten = (vector==10)
print(equal_to_ten)
print(vector[equal_to_ten])
输出
[False True False False]
[10]
例四
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
second_column_25 =(matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25,:])
输出
[False True False]
[[20 25 30]]
例五(判断与或非)
vector = np.array([5,10,15,20])
equal_to_ten_and_five=(vector == 10) & (vector==5)
print(equal_to_ten_and_five)
equal_to_ten_or_five=(vector==10)|(vector==5)
print(equal_to_ten_or_five)
vector[equal_to_ten_or_five]=50
print(vector)
输出
[False False False False]
[ True True False False]
[50 50 15 20]
例六(类型转换)
vector = numpy.array(['1','2','3'])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print(vector.dtype)
print(vector)
例七(按行按列求和)
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
matrix.sum(axis = 1)
输出
array([ 30, 75, 120])
matrix.sum(axis = 0)
输出
array([60, 75, 90])
2.numpy常用函数
1.矩阵变换
print(np.arange(15))
a = np.arange(15).reshape(3,5)
a
输出‘
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
其他
a.shape#矩阵的大小
(3, 5)
a.ndim#矩阵的维度
2
a.dtype.name#矩阵的数据类型
'int32'
a.size
15
2.初始化矩阵
初始化零矩阵
np.zeros((3,4))
输出
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
初始化值全为1的矩阵
np.ones((2,3,4),dtype=np.int32)#指定类型
输出
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
初始化序列
np.arange(0,2,0.3)
np.arange(12).reshape(4,3)
输出
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
初始化随机数矩阵
np.random.random((2,3))#矩阵的大小
输出
array([[0.33435409, 0.10347198, 0.57990129],
[0.56307668, 0.17773752, 0.57449889]])
在区间内平均取值
from numpy import pi
np.linspace(0,2*pi,100)#0起始,2*pi终止,100个数
输出`
array([0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607,
0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,
0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,
0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,
1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,
1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,
1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,
2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,
2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,
2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,
3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,
3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,
3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,
4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,
4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,
4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,
5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,
5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,
5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,
6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])
数学运算
a = np.array([20,30,40,50])
b=np.arange(4)
print(a)
print(b)
c=a-b
print(c)
c=c-1
print(c)
b**2
print(b**2)
print(a<35)
[20 30 40 50]
[0 1 2 3]
[20 29 38 47]
[19 28 37 46]
[0 1 4 9]
[ True True False False]
乘法
A=np.array([[1,1],
[0,1]])
B=np.array([[2,0],
[3,4]])
print(A*B)#对应位置相乘
print(A.dot(B))#矩阵乘法(前行后列)
print(np.dot(A,B))
输出
[[2 0]
[0 4]]
[[5 4]
[3 4]]
[[5 4]
[3 4]]
3. 矩阵常用操作
1.e和开方
B = np.arange(3)
print(np.exp(B))
print(np.sqrt(B))
输出
[1. 2.71828183 7.3890561 ]
[0. 1. 1.41421356]
a = np.floor(10*np.random.random((3,4)))#floor()化为向下取整
print(a)
print(a.ravel())#展开为一维
a.shape=(6,2)#可以指定剩一个,比如a.shape(6,)
print(a)
print(a.T)#转置
矩阵拼接
a=np.floor(10*np.random.random((2,2)))
b=np.floor(10*np.random.random((2,2)))
print(a)
print(b)
print(np.hstack((a,b)))#横
print(np.vstack((a,b)))#列
输出
[[5. 9.]
[2. 7.]]
[[6. 3.]
[8. 7.]]
[[5. 9. 6. 3.]
[2. 7. 8. 7.]]
[[5. 9.]
[2. 7.]
[6. 3.]
[8. 7.]]
矩阵分割
a=np.floor(10*np.random.random((2,12)))
print(a)
print(np.hsplit(a,3))
print(np.hsplit(a,3))#横分割
a=np.floor(10*np.random.random((12,2)))
print(a)
np.vsplit()#列分割
输出
[[5. 5. 6. 3. 0. 5. 8. 8. 7. 5. 7. 3.]
[1. 1. 7. 4. 4. 6. 2. 8. 6. 6. 8. 3.]]
[array([[5., 5., 6., 3.],
[1., 1., 7., 4.]]), array([[0., 5., 8., 8.],
[4., 6., 2., 8.]]), array([[7., 5., 7., 3.],
[6., 6., 8., 3.]])]
[array([[5., 5., 6., 3.],
[1., 1., 7., 4.]]), array([[0., 5., 8., 8.],
[4., 6., 2., 8.]]), array([[7., 5., 7., 3.],
[6., 6., 8., 3.]])]
[[4. 6.]
[6. 2.]
[0. 9.]
[2. 4.]
[7. 5.]
[0. 9.]
[2. 9.]
[4. 4.]
[8. 9.]
[2. 3.]
[6. 2.]
[6. 0.]]
[array([[4., 6.],
[6., 2.],
[0., 9.],
[2., 4.]]),
array([[7., 5.],
[0., 9.],
[2., 9.],
[4., 4.]]),
array([[8., 9.],
[2., 3.],
[6., 2.],
[6., 0.]])]
4.复制(我觉得emmm)
a = np.arange(12)
b=a
print(b is a)
b.shape=(3,4)
print(a.shape)
print(id(a))#类似于指针
print(id(b))
True
(3, 4)
2571844056768
2571844056768
c=a.view()#不一样但是指向的值一样
print(c is a)
c.shape=(2,6)
print(a.shape)
c[0,4]=1234
print(a)
print(id(a))
print(id(c))
False
(3, 4)
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
2571844056768
2571844560816
d = a.copy()#合乎想象的复制
d is a
d[0,0]=9999
print(d)
print(a)
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
获取索引
data = np.sin(np.arange(20)).reshape(5,4)
print(data)
ind = data.argmax(axis=0)#获取列最大值索引
print(ind)
data_max=data[ind,range(data.shape[1])]
print(data_max)
输出
[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
a=np.arange(0,40,10)
print(a)
b = np.tile(a,(3,5,))#扩展
print(b)
输出
[ 0 10 20 30]
[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
排序
a = np.array([[4,3,5],[1,2,1]])
print(a)
b=np.sort(a,axis = 1)
print(b)
a.sort(axis = 1)
print(a)
a = np.array([4,3,1,2])
j = np.argsort(a)#从小到大索引
print(j)
print(a[j])
输出
[[4 3 5]
[1 2 1]]
[[3 4 5]
[1 1 2]]
[[3 4 5]
[1 1 2]]
[2 3 1 0]
[1 2 3 4]

这篇博客详细介绍了numpy的基本结构,包括数组创建、索引取值和判断方法。进一步探讨了numpy的常用函数,如矩阵变换、初始化、数学运算。此外,还涉及了矩阵的常见操作,如e的幂运算、矩阵拼接、分割,以及复制和排序等实用技巧。
1261

被折叠的 条评论
为什么被折叠?



