我有一个包含数值和名义属性的CSV数据集。我为该数据集定义了 模式,列出了名义属性的所有可能值。之后,我创建了 TransformProcess,使用 CategoricalToOneHotTransform 将名义值转换为数值。我如何在 RecordReaderDataSetIterator 上使用此 TransformProcess 来为我的神经网络做准备?
Schema schema = new Schema.Builder()
.addColumnInteger("age")
.addColumnCategorical("workclass", "Private", "Self-emp-not-inc", "Self-emp-inc", "Federal-gov", "Local-gov", "State-gov", "Without-pay", "Never-worked")
.addColumnInteger("fnlwgt")
.addColumnCategorical("education", "Bachelors", "Some-college", "11th", "HS-grad", "Prof-school", "Assoc-acdm", "Assoc-voc", "9th", "7th-8th", "12th", "Masters", "1st-4th", "10th", "Doctorate", "5th-6th", "Preschool")
.addColumnInteger("education-num")
.addColumnCategorical("marital-status", "Married-civ-spouse", "Divorced", "Never-married", "Separated", "Widowed", "Married-spouse-absent", "Married-AF-spouse")
.addColumnCategorical("occupation", "Tech-support", "Craft-repair", "Other-service", "Sales", "Exec-managerial", "Prof-specialty", "Handlers-cleaners", "Machine-op-inspct", "Adm-clerical", "Farming-fishing", "Transport-moving", "Priv-house-serv", "Protective-serv", "Armed-Forces")
.addColumnCategorical("relationship", "Wife", "Own-child", "Husband", "Not-in-family", "Other-relative", "Unmarried")
.addColumnCategorical("race", "White", "Asian-Pac-Islander", "Amer-Indian-Eskimo", "Other", "Black")
.addColumnCategorical("sex", "Female", "Male")
.addColumnInteger("capital-gain")
.addColumnInteger("capital-loss")
.addColumnInteger("hours-per-week")
.addColumnCategorical("native-country", "United-States", "Cambodia", "England", "Puerto-Rico", "Canada", "Germany", "Outlying-US(Guam-USVI-etc)", "India", "Japan", "Greece", "South", "China", "Cuba", "Iran", "Honduras", "Philippines", "Italy", "Poland", "Jamaica", "Vietnam", "Mexico", "Portugal", "Ireland", "France", "Dominican-Republic", "Laos", "Ecuador", "Taiwan", "Haiti", "Columbia", "Hungary", "Guatemala", "Nicaragua", "Scotland", "Thailand", "Yugoslavia", "El-Salvador", "Trinadad&Tobago", "Peru", "Hong", "Holand-Netherlands")
.addColumnCategorical("class", ">50K", "<=50K")
.build();
TransformProcess tp = new TransformProcess.Builder(schema)
.transform(new CategoricalToOneHotTransform("workclass"))
.transform(new CategoricalToOneHotTransform("education"))
.transform(new CategoricalToOneHotTransform("marital-status"))
.transform(new CategoricalToOneHotTransform("occupation"))
.transform(new CategoricalToOneHotTransform("relationship"))
.transform(new CategoricalToOneHotTransform("race"))
.transform(new CategoricalToOneHotTransform("sex"))
.transform(new CategoricalToOneHotTransform("native-country"))
.transform(new CategoricalToIntegerTransform("class"))
.build();
Schema outputSchema = tp.getFinalSchema();
int numLinesToSkip = 0;
String delimiter = ",";
CSVRecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
recordReader.initialize(new FileSplit(Paths.get("..\\adult.data").toFile()));
int labelIndex = outputSchema.getColumnNames().size() - 1;
int numClasses = 2;
int batchSize = 2000;
RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, numClasses);
DataSet allData = iterator.next();
allData.shuffle();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65);
RecordReader transformProcessRecordReader = new TransformProcessRecordReader(reader,transformProcess);
DataSetIterator iterator = new RecordReaderDataSetIterator(transformProcessRecordReader,labelIndex,numClasses,batchSize);
但它抛出了错误next() is unsupported
。 - Rahul Raj