我正在尝试使用 uci 心脏病数据集来预测心脏病。我正在应用前向特征选择技术来选择特征,然后应用 knn 进行预测。附上代码片段。
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
selected_features = set()
# Initialize a KNN classifier
knn_classifier = KNeighborsClassifier(n_neighbors=5)
# Number of features to select (you can choose based on your requirements)
num_features_to_select = 10
# Perform forward feature selection
for _ in range(num_features_to_select):
best_accuracy = 0.0
best_feature = None
# Iterate over remaining features
for feature_index in range(X_train.shape[1]):
if feature_index not in selected_features:
# Add the feature and train the KNN classifier
current_features = list(selected_features) + [feature_index]
knn_classifier.fit(X_train[:, current_features], y_train)
# Predict using the trained KNN classifier
y_pred = knn_classifier.predict(X_test[:, current_features])
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
# Update the best feature and accuracy
if accuracy > best_accuracy:
best_accuracy = accuracy
best_feature = feature_index
# Add the best feature to the selected features
selected_features.add(best_feature)
# Print the selected feature and its accuracy
print(f"Selected Feature: {best_feature}, Accuracy: {best_accuracy}")
# Train the KNN classifier with the selected features
knn_classifier.fit(X_train[:, list(selected_features)], y_train)
# Predict using the trained KNN classifier with the selected features
y_pred = knn_classifier.predict(X_test[:, list(selected_features)])
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Final Accuracy:", accuracy)
但是我收到错误为
InvalidIndexError: (slice(None, None, None), [0])
我做错了什么?
# Iterate over remaining features
for feature_index in range(X_train.shape[1]):
if feature_index not in selected_features:
# Add the feature and train the KNN classifier
current_features = list(selected_features) + [feature_index]
knn_classifier.fit(X_train[:, current_features], y_train)
在此 for 循环中,feature_index 获取的第一个值是 0。您不是迭代列名,而是迭代一个范围。所以,当你检查
if feature_index not in selected_features
时,它没有意义。请检查这部分。