日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

numpy.reshape(-1,1)的具體使用_python

作者:Pikachu_simple ? 更新時間: 2022-09-16 編程語言

數組新的shape屬性應該要與原來的配套,如果等于-1的話,那么Numpy會根據剩下的維度計算出數組的另外一個shape屬性值。

舉個例子:

x = np.array([[2, 0], [1, 1], [2, 3]])

指定新數組行為3,列為,2,則:

y = x.reshape(3,2)
 
y
Out[43]: 
array([[2, 0],
       [1, 1],
       [2, 3]])

指定新數組列為1,則:

y = x.reshape(-1,1)
 
y
Out[34]: 
array([[2],
       [0],
       [1],
       [1],
       [2],
       [3]])

指定新數組列為2,則:

y = x.reshape(-1,2)
 
y
Out[37]: 
array([[2, 0],
       [1, 1],
       [2, 3]])

指定新數組行為1,則:

y = x.reshape(1,-1)
 
y
Out[39]: array([[2, 0, 1, 1, 2, 3]])

指定新數組行為2,則:

y = x.reshape(2,-1)
 
y
Out[41]: 
array([[2, 0, 1],
       [1, 2, 3]])

numpy中reshape(-1,1)與reshape(1,-1)的作用

如果你的數據只有一個特征,可以用reshape(-1,1)改變你的數據形狀;或者如果你的數據只包含一個樣本,可以使用reshape(1,-1)來改變。

e = np.array([1]) #只包含一個數據
f = e.reshape(1,-1) #改變形狀,輸出f之后發現它已經變成了二維數據
import numpy as np
a = np.array([[1,2,3],[4,5,6]]) #是兩行三列的數據,二維
b = np.array([1,2])    #是一維數據
c = b.reshape(-1,1)    #c已經變成了二維數據,變成了兩行一列
d = b.reshape(1,-1)    #d變成了一行兩列的數據,
print('b.shape is {0}'.format(b.shape))
print(b)
print('c.shape is {0}'.format(c.shape))
print(c)
print('d.shape is {0},d array is {1}'.format(d.shape,d))

可以發現reshape(-1,1)是將一維數據在行上變化,而reshape(1,-1)是將一維數據在列上變化

原文鏈接:https://blog.csdn.net/qq_42804678/article/details/99062431

欄目分類
最近更新