import numpy as np
#列表转化为矩阵
array = np.array([1,2,3])
print(array)
array2 = np.array([[1,2,3],
[4,5,6]])
print(array2)
[1 2 3]
[[1 2 3]
[4 5 6]]
#查看属性结果
print(array.ndim)#维数
print(array.shape)#行列数,若是一维矩阵数组,则第一个显示的是列数
print(array.size)#元素个数
print(array2.shape)
1
(3,)
3
(2, 3)
array = np.array([1,2,3])
array
array([1, 2, 3])
array.dtype #默认即为int32
dtype('int32')
#将其数据类型改成64int
a = np.array([1,2,3],dtype=np.int64)
a.dtype
dtype('int64')
a.dtype = np.float32
a.dtype = np.float64
a = np.array([[1,2,3],[4,5,6]])
print(a)#二维数组
[[1 2 3]
[4 5 6]]
#全0数组
b = np.zeros(shape=(2,3),dtype=np.int)
print(b)
[[0 0 0]
[0 0 0]]
#全一数组
c = np.ones(shape=(3,2),dtype=np.float16)
print(c)
[[1. 1.]
[1. 1.]
[1. 1.]]
#全空数组,每个值都接近0
d = np.empty((4,5))
print(d)
[[6.23042070e-307 4.67296746e-307 1.69121096e-306 3.33772792e-307
6.23047503e-307]
[8.45593934e-307 7.56593017e-307 1.11261027e-306 1.11261502e-306
1.42410839e-306]
[7.56597770e-307 6.23059726e-307 1.42419530e-306 1.37962185e-306
1.37962049e-306]
[1.24610723e-306 1.37962185e-306 7.56591659e-307 1.42410974e-306
2.22507386e-306]]
#创建连续数组,其实就是一个等差数列
e = np.arange(1,10,5) #类似于range
print(e)
[1 6]
#reshape改变数据形状
f = np.arange(12).reshape((3,4))
print(f)
# 无以下用法
#g = np.arange(12,dtype=(3,4))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
#用linspace创建线段型数据
h = np.linspace(1,10,20)
print(h)
#更改它的形状
h.reshape((4,5))
print(h)
[ 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. ]
[ 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. ]
a = np.array([1,2,3])
b = np.array([4,5,6])
print(a,b)
[1 2 3] [4 5 6]
#矩阵相加
print(a+b)
#矩阵相减
print(a-b)
#两矩阵所对应元素相乘
print(a*b)
#矩阵每个元素平方
print(a**2)
[5 7 9]
[-3 -3 -3]
[ 4 10 18]
[1 4 9]
#每个元素先sin处理后×10
print(10*np.sin(a))
[8.41470985 9.09297427 1.41120008]
print(a<2) #a=[1,2,3]
[ True False False]
#数学意义上的矩阵相乘
c = np.arange(1,10).reshape(3,3)
print(c)
[[1 2 3]
[4 5 6]
[7 8 9]]
d = np.ones((3,1),dtype=np.int)
print(d)
[[1]
[1]
[1]]
print(np.dot(c,d))
#等价于
print(c.dot(d))
[[ 6]
[15]
[24]]
[[ 6]
[15]
[24]]
# 对应的是对所有元素求和求最小求最大
print(np.sum(c))
print(np.min(c))
print(np.max(c))
45
1
9
print(np.sum(c,axis=0)) #0是对列操作
print(np.sum(c,axis=1)) #1是对行操作
[12 15 18]
[ 6 15 24]
print(np.min(c,axis=0))#列
print(np.max(c,axis=1))#行
[1 2 3]
[3 6 9]
array = np.arange(1,5).reshape(2,2)
print(array)
[[1 2]
[3 4]]
print(np.argmax(array))
print(np.argmin(array))
#等价于
print(array.argmax())
print(array.argmin())
3
0
3
0
print(np.mean(array))
print(np.average(array))
2.5
2.5
print(np.median(array))
2.5
返回一个一维矩阵数组,矩阵每一个元素都是从原矩阵首项累加所得
该函数计算的是每一行中后一项与前一项之差
print(np.cumsum(array))
[ 1 3 6 10]
print(np.diff(array))
[[1]
[1]]
这个函数将所有非零元素的行与列坐标分割开,重构成两个分别关于行和列的矩阵。
print(np.nonzero(array))
(array([0, 0, 1, 1], dtype=int64), array([0, 1, 0, 1], dtype=int64))
a = np.arange(4,10).reshape(2,3)
print(a)
print(a.nonzero())
[[4 5 6]
[7 8 9]]
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))
这里的排序仅仅针对每一行,从小到大
a = np.array([[4,3,1],[5,8,4]])
print(a)
[[4 3 1]
[5 8 4]]
a.sort()
print(a)
#等价于
print(np.sort(a))
[[1 3 4]
[4 5 8]]
[[1 3 4]
[4 5 8]]
print(a.T)
#并未改变a
print(a.transpose())
[[1 4]
[3 5]
[4 8]]
[[1 4]
[3 5]
[4 8]]
这个函数的格式是clip(Array,Array_min,Array_max),顾名思义,Array指的是将要被执行用的矩阵,而后面的最小值最大值则用于让函数判断矩阵中元素是否有比最小值小的或者比最大值大的元素,并将这些指定的元素转换为最小值或者最大值。
print(a.clip(5,9))
[[5 5 5]
[5 5 8]]
基本等同于列表索引
import numpy as np
A = np.arange(3,15)
print(A)
print(A[2])
[ 3 4 5 6 7 8 9 10 11 12 13 14]
5
A = A.reshape((3,4))
print(A)
print(A[2])
print(A[2][0])
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[11 12 13 14]
11
print(A[1][3])
#等价于
print(A[1,3])
10
10
#切片,顾头不顾尾
print(A[1,1:3])
[8 9]
#逐行打印
for row in A:
print(row)
#逐列打印
print('======')
for column in A.T:
print(column)
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
======
[ 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]
#flat是一个迭代器,本身是一个对象属性
print(A.flat)
for item in A.flat:
print(item)
3
4
5
6
7
8
9
10
11
12
13
14
#np.vstack()
'''
本身属于一种上下合并,即对括号中的两个整体进行对应操作
'''
A = np.array([1,2,3])
B = np.array([4,5,6])
print(np.vstack((A,B)))
#print(A.vstack(B)) X
[[1 2 3]
[4 5 6]]
#np.hstack()左右合并
C = np.hstack((A,B))
print(C)
[1 2 3 4 5 6]
#np.newaxis()
print(A)
print(A.T) #转置没有变化
print(A[np.newaxis,:])
print(A[np.newaxis,:].shape)
print(A[:,np.newaxis])
print(A[:,np.newaxis].shape)
[1 2 3]
[1 2 3]
[[1 2 3]]
(1, 3)
[[1]
[2]
[3]]
(3, 1)
#综合以上几种方法
a = np.array([1,1,1])[:,np.newaxis]
b = np.array([2,2,2])[:,np.newaxis]
c = np.vstack((a,b))
d = np.hstack((a,b))
print(c)
print('====')
print(d)
[[1]
[1]
[1]
[2]
[2]
[2]]
====
[[1 2]
[1 2]
[1 2]]
#np.concatenate()
'''
当你的合并操作需要针对多个矩阵或者序列时
借助concatenate函数可能会让你使用起来比前述函数更简单
行:1
列:0
'''
e = np.concatenate((a,b,b,a),axis=0)
print(e)
print('====')
f = np.concatenate((a,b,b,a),axis=1)
print(f)
[[1]
[1]
[1]
[2]
[2]
[2]
[2]
[2]
[2]
[1]
[1]
[1]]
====
[[1 2 2 1]
[1 2 2 1]
[1 2 2 1]]
a = np.arange(3,15).reshape(3,4)
print(a)
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
c,d=np.split(a,2,axis=1)
print(c)
print('===')
print(d)
[[ 3 4]
[ 7 8]
[11 12]]
===
[[ 5 6]
[ 9 10]
[13 14]]
print(np.split(a,3,axis=0))
[array([[3, 4, 5, 6]]), array([[ 7, 8, 9, 10]]), array([[11, 12, 13, 14]])]
'''
a有三行四列,split只能等量对分
'''
# 会报错
#print(np.split(a,5,axis=1))
'\na有三行四列,split只能等量对分\n'
在机器学习时经常会需要将数据做不等量的分割,因此解决办法为np.array_split()
a1,b1,c1=np.array_split(a,3,axis=1)
print(a1)
print(b1)
print(c1)
[[ 3 4]
[ 7 8]
[11 12]]
[[ 5]
[ 9]
[13]]
[[ 6]
[10]
[14]]
print(np.vsplit(a,3))
#等价于
print(np.split(a,3,axis=0))
print(np.hsplit(a,4))
print(np.split(a,4,axis=1))
[array([[3, 4, 5, 6]]), array([[ 7, 8, 9, 10]]), array([[11, 12, 13, 14]])]
[array([[3, 4, 5, 6]]), array([[ 7, 8, 9, 10]]), array([[11, 12, 13, 14]])]
[array([[ 3],
[ 7],
[11]]), array([[ 4],
[ 8],
[12]]), array([[ 5],
[ 9],
[13]]), array([[ 6],
[10],
[14]])]
[array([[ 3],
[ 7],
[11]]), array([[ 4],
[ 8],
[12]]), array([[ 5],
[ 9],
[13]]), array([[ 6],
[10],
[14]])]
a = np.arange(4)
print(a)
[0 1 2 3]
b = a
c = a
d = b
a[0] = 11
print(a,b,c,d)
[11 1 2 3] [11 1 2 3] [11 1 2 3] [11 1 2 3]
b is a
c is a
d is a
True
b = a.copy()
print(b)
a[3] = 44
print(a)
print(b)
[11 1 2 3]
[11 1 2 44]
[11 1 2 3]