import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
dataset = pd.read_csv('SD.csv')
print("查看dataset")
print(dataset)
X = dataset.iloc[:, :-1].values# 矩阵
y = dataset.iloc[:, 1].values # 向量
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
print("X_train")
print(X_train)
print("X_test")
print(X_test)
regressor = LinearRegression()
regressor.fit(X_train,y_train)
y_pred = regressor.predict(X_test)
print("y_pred")
print(y_pred)
plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary -training )')
plt.xlabel('Years ')
plt.ylabel('Salary')
plt.show()
# Visualising the Test set results
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary -test )')
plt.xlabel('Years')
plt.ylabel('Salary')
plt.show()
机器学习(3)-简单线性回归:数据集与源码下载
需积分: 50 137 浏览量
2018-04-09
20:11:17
上传
评论 5
收藏 1KB RAR 举报
qq_38248958
- 粉丝: 46
- 资源: 24