我正在進行 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()
數據框的方法。
添加回答
舉報
0/150
提交
取消