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

Office Address

Social List

How to Find Gender with Height and Weight from a Data Set Using Logistic Regression in Weka?

Logistic Regression for Gender Prediction

Condition for Finding Gender with Height and Weight using Logistic Regression in Weka

  • Description: To use logistic regression in Weka for gender prediction, first load the dataset (e.g., height, weight, gender) and set the target variable (gender) as the class attribute. Then, create and train a logistic regression model using the Logistic classifier. Finally, make predictions by inputting new data (height and weight) and output the predicted gender based on the trained model.
Sample Source Code
  • # LogisticRegression.java
    package JavaSamples2;

    import weka.core.Instances;
    import weka.core.Instance;
    import weka.core.converters.ConverterUtils.DataSource;
    import weka.classifiers.Classifier;
    import weka.classifiers.functions.Logistic;

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

    DataSource source = new DataSource("/home/soft20/NetBeansProjects/JavaSamples/gender_height_weight.arff"); // Ensure this path is correct
    Instances dataset = source.getDataSet();

    if (dataset.classIndex() == -1) {
    dataset.setClassIndex(dataset.numAttributes() - 1);
    }

    Classifier logistic = new Logistic();
    logistic.buildClassifier(dataset);

    System.out.println("Logistic Regression Model:");
    System.out.println(logistic);

    double height = 172.0;
    double weight = 65.0;

    Instance newInstance = (Instance) dataset.firstInstance().copy();
    newInstance.setValue(0, height);
    newInstance.setValue(1, weight);
    newInstance.setDataset(dataset);

    double predictedClassIndex = logistic.classifyInstance(newInstance);
    String predictedGender = dataset.classAttribute().value((int) predictedClassIndex);

    System.out.println("Predicted Gender: " + predictedGender);

    } catch (Exception e) {
    System.out.println("Error occurred: " + e.getMessage());
    System.out.println(e.getMessage());
    }
    }
    }
Screenshots
  • Data set (gender_height_weight.arff):
  • Data Set: gender_height_weight.arff

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