接下来,[o, O1, O2, RMSE_Error] = F7(x),那么如何索引O1
时间: 2024-06-04 14:08:32 浏览: 15
如果你的程序语言中O1是一个数组或向量,那么可以使用索引操作符来访问它的元素。具体来说,如果O1是一个n元素的向量,那么可以使用O1[i]来访问第i个元素,其中i是一个介于1和n之间的整数。例如,如果你想要访问O1的第3个元素,你可以使用O1[3]。注意,有些编程语言中的数组索引是从0开始的,这意味着你需要使用O1[2]来访问O1的第3个元素。
相关问题
random_params = {'n_estimators': randint(low=80, high=121), 'max_features': randint(low=5, high=10)} forset_reg = RandomForestRegressor(random_state=2020) random_search_forest = RandomizedSearchCV(forest_reg, param_distributions=random_params, n_iter=20, scoring='neg_mean_squared_error', cv=15, random_state=2020) random_search_forest.fit(housing_prepares, housing_label) print('随机搜索下最佳参数', random_search_forest.best_params_) print('随机搜索下最佳评估器', random_search_forest.best_estimator_) final_model1 = random_search_forest.best_estimator_ y_predict = final_model1.predict(x_test) final_mse = mean_squared_error(y_test, y_predict) final_rmse = np.sqrt(final_mse) print(final_rmse) display(final_rmse) cvrus = random_search_forest.cv_results_ for mean_score, params in zip(cvrus['mean_test_score'], cvrus['params']): print(np.sqrt(-mean_score), params) 将以上代码绘图实现
很抱歉,我作为一个语言模型,无法直接绘图。但我可以提供一些思路,您可以使用 Matplotlib 或其他绘图库来实现。
根据您的代码,可以将随机搜索下不同参数组合的 RMSE 值绘制成折线图或柱状图,以便比较它们的效果。您可以使用 Matplotlib 库中的 plot() 函数或 bar() 函数来绘制图表。
首先,您需要将测试集的 RMSE 值与每个参数组合的 RMSE 值进行比较。您可以使用以下代码:
```
test_rmse = np.sqrt(mean_squared_error(y_test, y_predict))
cv_rmse = [np.sqrt(-x) for x in random_search_forest.cv_results_['mean_test_score']]
params = [str(x) for x in random_search_forest.cv_results_['params']]
cv_df = pd.DataFrame({'params': params, 'RMSE': cv_rmse})
cv_df = cv_df.sort_values(by='RMSE', ascending=True)
cv_df = cv_df.reset_index(drop=True)
cv_df.loc[len(cv_df)] = ['Test set', test_rmse]
```
然后,您可以使用以下代码将 RMSE 值绘制成折线图:
```
plt.plot(cv_df['params'], cv_df['RMSE'], label='CV RMSE')
plt.plot(cv_df['params'], cv_df[cv_df['params'] == 'Test set']['RMSE'], 'o', label='Test set')
plt.xticks(rotation=90)
plt.xlabel('Parameter combinations')
plt.ylabel('RMSE')
plt.legend()
plt.show()
```
或者,您可以使用以下代码将 RMSE 值绘制成柱状图:
```
plt.bar(cv_df['params'], cv_df['RMSE'])
plt.xticks(rotation=90)
plt.xlabel('Parameter combinations')
plt.ylabel('RMSE')
plt.show()
```
希望这些代码可以帮助您实现绘图。
修改和补充下列代码得到十折交叉验证的平均每一折auc值和平均每一折aoc曲线,平均每一折分类报告以及平均每一折混淆矩阵 min_max_scaler = MinMaxScaler() X_train1, X_test1 = x[train_id], x[test_id] y_train1, y_test1 = y[train_id], y[test_id] # apply the same scaler to both sets of data X_train1 = min_max_scaler.fit_transform(X_train1) X_test1 = min_max_scaler.transform(X_test1) X_train1 = np.array(X_train1) X_test1 = np.array(X_test1) config = get_config() tree = gcForest(config) tree.fit(X_train1, y_train1) y_pred11 = tree.predict(X_test1) y_pred1.append(y_pred11 X_train.append(X_train1) X_test.append(X_test1) y_test.append(y_test1) y_train.append(y_train1) X_train_fuzzy1, X_test_fuzzy1 = X_fuzzy[train_id], X_fuzzy[test_id] y_train_fuzzy1, y_test_fuzzy1 = y_sampled[train_id], y_sampled[test_id] X_train_fuzzy1 = min_max_scaler.fit_transform(X_train_fuzzy1) X_test_fuzzy1 = min_max_scaler.transform(X_test_fuzzy1) X_train_fuzzy1 = np.array(X_train_fuzzy1) X_test_fuzzy1 = np.array(X_test_fuzzy1) config = get_config() tree = gcForest(config) tree.fit(X_train_fuzzy1, y_train_fuzzy1) y_predd = tree.predict(X_test_fuzzy1) y_pred.append(y_predd) X_test_fuzzy.append(X_test_fuzzy1) y_test_fuzzy.append(y_test_fuzzy1)y_pred = to_categorical(np.concatenate(y_pred), num_classes=3) y_pred1 = to_categorical(np.concatenate(y_pred1), num_classes=3) y_test = to_categorical(np.concatenate(y_test), num_classes=3) y_test_fuzzy = to_categorical(np.concatenate(y_test_fuzzy), num_classes=3) print(y_pred.shape) print(y_pred1.shape) print(y_test.shape) print(y_test_fuzzy.shape) # 深度森林 report1 = classification_report(y_test, y_prprint("DF",report1) report = classification_report(y_test_fuzzy, y_pred) print("DF-F",report) mse = mean_squared_error(y_test, y_pred1) rmse = math.sqrt(mse) print('深度森林RMSE:', rmse) print('深度森林Accuracy:', accuracy_score(y_test, y_pred1)) mse = mean_squared_error(y_test_fuzzy, y_pred) rmse = math.sqrt(mse) print('F深度森林RMSE:', rmse) print('F深度森林Accuracy:', accuracy_score(y_test_fuzzy, y_pred)) mse = mean_squared_error(y_test, y_pred) rmse = math.sqrt(mse)
首先,需要将代码放入循环中进行十折交叉验证。每一折都需要记录相应的分类报告、混淆矩阵、auc值和aoc曲线。以下是修改后的代码:
```
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve, auc
from sklearn.model_selection import StratifiedKFold
from gcforest.gcforest import GCForest
import numpy as np
import math
min_max_scaler = MinMaxScaler()
config = get_config()
tree = gcForest(config)
X_train = []
X_test = []
y_train = []
y_test = []
X_test_fuzzy = []
y_test_fuzzy = []
y_pred = []
y_pred1 = []
auc_scores = []
aoc_fprs = []
aoc_tprs = []
skf = StratifiedKFold(n_splits=10)
for train_id, test_id in skf.split(x, y):
X_train1, X_test1 = x[train_id], x[test_id]
y_train1, y_test1 = y[train_id], y[test_id]
X_train1 = min_max_scaler.fit_transform(X_train1)
X_test1 = min_max_scaler.transform(X_test1)
X_train1 = np.array(X_train1)
X_test1 = np.array(X_test1)
tree.fit(X_train1, y_train1)
y_pred11 = tree.predict(X_test1)
y_pred1.append(y_pred11)
X_train.append(X_train1)
X_test.append(X_test1)
y_test.append(y_test1)
y_train.append(y_train1)
X_train_fuzzy1, X_test_fuzzy1 = X_fuzzy[train_id], X_fuzzy[test_id]
y_train_fuzzy1, y_test_fuzzy1 = y_sampled[train_id], y_sampled[test_id]
X_train_fuzzy1 = min_max_scaler.fit_transform(X_train_fuzzy1)
X_test_fuzzy1 = min_max_scaler.transform(X_test_fuzzy1)
X_train_fuzzy1 = np.array(X_train_fuzzy1)
X_test_fuzzy1 = np.array(X_test_fuzzy1)
tree.fit(X_train_fuzzy1, y_train_fuzzy1)
y_predd = tree.predict(X_test_fuzzy1)
y_pred.append(y_predd)
X_test_fuzzy.append(X_test_fuzzy1)
y_test_fuzzy.append(y_test_fuzzy1)
y_pred_proba = tree.predict_proba(X_test1)
auc_score = roc_auc_score(y_test1, y_pred_proba, multi_class='ovr')
auc_scores.append(auc_score)
fpr, tpr, _ = roc_curve(y_test1, y_pred_proba[:, 1])
aoc_fprs.append(fpr)
aoc_tprs.append(tpr)
y_pred = to_categorical(np.concatenate(y_pred), num_classes=3)
y_pred1 = to_categorical(np.concatenate(y_pred1), num_classes=3)
y_test = to_categorical(np.concatenate(y_test), num_classes=3)
y_test_fuzzy = to_categorical(np.concatenate(y_test_fuzzy), num_classes=3)
print(y_pred.shape)
print(y_pred1.shape)
print(y_test.shape)
print(y_test_fuzzy.shape)
# 深度森林
report1 = classification_report(y_test, y_pred1)
print("DF Classification Report (10-Fold CV): \n", report1)
report = classification_report(y_test_fuzzy, y_pred)
print("DF-F Classification Report (10-Fold CV): \n", report)
mse = mean_squared_error(y_test, y_pred1)
rmse = math.sqrt(mse)
print('DF RMSE (10-Fold CV):', rmse)
print('DF Accuracy (10-Fold CV):', accuracy_score(y_test, y_pred1))
mse = mean_squared_error(y_test_fuzzy, y_pred)
rmse = math.sqrt(mse)
print('DF-F RMSE (10-Fold CV):', rmse)
print('DF-F Accuracy (10-Fold CV):', accuracy_score(y_test_fuzzy, y_pred))
mean_auc = np.mean(auc_scores)
print("Mean AUC (10-Fold CV): ", mean_auc)
mean_fpr = np.mean(aoc_fprs, axis=0)
mean_tpr = np.mean(aoc_tprs, axis=0)
mean_auc = auc(mean_fpr, mean_tpr)
print("Mean AOC (10-Fold CV): ", mean_auc)
```
在修改后的代码中,`StratifiedKFold`函数被用来进行十折交叉验证。每一折的训练数据和测试数据都是通过`train_id`和`test_id`来确定的。在每一折的训练和测试之后,需要记录相应的分类报告、混淆矩阵、auc值和aoc曲线。最后,需要计算平均每一折的auc值和aoc曲线。