我有一组48个特征列和一个二进制分类目标。在处理分类问题时,我能够在使用一个热编码或类似编码进行分类到数值转换后加载所有算法,如线性、逻辑、knn、随机森林和boosting分类器。但是,在运行诸如随机森林和决策树之类的算法时,如果没有从分类到数值的任何转换,我将面临“ValueError:无法将字符串转换为浮点…”的错误
我正在尝试一个没有任何变化的基础模型,请指导。
print(type(X)) ---> <class 'pandas.core.frame.DataFrame'>
print(type(y)) ---- > <class 'pandas.core.series.Series'>
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
X_train_rf, X_test_rf, y_train_rf, y_test_rf = train_test_split(X,y,random_state=0)
randomforest = RandomForestClassifier()
randomforest.fit(X_train_rf, y_train_rf)
y_train_pred_rf=randomforest.predict(X_train_rf)
y_pred_rf= randomforest.predict(X_test_rf)
print('training accuracy',accuracy_score(y_train_rf,y_train_pred_rf))
print('test accuracy',accuracy_score(y_test_rf,y_pred_rf))
# The o/p obtained is :
ValueError: could not convert string to float: 'Delhi' (# Delhi- the element in an feature column )
您可以使用pythonweka包装器,这样就不需要一个热编码。例子:
import weka.core.jvm as jvm
from weka.core.converters import Loader
from weka.classifiers import Classifier
def get_weka_prob(inst):
dist = c.distribution_for_instance(inst)
p = dist[next((i for i, x in enumerate(inst.class_attribute.values) if x == 'DONE'), -1)]
return p
jvm.start()
loader = Loader(classname="weka.core.converters.CSVLoader")
data = loader.load_file(r'.\recs_csv\df.csv')
data.class_is_last()
datatst = loader.load_file(r'.\recs_csv\dftst.csv')
datatst.class_is_last()
c = Classifier("weka.classifiers.trees.J48", options=["-C", "0.1"])
c.build_classifier(data)
print(c)
probstst = [get_weka_prob(inst) for inst in datatst]
jvm.stop()