亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

修復了 one-hot 編碼中的冗余,但在嘗試反轉時仍然出現錯誤

修復了 one-hot 編碼中的冗余,但在嘗試反轉時仍然出現錯誤

慕勒3428872 2023-09-19 14:54:35
我正在進行 one-hot 編碼并使用 ??? =((??????)^?1) * ?????? 來估計 theta。由于冗余,我收到了錯誤,因此我決定刪除有冗余的列。這是在刪除列之前:這是我的代碼,因為我嘗試刪除具有冗余的列: def one_hot_encode_revised(data):                all_columns = data.columns            records = data[all_columns].to_dict(orient='records')        encoder = DictVectorizer(sparse=False)        encoded_X = encoder.fit_transform(records)        df = pd.DataFrame(data=encoded_X, columns=encoder.feature_names_)                return df.drop(['day=Fri', 'sex=Male', 'smoker=No', 'time=Dinner'], axis =1)one_hot_X_revised = one_hot_encode_revised(X)輸出如下:然后,我使用這個函數根據上面的方程估計 theta:def get_analytical_sol(X, y):"""Computes the analytical solution to our least squares problemParameters-----------X: a 2D dataframe of numeric features (one-hot encoded)y: a 1D vector of tip amountsReturns-----------The estimate for theta"""return np.linalg.inv(X.T * X) * (X.T * y)運行這個:revised_analytical_thetas = get_analytical_sol(one_hot_X_revised, tips)我的錯誤是: ValueError: 無法強制轉換為 DataFrame,形狀必須是 (8, 244): 給定 (252, 252)作為參考,提示是這樣的:我是否正確地消除了冗余,如果是,為什么仍然有錯誤?
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

您在這一行中有一個錯誤return np.linalg.inv(X.T * X) * (X.T * y)。你想要做的是矩陣乘法。在 pandas 數據框中,符號*不用于矩陣乘法。您需要使用@dot()數據框的方法。



查看完整回答
反對 回復 2023-09-19
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號