使用fit_transform進行資料轉換出現錯誤
2019/09/27 上午 07:55
機器學習共學討論版
herohsu
觀看數:29
回答數:2
收藏數:0
day23
我進行minMaxScalar(1,10), 將資料值限制在1~10, 但在輸入boxcox前不確定哪裡出錯了
會一直出現底下錯誤,請問是哪裡有問題?
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
# 將 Fare 取 boxcox 後, 看散佈圖, 並計算分數 (執行會有 error, 請試圖修正)
from scipy import stats
df_fixed = copy.deepcopy(df)
"""
Your Code Here, fix the error
"""
from sklearn.preprocessing import MinMaxScaler
scalar = MinMaxScaler( feature_range=(1, 10))
scalar.fit(df_fixed)
df_fixed = scalar.fit_transform(df_fixed)
df_fixed['Fare'] = stats.boxcox(df_fixed['Fare'])[0]
output=====
ndexError Traceback (most recent call last)
<ipython-input-47-da10ee54c24a> in <module>
10 df_fixed = scalar.fit_transform(df_fixed)
11
---> 12 df_fixed['Fare'] = stats.boxcox(df_fixed['Fare'])[0]
13 sns.distplot(df_fixed['Fare'][:train_num])
14 plt.show()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
回答列表
-
2019/09/27 下午 01:31陳明佑 (Ming You Chen)贊同數:0不贊同數:0留言數:0
使用 scaler 系列的方式轉換資料時, 我們需要把一件事牢記於心 :
"scaler 傳回的是 numpy.array"
一但是 np.array, 就不是 pd.DataFrame, 就沒有 column name了!!
因此在呼叫df_fixed['Fare']時就會錯 : 他已經是 array, 所以沒有 'Fare' 與其他的欄位名稱
要避免這樣, 就是寫迴圈一個個column分別轉換
-
2019/09/27 下午 03:10張維元 (WeiYuan)贊同數:1不贊同數:0留言數:2
所以問題其實是「df_fixed = scalar.fit_transform(df_fixed)」這一行造成的,只是在下一行存取時才發現錯誤。