1、 矩陣轉(zhuǎn)向量:ravel()和flatten()函數(shù)可以實現(xiàn)多維數(shù)據(jù)向一維數(shù)據(jù)轉(zhuǎn)換
import numpy as np
x = np.arange(9).reshape(3,3)
print(type(x),x.shape)
print(x)
x = x.reshape(1,9)
print(type(x),x.shape)
print(x)
a = x.ravel()
print(type(a),a.shape)
print(a)
b = x.flatten()
print(b)
a[1]=100
print(a)
b[1]=100
print(b)
..............................
<class 'numpy.ndarray'> (3, 3)
[[0 1 2]
[3 4 5]
[6 7 8]]
<class 'numpy.ndarray'> (1, 9)
[[0 1 2 3 4 5 6 7 8]]
<class 'numpy.ndarray'> (9,)
[0 1 2 3 4 5 6 7 8]
[0 1 2 3 4 5 6 7 8]
[ 0 100 2 3 4 5 6 7 8]
[ 0 100 2 3 4 5 6 7 8]
2、向量轉(zhuǎn)矩陣:reshape() 可以將一維數(shù)據(jù)轉(zhuǎn)為多維數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-567307.html
import numpy as np
x = np.arange(10)
print(type(x),x.shape)
print(x)
a = x.reshape(1,10)
print(type(a),a.shape)
print(a)
b = x.reshape(5,2)
p
文章來源:http://www.zghlxwxcb.cn/news/detail-567307.html
到了這里,關(guān)于python學(xué)習(xí)筆記——矩陣跟向量間的轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!