Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/Optional Labs/C1_W3_Lab07_Scikit_Learn_Soln.ipynb
3748 views
Kernel: Python 3

Ungraded Lab: Logistic Regression using Scikit-Learn

Goals

In this lab you will:

  • Train a logistic regression model using scikit-learn.

Dataset

Let's start with the same dataset as before.

import numpy as np X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]]) y = np.array([0, 0, 0, 1, 1, 1])

Fit the model

The code below imports the logistic regression model from scikit-learn. You can fit this model on the training data by calling fit function.

from sklearn.linear_model import LogisticRegression lr_model = LogisticRegression() lr_model.fit(X, y)
LogisticRegression()

Make Predictions

You can see the predictions made by this model by calling the predict function.

y_pred = lr_model.predict(X) print("Prediction on training set:", y_pred)
Prediction on training set: [0 0 0 1 1 1]

Calculate accuracy

You can calculate this accuracy of this model by calling the score function.

print("Accuracy on training set:", lr_model.score(X, y))
Accuracy on training set: 1.0