# -*- coding: utf-8 -*-
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
# 载入数据
data = np.genfromtxt('一元线性回归.csv', delimiter=',')
x_data = data[:,0]
y_data = data[:,1]
plt.scatter(x_data,y_data)
plt.show()
#x_data = data[:,0:1]
#x_data = data[:,0,np.newaxis]
x_data
x_data = data[:,0,np.newaxis]
y_data = data[:,1,np.newaxis]
# 创建并拟合模型
model = LinearRegression()
model.fit(x_data, y_data)
coef = model.coef_ #获得该回该方程的回归系数与截距
intercept = model.intercept_
print("预测方程回归系数:", coef)
print("预测方程截距:", intercept)
# 画图
plt.plot(x_data, y_data, 'g.')
plt.plot(x_data, model.predict(x_data), 'r')
plt.show()