org.apache.spark.ml.classification.DecisionTreeClassifier Java Examples
The following examples show how to use
org.apache.spark.ml.classification.DecisionTreeClassifier.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DecisionTreeClassificationModelBridgeTest.java From spark-transformers with Apache License 2.0 | 5 votes |
@Test public void testDecisionTreeClassificationPrediction() { // Load the data stored in LIBSVM format as a DataFrame. String datapath = "src/test/resources/classification_test.libsvm"; Dataset<Row> data = spark.read().format("libsvm").load(datapath); // Split the data into training and test sets (30% held out for testing) Dataset<Row>[] splits = data.randomSplit(new double[]{0.7, 0.3}); Dataset<Row> trainingData = splits[0]; Dataset<Row> testData = splits[1]; // Train a DecisionTree model. DecisionTreeClassificationModel classifierModel = new DecisionTreeClassifier().fit(trainingData); trainingData.printSchema(); List<Row> output = classifierModel.transform(testData).select("features", "prediction","rawPrediction").collectAsList(); byte[] exportedModel = ModelExporter.export(classifierModel); DecisionTreeTransformer transformer = (DecisionTreeTransformer) ModelImporter.importAndGetTransformer(exportedModel); //compare predictions for (Row row : output) { Map<String, Object> data_ = new HashMap<>(); double [] actualRawPrediction = ((DenseVector) row.get(2)).toArray(); data_.put("features", ((SparseVector) row.get(0)).toArray()); transformer.transform(data_); System.out.println(data_); System.out.println(data_.get("prediction")); assertEquals((double)data_.get("prediction"), (double)row.get(1), EPSILON); assertArrayEquals((double[]) data_.get("rawPrediction"), actualRawPrediction, EPSILON); } }
Example #2
Source File: DatasetClassifier.java From mmtf-spark with Apache License 2.0 | 4 votes |
/** * @param args args[0] path to parquet file, args[1] name of classification column * @throws IOException * @throws StructureException */ public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println("Usage: " + DatasetClassifier.class.getSimpleName() + " <parquet file> <classification column name>"); System.exit(1); } // name of the class label String label = args[1]; long start = System.nanoTime(); SparkSession spark = SparkSession .builder() .master("local[*]") .appName(DatasetClassifier.class.getSimpleName()) .getOrCreate(); Dataset<Row> data = spark.read().parquet(args[0]).cache(); int featureCount = 0; Object vector = data.first().getAs("features"); if (vector instanceof DenseVector) { featureCount = ((DenseVector)vector).numActives(); } else if (vector instanceof SparseVector) { featureCount = ((SparseVector)vector).numActives(); } System.out.println("Feature count : " + featureCount); int classCount = (int)data.select(label).distinct().count(); System.out.println("Class count : " + classCount); System.out.println("Dataset size (unbalanced): " + data.count()); data.groupBy(label).count().show(classCount); data = DatasetBalancer.downsample(data, label, 1); System.out.println("Dataset size (balanced) : " + data.count()); data.groupBy(label).count().show(classCount); double testFraction = 0.3; long seed = 123; SparkMultiClassClassifier mcc; Map<String, String> metrics; DecisionTreeClassifier dtc = new DecisionTreeClassifier(); mcc = new SparkMultiClassClassifier(dtc, label, testFraction, seed); metrics = mcc.fit(data); System.out.println(metrics); RandomForestClassifier rfc = new RandomForestClassifier(); mcc = new SparkMultiClassClassifier(rfc, label, testFraction, seed); metrics = mcc.fit(data); System.out.println(metrics); LogisticRegression lr = new LogisticRegression(); mcc = new SparkMultiClassClassifier(lr, label, testFraction, seed); metrics = mcc.fit(data); System.out.println(metrics); // specify layers for the neural network // input layer: dimension of feature vector // output layer: number of classes int[] layers = new int[] {featureCount, 10, classCount}; MultilayerPerceptronClassifier mpc = new MultilayerPerceptronClassifier() .setLayers(layers) .setBlockSize(128) .setSeed(1234L) .setMaxIter(200); mcc = new SparkMultiClassClassifier(mpc, label, testFraction, seed); metrics = mcc.fit(data); System.out.println(metrics); long end = System.nanoTime(); System.out.println((end-start)/1E9 + " sec"); }
Example #3
Source File: JavaDecisionTreeClassificationExample.java From SparkDemo with MIT License | 4 votes |
public static void main(String[] args) { SparkSession spark = SparkSession .builder() .appName("JavaDecisionTreeClassificationExample") .getOrCreate(); // $example on$ // Load the data stored in LIBSVM format as a DataFrame. Dataset<Row> data = spark .read() .format("libsvm") .load("data/mllib/sample_libsvm_data.txt"); // Index labels, adding metadata to the label column. // Fit on whole dataset to include all labels in index. StringIndexerModel labelIndexer = new StringIndexer() .setInputCol("label") .setOutputCol("indexedLabel") .fit(data); // Automatically identify categorical features, and index them. VectorIndexerModel featureIndexer = new VectorIndexer() .setInputCol("features") .setOutputCol("indexedFeatures") .setMaxCategories(4) // features with > 4 distinct values are treated as continuous. .fit(data); // Split the data into training and test sets (30% held out for testing). Dataset<Row>[] splits = data.randomSplit(new double[]{0.7, 0.3}); Dataset<Row> trainingData = splits[0]; Dataset<Row> testData = splits[1]; // Train a DecisionTree model. DecisionTreeClassifier dt = new DecisionTreeClassifier() .setLabelCol("indexedLabel") .setFeaturesCol("indexedFeatures"); // Convert indexed labels back to original labels. IndexToString labelConverter = new IndexToString() .setInputCol("prediction") .setOutputCol("predictedLabel") .setLabels(labelIndexer.labels()); // Chain indexers and tree in a Pipeline. Pipeline pipeline = new Pipeline() .setStages(new PipelineStage[]{labelIndexer, featureIndexer, dt, labelConverter}); // Train model. This also runs the indexers. PipelineModel model = pipeline.fit(trainingData); // Make predictions. Dataset<Row> predictions = model.transform(testData); // Select example rows to display. predictions.select("predictedLabel", "label", "features").show(5); // Select (prediction, true label) and compute test error. MulticlassClassificationEvaluator evaluator = new MulticlassClassificationEvaluator() .setLabelCol("indexedLabel") .setPredictionCol("prediction") .setMetricName("accuracy"); double accuracy = evaluator.evaluate(predictions); System.out.println("Test Error = " + (1.0 - accuracy)); DecisionTreeClassificationModel treeModel = (DecisionTreeClassificationModel) (model.stages()[2]); System.out.println("Learned classification tree model:\n" + treeModel.toDebugString()); // $example off$ spark.stop(); }
Example #4
Source File: DecisionTreeClassificationModelBridgePipelineTest.java From spark-transformers with Apache License 2.0 | 4 votes |
@Test public void testDecisionTreeClassificationWithPipeline() { // Load the data stored in LIBSVM format as a DataFrame. String datapath = "src/test/resources/classification_test.libsvm"; Dataset<Row> data = spark.read().format("libsvm").load(datapath); // Split the data into training and test sets (30% held out for testing) Dataset<Row>[] splits = data.randomSplit(new double[]{0.7, 0.3}); Dataset<Row> trainingData = splits[0]; Dataset<Row> testData = splits[1]; StringIndexer indexer = new StringIndexer() .setInputCol("label") .setOutputCol("labelIndex"); // Train a DecisionTree model. DecisionTreeClassifier classificationModel = new DecisionTreeClassifier() .setLabelCol("labelIndex") .setFeaturesCol("features"); Pipeline pipeline = new Pipeline() .setStages(new PipelineStage[]{indexer, classificationModel}); // Train model. This also runs the indexer. PipelineModel sparkPipeline = pipeline.fit(trainingData); //Export this model byte[] exportedModel = ModelExporter.export(sparkPipeline); //Import and get Transformer Transformer transformer = ModelImporter.importAndGetTransformer(exportedModel); List<Row> output = sparkPipeline.transform(testData).select("features", "label","prediction","rawPrediction").collectAsList(); //compare predictions for (Row row : output) { Map<String, Object> data_ = new HashMap<>(); double [] actualRawPrediction = ((DenseVector) row.get(3)).toArray(); data_.put("features", ((SparseVector) row.get(0)).toArray()); data_.put("label", (row.get(1)).toString()); transformer.transform(data_); System.out.println(data_); System.out.println(data_.get("prediction")); assertEquals((double)data_.get("prediction"), (double)row.get(2), EPSILON); assertArrayEquals((double[]) data_.get("rawPrediction"), actualRawPrediction, EPSILON); } }
Example #5
Source File: DecisionTreeClassificationModelBridgeTest.java From spark-transformers with Apache License 2.0 | 4 votes |
@Test public void testDecisionTreeClassificationRawPrediction() { // Load the data stored in LIBSVM format as a DataFrame. DataFrame data = sqlContext.read().format("libsvm").load("src/test/resources/classification_test.libsvm"); StringIndexerModel stringIndexerModel = new StringIndexer() .setInputCol("label") .setOutputCol("labelIndex") .fit(data); data = stringIndexerModel.transform(data); // Split the data into training and test sets (30% held out for testing) DataFrame[] splits = data.randomSplit(new double[]{0.7, 0.3}); DataFrame trainingData = splits[0]; DataFrame testData = splits[1]; // Train a DecisionTree model. DecisionTreeClassificationModel classificationModel = new DecisionTreeClassifier() .setLabelCol("labelIndex") .setFeaturesCol("features") .setRawPredictionCol("rawPrediction") .setPredictionCol("prediction") .fit(trainingData); byte[] exportedModel = ModelExporter.export(classificationModel, null); Transformer transformer = (DecisionTreeTransformer) ModelImporter.importAndGetTransformer(exportedModel); Row[] sparkOutput = classificationModel.transform(testData).select("features", "prediction", "rawPrediction").collect(); //compare predictions for (Row row : sparkOutput) { Vector inp = (Vector) row.get(0); double actual = row.getDouble(1); double[] actualRaw = ((Vector) row.get(2)).toArray(); Map<String, Object> inputData = new HashMap<>(); inputData.put(transformer.getInputKeys().iterator().next(), inp.toArray()); transformer.transform(inputData); double predicted = (double) inputData.get(transformer.getOutputKeys().iterator().next()); double[] rawPrediction = (double[]) inputData.get("rawPrediction"); assertEquals(actual, predicted, EPSILON); assertArrayEquals(actualRaw, rawPrediction, EPSILON); } }
Example #6
Source File: DecisionTreeClassificationModelBridgeTest.java From spark-transformers with Apache License 2.0 | 4 votes |
@Test public void testDecisionTreeClassificationWithPipeline() { // Load the data stored in LIBSVM format as a DataFrame. DataFrame data = sqlContext.read().format("libsvm").load("src/test/resources/classification_test.libsvm"); // Split the data into training and test sets (30% held out for testing) DataFrame[] splits = data.randomSplit(new double[]{0.7, 0.3}); DataFrame trainingData = splits[0]; DataFrame testData = splits[1]; StringIndexer indexer = new StringIndexer() .setInputCol("label") .setOutputCol("labelIndex"); // Train a DecisionTree model. DecisionTreeClassifier classificationModel = new DecisionTreeClassifier() .setLabelCol("labelIndex") .setFeaturesCol("features"); Pipeline pipeline = new Pipeline() .setStages(new PipelineStage[]{indexer, classificationModel}); // Train model. This also runs the indexer. PipelineModel sparkPipeline = pipeline.fit(trainingData); //Export this model byte[] exportedModel = ModelExporter.export(sparkPipeline, null); //Import and get Transformer Transformer transformer = ModelImporter.importAndGetTransformer(exportedModel); Row[] sparkOutput = sparkPipeline.transform(testData).select("label", "features", "prediction").collect(); //compare predictions for (Row row : sparkOutput) { Vector v = (Vector) row.get(1); double actual = row.getDouble(2); Map<String, Object> inputData = new HashMap<String, Object>(); inputData.put("features", v.toArray()); inputData.put("label", row.get(0).toString()); transformer.transform(inputData); double predicted = (double) inputData.get("prediction"); assertEquals(actual, predicted, EPSILON); } }