转自:https://blog.csdn.net/lishuandao/article/details/52444288?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
给矩阵中添加行:
np.row_stack(mat, a)
a 是要加入的行序列
a = np.zeros((10,5), dtype=np.float32)
print("a", a)
b = a[[1,2]]
print("b", b)
c = np.zeros((10,5), dtype=np.float32)
print("c", c)
d = c[[3,4]]
print("d", d)
e = np.row_stack((b, d))
print("e", e)
注意里面两层括号!
结果:
https://blog.csdn.net/S09M03J/article/details/101604418?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158773418219725247612074%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=158773418219725247612074&biz_id=0&utm_source=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-3
给矩阵添加列:
np.column_stack(mat, a)
同理
查看矩阵行列号:
a[ [1,2,3],
[1,2,3],
[1,2,3] ]
print(a.shape)
打印:(3,3)
查看行数:print(len(a))
打印:3
查看维数:print(a.ndim)
打印: 2
reshape(3, 4) 变成3行4列
转自:https://blog.csdn.net/jacke121/article/details/81535135?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158773358019725247656295%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=158773358019725247656295&biz_id=0&utm_source=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-3
ValueError: operands could not be broadcast together with shapes解决
行列相乘或相加,行列数对不上,做不了计算
import numpy as np
a=np.array([[1,2]])
b=np.array([[1,5,3]])
print(a+b)
原因:a与b的shape不同,所以报错,解决方法。维度调成相同即可
转自:https://blog.csdn.net/lllxxq141592654/article/details/84260091?ops_request_misc=&request_id=&biz_id=102&utm_source=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1
矩阵的行列连接问题:
np.hstack()是把矩阵进行行连接。
np.vstack()是把矩阵进行列连接。
多维数组降为一维数组numpy.ravel()
import numpy as np #导入numpy,并重命名为np
x = np.array([[1, 2], #x为二维数组
[3, 4]])
print(np.ravel(x)) #将二维数组按行优先,变为一维数组
print(np.ravel(x,'F')) #将二维数组按列优先,变为一维数组
结果:
[1 2 3 4] #按行优先
[1 3 2 4] #按列优先