Python 值:使用Sklearn功能相关性时,错误无法将字符串转换为浮点

Python 值:使用Sklearn功能相关性时,错误无法将字符串转换为浮点,python,scikit-learn,ipython,feature-selection,Python,Scikit Learn,Ipython,Feature Selection,您好,我已经培训和测试了数据。我试图使用sklearn的功能相关性Seelct K Best来选择相关功能,并在之后绘制条形图。但是我得到了这个错误: ValueError: could not convert string to float: B 但我开始认为,我的数据集中确实有这样一列,这可能就是问题所在: CancellationCode: A B C D 如果此列导致问题,如何解决此错误 下面是我的代码: import numpy as np from sklearn.feature

您好,我已经培训和测试了数据。我试图使用sklearn的功能相关性Seelct K Best来选择相关功能,并在之后绘制条形图。但是我得到了这个错误:

ValueError: could not convert string to float: B
但我开始认为,我的数据集中确实有这样一列,这可能就是问题所在:

CancellationCode:
A
B
C
D
如果此列导致问题,如何解决此错误 下面是我的代码:

import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
import matplotlib.pyplot as plt

selector = SelectKBest(f_classif, k=13)
selector.fit(X_train, y_train)

scores_select = selector.pvalues_
print scores_select


# Plotting the bar Graph to visually see the weight of each feature
plt.bar(range(len(scores_select)), scores_select, align='center')
plt.xticks(range(len(features_columns)), features_columns, rotation='vertical')
plt.show()

您需要将分类变量转换为虚拟变量

 df = pd.get_dummies(df)

您需要将分类变量转换为虚拟变量

 df = pd.get_dummies(df)