這是一個我稱之為 this_col = [18 18 18 ... 24 24 24] 的 numpy 陣列
我試圖以多種方式重塑我的資料
print("yo")
print(this_col.shape)
try:
min_max_scaler = preprocessing.MinMaxScaler()
print(this_col)
this_col = pd.Series(min_max_scaler.fit_transform(this_col))
except Exception as e:
print("the exception ")
print(e)
try:
print("no og ")
this_col = this_col.reshape(-1, 1)
print(this_col)
min_max_scaler = preprocessing.MinMaxScaler()
this_col = pd.Series(min_max_scaler.fit_transform(this_col))
except Exception as e:
print("the exception ")
print(e)
try:
print("no .reshape(-1, 1) ")
this_col = this_col.reshape(1, -1)
print(this_col)
min_max_scaler = preprocessing.MinMaxScaler()
this_col = pd.Series(min_max_scaler.fit_transform(this_col))
except Exception as e:
print("the exception ")
print(e)
print("no .reshape(1, -1) ")
print(9/0)
下面是我從這段代碼中收到的輸出
yo
(34144,)
[18 18 18 ... 24 24 24]
the exception
Expected 2D array, got 1D array instead:
array=[18. 18. 18. ... 24. 24. 24.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
no og
[[18]
[18]
[18]
...
[24]
[24]
[24]]
the exception
Data must be 1-dimensional
no .reshape(-1, 1)
[[18 18 18 ... 24 24 24]]
the exception
Data must be 1-dimensional
no .reshape(1, -1)
ZeroDivisionError: division by zero
這 3 種安排中的 1 種肯定奏效了!!!!>:(
更新:我重做了上面的例子,包括形狀和例外訊息
再次更新:我開始懷疑問題可能出在 min_max 函式上。第一個錯誤狀態“預期二維陣列,得到一維陣列”,然后第二個和第三個錯誤狀態“資料必須是一維的”。它想要什么?
uj5u.com熱心網友回復:
您可以使用 flatten() 方法:
a = np.array([[1,2,3,1],[8,9,4,1],[7,6,5,1],[7,6,5,1]])
print(a.shape) # gives (4,4)
print(a.flatten().shape) # gives(16,)
編輯:您可以閱讀有關檔案的更多資訊
uj5u.com熱心網友回復:
您的錯誤訊息來自兩個不同的地方。
一方面,錯誤訊息“Expected 2D array, got 1D array instead”來自將 1D 陣列傳遞給min_max_scaler.fit_transform()何時需要 2D 陣列。
另一方面,錯誤訊息“資料必須是一維的”來自于在pd.Series()需要一維陣列時將二維陣列傳遞給建構式。
運算式pd.Series(min_max_scaler.fit_transform(this_col))總是會失敗,因為如果this_col是 1D,那么min_max_scaler.fit_transform()將失敗,如果this_col是 2D,那么 的輸出min_max_scaler.fit_transform()也將是 2D,并且pd.Series()不能接受該輸出。
你可能想做這樣的事情:
this_col_transformed = min_max_scaler.fit_transform(this_col.reshape(-1, 1))
this_col_series = pd.Series(this_col_transformed.ravel())
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/426274.html
