[Machine Learning] P7 Scikit Learn implements linear regression and logistic regression

Scikit-learn package

Execute in Anaconda Prompt:

conda install scikit-learn

Install the Scikit Learn package to complete;

Scikit Learn implements linear regression

Import package LinearRegression
Import datasetload_boston

from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston

Load the Boston house price dataset

# 加载波士顿房价数据集
boston = load_boston()
X = boston.data
y = boston.target

LinearRegressionCreate a linear regression model usinglr

# 创建一个线性回归模型
lr = LinearRegression()

Training dataset, use the trained linear regression model to make predictionsy_pred

# 使用训练集对模型进行训练
lr.fit(X, y)

# 训练完成
# 预测新数据的房价
y_pred = lr.predict(X)

Graphing observed predicted values ​​(blue) versus actual values ​​(green):

import matplotlib.pyplot as plt

plt.scatter(range(X.shape[0]),y,c="green")
plt.scatter(range(X.shape[0]),y_pred,c="blue")

plt.show()

insert image description here
The number of attributes, coefficients, and accuracy of the output model

print("模型系数数量:", X.shape[1])
print("模型系数:", lr.coef_)
print("模型准确度为:", lr.score(X, y))

Full code:

from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston

# 加载波士顿房价数据集
boston = load_boston()
X = boston.data
y = boston.target

# 创建一个线性回归模型
lr = LinearRegression()

# 使用训练集对模型进行训练
lr.fit(X, y)

# 训练完成
# 预测新数据的房价
y_pred = lr.predict(X)
# print(y_pred)
# print(y)

import matplotlib.pyplot as plt

plt.scatter(range(X.shape[0]),y,c="green")
plt.scatter(range(X.shape[0]),y_pred,c="blue")

plt.show()

print("模型系数:", lr.coef_)
print("模型准确度:", lr.score(X, y))

More additions:

sklearn 特征缩放

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

sklearn 计算均方误差

from sklearn.metrics import mean_squared_error

y_pred = model.predict(X_scaled)
mse = mean_squared_error(y, y_pred)
print("均方误差:", mse)

Scikit Learn Implements Logistic Regression

import packageLinearRegression

from sklearn.linear_model import LogisticRegression

Build simple case data

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])

LogisticRegressionCreate a logistic regression model usinglr_model

lr_model = LogisticRegression()

Training dataset, use the trained logistic regression model to make predictionsy_pred

# 训练数据集
lr_model.fit(X, y)

# 训练完成
# 做出预测 y_pred
y_pred = lr_model.predict(X)

Graphing observed predicted values ​​(blue) versus actual values ​​(green):

import matplotlib.pyplot as plt

plt.scatter(range(X.shape[0]),y,c="green")
plt.scatter(range(X.shape[0]),y_pred,c="blue")

plt.show()

因为数据太少,准确度达到了 100% ,蓝色预测结果把绿色实际值完全覆盖
insert image description here

View accuracyAccuracy

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

Reference

[1] Ng, A. (2021). Coursera - Machine Learning - Week 3: Logistic Regression with Scikit-Learn [Ungraded Lab]. Coursera. Retrieved from https://www.coursera.org/learn/machine-learning/ungradedLab/F3ZpI/optional-lab-logistic-regression-with-scikit-learn/lab?path=%2Fnotebooks%2FC1_W3_Lab07_Scikit_Learn_Soln.ipynb

[2] Ng, A. (2021). Coursera - Machine Learning - Week 2: Linear Regression with Scikit-Learn [Ungraded Lab]. Coursera. Retrieved from https://www.coursera.org/learn/machine-learning/ungradedLab/uaIsm/optional-lab-linear-regression-with-scikit-learn

Guess you like

Origin blog.csdn.net/weixin_43098506/article/details/129897808