Scikit Learn 解答
Ungraded Lab: Logistic Regression using Scikit-Learn¶
Dataset¶
Let's start with the same dataset as before.
In [1]:
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.
In [3]:
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression()
lr_model.fit(X, y)
Out[3]:
LogisticRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression()
Make Predictions¶
You can see the predictions made by this model by calling the predict function.
In [4]:
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.
In [5]:
print("Accuracy on training set:", lr_model.score(X, y))
Accuracy on training set: 1.0
In [ ]: