使用Weka分类作者博客性别

我试图用Java中的Weka将作者的博客分类为男性或女性。 我创build了一个名为Weka的类,它定义了要在训练集中使用的属性,然后调用一个方法从Excel表单中加载所有已知的数据。 文件中的数据是这样组织的:每行在单元格0中包含博客文本,然后在单元格1中包含M或F。

博客文字M更多文字F

我也跟着这个教程一点点Weka Java教程

当我运行程序时,我开始在eclipse中的控制台窗口中看到文本,但是突然间我得到一个红色的错误,说“值没有为给定的名义属性定义! 我不太清楚为什么会发生这种情况。 文本正在逐行改变,所以我认为不可能定义所有的名义属性。 任何人都可以看到我在做什么错误或愚蠢在这里? 我将不胜感激任何帮助。 我一直坚持了几个小时。

码:

public class Weka { static FastVector fvWekaAttributes; static Instances isTrainingSet; static Classifier cModel; public static void main(String[] args) throws Exception { // Declaring attributes Attribute stringAttribute = new Attribute("text", (FastVector) null); // Declaring a class attribute along with values FastVector fastVClassVal = new FastVector(2); fastVClassVal.addElement("M"); fastVClassVal.addElement("F"); Attribute classAttribute = new Attribute("theClass", fastVClassVal); // Declaring the feature vector fvWekaAttributes = new FastVector(2); fvWekaAttributes.addElement(stringAttribute); fvWekaAttributes.addElement(classAttribute); // create the training set isTrainingSet = new Instances("Rel", fvWekaAttributes, 10); // set class index isTrainingSet.setClassIndex(1); // create however many instances is in my excel file // and add it to the training set in a loop. Weka.LoadExcelWorkBook(isTrainingSet); Weka.TestSetWork(); } public static void TestSetWork() throws Exception { // test the model Evaluation testing = new Evaluation(isTrainingSet); testing.evaluateModel(cModel, isTrainingSet); // printing the results.... String strSummary = testing.toSummaryString(); System.out.println(strSummary); // get confusion matrix. double[][] cmMatrix = testing.confusionMatrix(); for (int i = 0; i < cmMatrix.length; i++) { for (int col = 0; col < cmMatrix.length; col++) { System.out.print(cmMatrix[i][col]); System.out.print("|"); } System.out.println(); } } public static void LoadExcelWorkBook(Instances trainingSet) throws Exception { System.out.println("LOADING EXCEL WORKBOOK!!!"); Workbook wb = null; // opening excel file. try { wb = WorkbookFactory .create(new File("C://blog-gender-dataset.xlsx")); } catch (IOException ieo) { ieo.printStackTrace(); } // opening worksheet. Sheet sheet = wb.getSheetAt(0); StringToWordVector filter = new StringToWordVector(); filter.setInputFormat(isTrainingSet); Instances dataFiltered = Filter.useFilter(isTrainingSet, filter); for (Row row : sheet) { Cell textCell = row.getCell(0); Cell MFCell = row.getCell(1); String blogText = textCell.getStringCellValue(); String MFIndicator = MFCell.getStringCellValue(); System.out.println("TEXT FROM EXCEL " + blogText); Instance iText = new Instance(2); iText.setValue((Attribute) fvWekaAttributes.elementAt(0), tweetText); iText.setValue((Attribute) fvWekaAttributes.elementAt(1), MFIndicator); isTrainingSet.add(iText); cModel = (Classifier) new J48(); cModel.buildClassifier(dataFiltered); } } } 

“给定的名义属性没有定义的值!” 在您构build的实例中到达时,预期的数据碰巧具有其他值,而不是您在arff @attribute部分为给定名义属性定义的值。 例如,您将期望值定义为“M”或“F”,但是您读取的值可能为空(N / A)等。解决scheme是严格validation数据,debugging/跟踪您加载的内容在哪个属性上发生错误,并将该值添加到该属性的可能值 – 或者,如果在您的情况下系统地出现,则将该属性定义为具有更通用的types(string,数字,..)。