我有这些数据:
print training_data
print labels
# prints
[[1, 0, 1, 1], [1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 0], [1, 1, 0, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 0,0], [1, 1, 1, 1], [1, 0, 1, 1]]
['a', 'b', 'a', 'b', 'a', 'b', 'b', 'a', 'a', 'a', 'b']
我正在尝试将它从sklearn python库提供给RandomForestClassifier。
classifier = RandomForestClassifier(n_estimators=10)
classifier.fit(training_data, labels)
但收到此错误:
Traceback (most recent call last):
File "learn.py", line 52, in <module>
main()
File "learn.py", line 48, in main
classifier = train_classifier()
File "learn.py", line 33, in train_classifier
classifier.fit(training_data, labels)
File "/Library/Python/2.7/site-packages/scikit_learn-0.14_git-py2.7-macosx-10.8-intel.egg/sklearn/ensemble/forest.py", line 348, in fit
y = np.ascontiguousarray(y, dtype=DOUBLE)
File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_bbcfcf6_20130307-py2.7-macosx-10.8-intel.egg/numpy/core/numeric.py", line 419, in ascontiguousarray
return array(a, dtype, copy=False, order='C', ndmin=1)
ValueError: could not convert string to float: a
我的猜测是我没有正确格式化这些数据以进行拟合。但我不明白为什么来自the documentation
这似乎是一个非常基本的简单问题。有人知道答案吗?
答案 0 :(得分:7)
尝试使用LabelEncoder预先转换标签。
答案 1 :(得分:0)
您可以使用分类器自动识别的numpy数组,如下所示:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.apache.poi.xslf.model.geom.AddSubtractExpression;
public class a {
private static FileWriter fw;
private static FileReader fr;
private static BufferedWriter bw;
private static BufferedReader br;
static File file;
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
fw = new FileWriter(file); //dont put filewriter or buffedwriter before these, because already declared and end up
bw = new BufferedWriter(fw); //making null expection thing
fr = new FileReader(file);
br = new BufferedReader(fr);
}catch(Exception ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
public static void addStuff(String toAdd){
String line = "";
try {
bw.write(toAdd);
//bw.newLine();
System.out.println(toAdd + " added");
//line = br.readLine();
while((line = br.readLine()) != null) {
System.out.println(line + "r");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error at addstuff");
}
}
public static void main(String... args){
openFile();
int i=0;
while(true)
addStuff("hello" + i++);
}
}
那应该有用