数据下载:
https://pan.baidu.com/s/1eHYQ6YHWXx8E-BZJk1qTFg
提取码:o87r
代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
path = "ex1data1.txt"
data = pd.read_csv(path, names=["Population", "Profit"])
model = linear_model.LinearRegression()
cols = data.shape[1] # 获取列数
x = data.iloc[:, 0:cols - 1] # 输入向量X为前cols-1列
y = data.iloc[:, cols - 1:cols] # 目标变量y为最后一列
model.fit(x, y)
f = model.predict(x).flatten()
fig, ax = plt.subplots(figsize=(8,5))
ax.plot(x, f, 'r', label='Prediction')
ax.scatter(data.Population, data.Profit, label='Traning Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')
plt.show()