numpy拼接矩陣的實現(xiàn)
1、文檔
使用numpy的 concatenate 拼接矩陣,文檔里面這樣解釋:
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
(a1, a2, ...):連接的數(shù)組必須有一樣的維度;
axis:拼接的方向;
out:預(yù)設(shè)輸出矩陣的大小
…………
2、舉例
首先給定兩個矩陣:
rotation = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) trans = np.array([[7], [8], [0]])
①:在第一個矩陣后面加上第二個矩陣(加一列):
z = np.concatenate((rotation, trans), axis=1)
輸出為:
[[1 2 3 7]
[4 5 6 8]
[7 8 9 0]]
②:以及在矩陣下面加一個全零行:
add_arr = np.array([[0, 0, 0, 0]]) array_by_add = np.concatenate((z, add_arr), axis=0)
輸出:
[[1 2 3 7]
[4 5 6 8]
[7 8 9 0]
[0 0 0 0]]
③:把矩陣填充為對角矩陣:
第一個為3×3矩陣,第二個為2×3矩陣。
arr1 = np.array([[2, 3, 4], [3, 4, 5], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [8, 9, 10]]) arr_zeros1 = np.zeros((arr2.shape[1], arr1.shape[0])) print(arr_zeros1) arr_zeros2 = np.zeros((arr2.shape[0], arr1.shape[1])) print(arr_zeros2) total_arr1 = np.concatenate((arr1, arr_zeros1), axis=1) total_arr2 = np.concatenate((arr_zeros2, arr2,), axis=1) total_arr = np.concatenate((total_arr1, total_arr2), axis=0) print(total_arr)
拼接之后結(jié)果如下:
[5 6]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[0. 0. 0.]]
[[ 2. 3. 4. 0. 0. 0.]
[ 3. 4. 5. 0. 0. 0.]
[ 4. 5. 6. 0. 0. 0.]
[ 0. 0. 0. 7. 8. 9.]
[ 0. 0. 0. 8. 9. 10.]]
官方文檔地址:numpy官網(wǎng)地址
到此這篇關(guān)于numpy拼接矩陣的實現(xiàn)的文章就介紹到這了,更多相關(guān)numpy 拼接矩陣內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python學(xué)習(xí)筆記之pandas索引列、過濾、分組、求和功能示例
這篇文章主要介紹了Python學(xué)習(xí)筆記之pandas索引列、過濾、分組、求和功能,結(jié)合實例形式分析了Python針對抓取保存的csv數(shù)據(jù)使用pandas進行索引列、過濾、分組、求和等操作的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-06-06python中如何用time方法生成當(dāng)前時間年月日時分秒
這篇文章主要給大家介紹了關(guān)于python中如何用time方法生成當(dāng)前時間年月日時分秒的相關(guān)資料,在Python中與時間處理有關(guān)的模塊就包括:time,datetime以及calendar,Time模塊用以取得系統(tǒng)時間相關(guān)的信息和時間的格式化等操作,需要的朋友可以參考下2023-08-08python實現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)詳解
bitmap是很常用的數(shù)據(jù)結(jié)構(gòu),比如用于Bloom Filter中,下面是使用python實現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)的代碼講解,需要的朋友可以參考下2014-02-02python中的json數(shù)據(jù)和pyecharts模塊入門示例教程
JSON是一種輕量級的數(shù)據(jù)交互格式??梢园凑?JSON指定的格式去組織和封裝數(shù)據(jù),這篇文章主要介紹了python中的json數(shù)據(jù)和pyecharts模塊入門,需要的朋友可以參考下2022-12-12Python實現(xiàn)列表中非負數(shù)保留,負數(shù)轉(zhuǎn)化為指定的數(shù)值方式
這篇文章主要介紹了Python實現(xiàn)列表中非負數(shù)保留,負數(shù)轉(zhuǎn)化為指定的數(shù)值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06