波士顿房价预测
from sklearn.linear_model import LinearRegression
print("线性回归模型训练例子")
clf = LinearRegression()
clf.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
'''
y = 0.5*x1 + 0.5*x2 #线性方程(预测模型)
'''
pre = clf.predict([[3, 3]])
print("回归系数",clf.coef_)
print("截距",clf.intercept_)
print("数据[3, 3]的预测值:",pre)
print("*****************************")
print("波士顿房价数据回归分析")
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
boston = load_boston()
x = boston.data[:, 5:6]
clf = LinearRegression()
clf.fit(x, boston.target)
clf.coef_
y_pre = clf.predict(x)
plt.scatter(x, boston.target)
plt.plot(x, y_pre, color='red')
plt.show()
研究生录取预测
print("前5个研究生信息",'\n',data.head())
print("研究生录取预测*************************************")
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
data = pd.read_csv('LogisticRegression.csv')
data_tr, data_te, label_tr, label_te = train_test_split(data.iloc[:, 1:], data['admit'], test_size=0.2)
clf = LogisticRegression()
clf.fit(data_tr, label_tr)
pre = clf.predict(data_te)
res = classification_report(label_te, pre)
print(res)