import numpy as np
array = np.array([[1,2,3],[2,3,4]])
print(array)
print('number of dim:',array.ndim) # 几阶矩阵
print('shape:',array.shape) # 多少行 多少列
print('size:',array.size) # 总共多少元素
a = np.array([2,45,23],dtype=np.int)
print(a.dtype)
print(a)
a = np.array([
[1,2,3,4,5,6,7],
[2,3,4,5,6,7,8],
[3,4,5,6,7,8,9],
[4,5,6,7,8,9,10]])
print(a)
a = np.zeros((3,3),dtype=int) # 指定几行几列
print(a)
b = np.ones((3,5),dtype=int) # 指定几行几列
print(b)
c = np.empty((4,5))
print(c)
d = np.arange(12,dtype=int).reshape(3,4)
print(d)
从1开始 到10结束 总共20段
e = np.linspace(1,10,20).reshape(4,5)
print(e)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# 减法
c = a-b
print(c)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# 加法
d = a+b
print(d)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# 幂运算
e = b**2
print(e)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# 三角函数
f1 = np.sin(a)
f2 = np.cos(a)
f3 = np.tan(a)
print(f1)
print(f2)
print(f3)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
# 判断矩阵中的数是否大于或小于指定的数
print(b<3) # [True True True False]
print(b>=2) # [False False True True]
print(b!=2) #[ True True False True]
普通逐个相乘
a = np.array([[1,1],[0,1]])
b = np.arange(4).reshape(2,2)
c = a * b
print(c)
矩阵乘法法则
a = np.array([[1,1],[0,1]])
b = np.arange(4).reshape(2,2)
c_dot = np.dot(a,b)
print(c_dot)
# 生成随机的矩阵 0-1之间
a = np.random.random((2,4)) # 指定 几行几列
b = np.sum(a) # 求和
c = np.max(a) # 最大值
d = np.min(a) # 最小值
print(a)
# 生成随机的矩阵 0-1之间
a = np.random.random((2,4)) # 指定 几行几列
b = np.sum(a) # 求和
c = np.max(a) # 最大值
d = np.min(a) # 最小值
print(a)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
print("sum:",b)
print("max:",c)
print("min:",d)
# axis 为 1 表示在行进行运算 为0 表示在列进行运算
# 求出每一行的和
print(np.sum(a,axis=1)) # [2.53072032 2.44584563]
# 求出每一列的最大值
print(np.max(a,axis=0)) # [0.98175811 0.74696834 0.82824131 0.75611382]
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
b = np.argmax(a) # 最大值的索引
c = np.argmin(a) # 最小值的索引
print(b)
print(c)
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
d = np.mean(a) # 计算矩阵的平均值
print(d.mean())
print(d)
e = np.average(a) # 计算矩阵的平均值 和mean()相同
print(e)
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
f = np.median(a) # 计算矩阵的中位数
print(f)
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
g = np.cumsum(a) # 累加
print(g) # [ 2 5 9 14 20 27 35 44 54 65 77 90]
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
h = np.diff(a) # 累差
print(h)
# [[1 1 1]
# [1 1 1]
# [1 1 1]]
import numpy as np
a = np.arange(2,14).reshape(3,4)
#[[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]
k = np.nonzero(a) # 求出非 0 的数
print(k)
# (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))
# 前一个array 代表非0数的行数 后一个array 代表非0数的列数
import numpy as np
a = np.arange(14,2,-1).reshape(3,4)
#[[14 13 12 11]
# [10 9 8 7]
# [ 6 5 4 3]]
print(a)
m = np.sort(a) # 在每一行中进行排序
print(m)
# [[11 12 13 14]
# [ 7 8 9 10]
# [ 3 4 5 6]]
import numpy as np
a = np.arange(14,2,-1).reshape(3,4)
#[[14 13 12 11]
# [10 9 8 7]
# [ 6 5 4 3]]
n = np.transpose(a)
print(n)
print(a.T) # 和np.transpose(a)效果一样
# [[14 10 6]
# [13 9 5]
# [12 8 4]
# [11 7 3]]
# 矩阵转置相乘
print((a.T).dot(a))
# [[332 302 272 242]
# [302 275 248 221]
# [272 248 224 200]
# [242 221 200 179]]
import numpy as np
a = np.arange(14,2,-1).reshape(3,4)
#[[14 13 12 11]
# [10 9 8 7]
# [ 6 5 4 3]]
# 截取矩阵
# 对于矩阵a 所有小于5的数都让它等于5 所有大于9的数都让它等于9
print(np.clip(a,5,9))
import numpy as np
a = np.arange(3,15).reshape(3,4)
# [[ 3 4 5 6]
# [ 7 8 9 10]
# [11 12 13 14]]
print(a[0]) # 查询出第0行 [3 4 5 6]
print(a[2][2]) # 查询出第2行,第2列的数据 13
print(a[2,2]) # 查询出第2行,第2列的数据 13
print(a[2,:]) # 查询出第2行所有的数 [11 12 13 14]
print(a[:,1]) # 查询出第1列所有的数 [ 4 8 12]
print(a[1,0:3]) # 查询出第1行,0到3列的数 [7 8 9]
迭代矩阵,默认是迭代矩阵的每一行
迭代矩阵的每一行
import numpy as np
a = np.arange(3,15).reshape(3,4)
# [[ 3 4 5 6]
# [ 7 8 9 10]
# [11 12 13 14]]
for row in a:
print(row)
# [[ 3 4 5 6]
# [ 7 8 9 10]
# [11 12 13 14]]
迭代矩阵的每一列
import numpy as np
a = np.arange(3,15).reshape(3,4)
# [[ 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]
迭代矩阵的每一项
import numpy as np
a = np.arange(3,15).reshape(3,4)
# [[ 3 4 5 6]
# [ 7 8 9 10]
# [11 12 13 14]]
print(a.flatten()) # 将矩阵转换为一行 [ 3 4 5 6 7 8 9 10 11 12 13 14]
print(a.reshape(1,a.size)) # 与a.flatten()效果相同
for item in a.flat:
print(item)
import numpy as np
a = np.array([1,1,1])
b = np.array([2,2,2])
print(np.vstack((a,b))) # 进行上下的合并
# [[1 1 1]
# [2 2 2]]
print(np.hstack((a,b))) # 左右合并 [1 1 1 2 2 2]
# np.newaxis 的意思 np.newaxis所在的位置增加一个一维
print(a[:,np.newaxis])
# [[1]
# [1]
# [1]]
a = a[:,np.newaxis]
b = b[:,np.newaxis]
c = np.concatenate((a,b,b,a),axis=1) # axis=0 上下方向合并 axis=1 左右方向进行合并
print(c)
# [[1 2 2 1]
# [1 2 2 1]
# [1 2 2 1]]
不能进行不等的分割!!
import numpy as np
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.split(a,3,axis=0)) # 指定分割成几块,指定分割的方向
# [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
如果要进行不等的分割
import numpy as np
a = np.arange(12).reshape(3,4)
# print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# 不等分割
print(np.array_split(a,2,axis=0))
# [array([[0, 1, 2, 3],
# [4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
利用vsplit和hsplit进行分割
import numpy as np
a = np.arange(12).reshape(3,4)
# print(a)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(np.vsplit(a,3))
# [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
print(np.hsplit(a,2))
# [array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]
import numpy as np
a = np.arange(4)
print(a)
# [0 1 2 3]
b =a
c = a
d = b
print(b is a) # True
a[0] = 5
print(a) # [5 1 2 3]
print(b) # [5 1 2 3]
print(d) # [5 1 2 3]
d[1:3] = [22,33]
print(a) # [ 5 22 33 3]
print(d) # [ 5 22 33 3]
# 如果我们想 把a赋值给b 但是我们不想让他们关联起来
# deep copy
b = a.copy()
print(b) # [ 5 22 33 3]
a[3] = 33
print(a) # [ 5 22 33 33]
print(b) # [ 5 22 33 3] a的值改变了 但是 b 的值并没有发生变化