Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Find Height and Weight from a Data Set Using Simple Linear Regression in Weka?

Linear Regression for Height and Weight Prediction

Condition for Finding Height and Weight using Simple Linear Regression in Weka

  • Description: Linear regression is used to predict one variable based on another by finding the best-fit relationship between them. The model is trained using existing data, and once trained, it can predict a value for a new input. In this case, it predicts height based on the given weight.
Sample Source Code
  • # LinearRegressionHeightWeight.java
    package JavaSamples2;

    import weka.core.Instance;
    import weka.core.DenseInstance;
    import weka.core.Instances;
    import weka.classifiers.functions.LinearRegression;
    import weka.classifiers.Evaluation;
    import weka.core.converters.ConverterUtils.DataSource;
    import java.util.Random;

    public class LinearRegressionHeightWeight {
    public static void main(String[] args) {
    try {

    DataSource source = new DataSource("dataset.arff");
    Instances dataset = source.getDataSet();

    dataset.setClassIndex(dataset.numAttributes() - 1); // Assuming "Weight" is the last attribute

    LinearRegression model = new LinearRegression();
    model.buildClassifier(dataset);

    System.out.println("Linear Regression Model: " + model);

    Evaluation evaluation = new Evaluation(dataset);
    int numFolds = Math.min(dataset.numInstances(), 10);
    evaluation.crossValidateModel(model, dataset, numFolds, new Random(1));

    System.out.println(evaluation.toSummaryString());

    double[] newInstanceValues = new double[dataset.numAttributes()];
    newInstanceValues[0] = 172; // Example height
    newInstanceValues[dataset.classIndex()] = 0; // Placeholder value for weight

    Instance instance = new DenseInstance(1.0, newInstanceValues);
    instance.setDataset(dataset);

    double predictedWeight = model.classifyInstance(instance);
    System.out.println("Predicted Weight for height 172 cm: " + predictedWeight);

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    }
Screenshots
  • Data set (dataset.arff):
  • Data Set: dataset.arff

  • Step 1: The Height and Weight results of Linear Regression are displayed in the output window.
  • Linear Regression Output for Height and Weight