一維數(shù)組
1.numpy初始化一維數(shù)組
1
2
|
a = np.array([ 1 , 2 , 3 ]); print a.shape |
輸出的值應(yīng)該為(3,)
2.numpy初始化二維數(shù)組
1
2
3
4
5
|
a = np.array([[ 1 , 2 , 3 ]]); b = np.array([[ 1 ],[ 2 ],[ 3 ]]); print a.shape / / ( 1 , 3 ) print b.shape / / ( 3 , 1 ) |
注意(3,)和(3,1)的數(shù)組是不一樣的,前者是一維數(shù)組,后者是二維數(shù)組。
拼接
3.numpy有很多的拼接函數(shù)。比如hstack和vstack等。網(wǎng)上又很多這樣的總結(jié)帖子。但是兩個數(shù)組能拼接的條件就是得滿足兩個數(shù)組的維度要相同。所以二維數(shù)組和一維數(shù)組拼接的時候需要使用newaxis將一維數(shù)組轉(zhuǎn)化為二維數(shù)組,也就是shape從(3,)轉(zhuǎn)化為(3,1)。
1
2
3
4
5
6
|
a = np.array([ 1 , 2 , 3 ]); b = np.array([[ 1 ],[ 2 ],[ 3 ]]); #將一維數(shù)組a轉(zhuǎn)化為二維數(shù)組 a = a[:,np.newaxis]; c = np.concatenate((b,a),axis = 1 ) print c.shape / / 輸出為( 3 , 2 ) |
總結(jié)
以上就是本文關(guān)于利用numpy實現(xiàn)一、二維數(shù)組的拼接簡單代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/selous/article/details/72650426