import pandas as pd import numpy as np import math import matplotlib.pyplot as plt…
Private research annotation
Sign in to add a private research label or note.
- User
import pandas as pd import numpy as np import math import matplotlib.pyplot as plt import xgboost as xgb from sklearn.model_selection import train_test_split
选择特征和目标变量
train_data = pd.read_csv('weibo_train_data.txt', header=None, sep='\t') train_frame = pd.DataFrame(train_data) train_frame.columns = ["uid", "mid", "date", "forward", "comment", "like", "content"] target_train = train_frame[['uid', "forward", "comment", "like"]] n_rows = len(target_train) print(len(target_train))
划分数据集
split_index = n_rows//2 X_train = target_train.iloc[:split_index] y_train = target_train.iloc[split_index:2*(split_index)]
拆分DataFrame
X_train = X_train[["forward", "comment", "like"]] y_train = y_train[["forward", "comment", "like"]]
y_train = y_train.head(1000) X_train = X_train.head(1000)
y_train['forward'] = y_train['forward'].astype(int) y_train['comment'] = y_train['comment'].astype(int) y_train['like'] = y_train['like'].astype(int)
X_train['forward'] = X_train['forward'].astype(int) X_train['comment'] = X_train['comment'].astype(int) X_train['like'] = X_train['like'].astype(int)
训练XGBoost模型
model = xgb.XGBoostRegressor() model.fit(X_train,y_train)
y_pred = xgb_model.predict(X_train) y_pred = y_pred.round()
mse_per_output = [] for i in range(y_pred.shape[1]): mse = mean_squared_error(y_train[:, i], y_pred[:, i]) mse_per_output.append(mse) print(f"Mean Squared Error for output {i + 1}: {mse}")
average_mse = np.mean(mse_per_output) print(f"Average Mean Squared Error: {average_mse}")
print("True Values:", y_train[:10]) print("Predicted Values:", y_pred[:10]) Traceback (most recent call last): File "d:\jiqixuexi\xgboost4(1)", line 39, in model.fit(X_train,y_train) File "d:\jiqixuexi\xgboost.py", line 22, in fit tree = self.build_tree(X, gradient, depth=0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "d:\jiqixuexi\xgboost.py", line 46, in build_tree split_index, split_value = self.find_best_split(X, gradient) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "d:\jiqixuexi\xgboost.py", line 70, in find_best_split feature_values = X[:, feature_index] ~^^^^^^^^^^^^^^^^^^ File "E:\Anaconda\envs\test\Lib\site-packages\pandas\core\frame.py", line 3893, in getitem indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Anaconda\envs\test\Lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc self._check_indexing_error(key) File "E:\Anaconda\envs\test\Lib\site-packages\pandas\core\indexes\base.py", line 5975, in _check_indexing_error
raise InvalidIndexError(key) pandas.errors.InvalidIndexError: (slice(None, None, None), 0) Assistant
The error message "pandas.errors.InvalidIndexError: (slice(None, None, None), 0)" indicates that you're trying to access a column in your Pandas DataFrame using an invalid index. This usually happens when:
-
Column Name Mismatch: The column name you're using to access the data doesn't exist in your DataFrame.
-
Incorrect Indexing: You're using an incorrect indexing method, such as trying to access a column by its numerical index when it should be accessed by its name.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.