numpy將多維陣列降維成一維

2022-02-25 10:59:29 字數 2158 閱讀 4344

可以用reshape方法,但是感覺flatten方法更好

pridict_y

[[14.394563]

[ 4.5585423]

[10.817445]

[12.291978]

[26.076233]

[20.033213]

[11.320534]

[14.528755]

[11.454205]

[ 9.153889]

[12.769189]

[ 5.7419834]

[25.451023]

[18.215645]

[21.743513]

[ 8.488817]

[17.128687]

[17.53172]

[ 4.953989]

[11.3504]

[ 7.5612407]

[ 4.2715034]

[20.316795]

[17.732632]

[ 4.2850647]

[ 6.971166]

[11.657596]

[24.968727]

[13.93272]]

pridict_y.reshape(29,)

和pridict_y.flatten()

結果都是

array([14.394563 , 4.5585423, 10.817445 , 12.291978 , 26.076233,

20.033213 , 11.320534 , 14.528755 , 11.454205 , 9.153889,

12.769189 , 5.7419834, 25.451023 , 18.215645 , 21.743513,

8.488817 , 17.128687 , 17.53172 , 4.953989 , 11.3504,

7.5612407, 4.2715034, 20.316795 , 17.732632 , 4.2850647,

6.971166 , 11.657596 , 24.968727 , 13.93272 ], dtype=float32)

**或參考:【python】numpy庫ndarray多維陣列的維度變換方法:reshape、resize、swapaxes、flatten等詳解與例項

numpy庫對多維陣列有非常靈巧的處理方式,主要的處理方法有:

in [22]: a = np.arange(20)

#原陣列不變

in [23]: a.reshape([4,5])

out[23]:

array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14],

[15, 16, 17, 18, 19]])

in [24]: a

out[24]:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19])

#修改原陣列

in [25]: a.resize([4,5])

in [26]: a

out[26]:

array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14],

[15, 16, 17, 18, 19]])

in [27]: a.swapaxes(1,0)

out[27]:

array([[ 0, 5, 10, 15],

[ 1, 6, 11, 16],

[ 2, 7, 12, 17],

[ 3, 8, 13, 18],

[ 4, 9, 14, 19]])

in [29]: a.flatten()

out[29]:

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19])

js將多維陣列轉換為一維陣列

解決方案有很多,以下來一一例舉 方法一 使用陣列的join let arr 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 console.log arr.join 輸出為 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 let newar...

資料降維 多維縮放MDS

多維縮放 multidimensional scaling,mds 是一組物件之間的距離的視覺化表示,也可以當做一種無監督降維演算法使用。為了直觀了解mds,給乙個簡單例子。假設現在給定一組城市之間的距離資訊如下 現在要求繪製一幅地圖,在地圖中標出所有城市,並且城市之間的距離等於上表中給出的距離。顯...

多維陣列變一維陣列

判斷是否是陣列 let isarr arr arr instanceof array 判斷是否一維陣列 let istdim arr arr.reduce o1,o2 o1 isarr o2 true 多維陣列變一維陣列function name return a console.log name ...