Machine Learning A-Z: Hands-On Python and java
About Lesson
      • Introduction to classification, implementation in Python and Java.

Introduction to Logistic Regression

Logistic regression is a supervised learning algorithm used for classification tasks. Unlike linear regression, which predicts continuous values, logistic regression predicts categorical outcomes by estimating the probability that a given input belongs to a particular class using the logistic function.

Implementation in Python (Scikit-learn)

Step 1: Import Libraries

pythonCopier le codefrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import train_test_split

Step 2: Prepare Data and Split into Training/Testing Sets

pythonCopier le codeX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 3: Train the Model

pythonCopier le codemodel = LogisticRegression()model.fit(X_train, y_train)

Step 4: Make Predictions

pythonCopier le codepredictions = model.predict(X_test)

Implementation in Java (Weka)

Step 1: Load the Dataset

javaCopier le codeInstances data = new Instances(new BufferedReader(new FileReader(“data.arff”)));data.setClassIndex(data.numAttributes() – 1);

Step 2: Build the Logistic Regression Model

javaCopier le codeLogistic model = new Logistic();model.buildClassifier(data);

Step 3: Evaluate the Model

javaCopier le codeEvaluation eval = new Evaluation(data);eval.crossValidateModel(model, data, 10, new Random(1));