Java で Weka を使用して、著者のブログを男性または女性によって書かれたものとして分類しようとしています。トレーニング セットで使用する属性を定義する 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);
}
}
}