ValueError:X 有 302 个特征,但 RandomForestClassifier 期望 428 个特征作为输入

问题描述 投票:0回答:0

从 sklearn.ensemble 导入 RandomForestClassifier

#我更改了数据集路径 dataset_df = pd.read_csv(r"C:\dataset ndroid_dataset-v2.csv")

Y = dataset_df['类'] X = dataset_df.drop(['class'], axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)

#定义参数范围 param_grid = {'n_estimators': [100, 200, 300, 500, 800, 1000], 'max_depth': [10, 20, 30, 50, 无], 'max_features':[X.shape[1]]}

grid = GridSearchCV(RandomForestClassifier(), param_grid, refit=True, verbose=3)

#fitting模型进行网格搜索 grid.fit(X_train, y_train)

打印(grid.best_params_) grid_predictions = grid.predict(X_test)

#打印分类报告 打印(分类报告(y_test,grid_predictions))

#我改变了路径 pickle.dump(grid," pp\static\models\RandomForestNew.pkl", 'wb'))

我尝试更改以下代码:

#定义参数范围 param_grid = {'n_estimators': [100, 200, 300, 500, 800, 1000], 'max_depth': [10, 20, 30, 50, 无], 'max_features':[X.shape[1]]}

对此:

#定义参数范围 param_grid = {'n_estimators': [100, 200, 300, 500, 800, 1000], 'max_depth': [10, 20, 30, 50, 无], 'max_features':[302]}

甚至这个:

#定义参数范围 param_grid = {'n_estimators': [100, 200, 300, 500, 800, 1000], 'max_depth': [10, 20, 30, 50, 无], 'max_features':[428]}

python android jupyter-notebook random-forest
© www.soinside.com 2019 - 2024. All rights reserved.