1.实验目的
使用线性回归模型预测2020年加拿大公民的人均收入。
数据链接
密码:zc6h
2.导入必要模块并读取数据
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression #导入线性回归模块
import matplotlib.pyplot as plt
df = pd.read_csv('canada_per_capita_income.csv')
df.head()
3.画当前数据分布散点图
%matplotlib inline
plt.scatter(df.year,df['per capita income (US$)'],color='red',marker='+') #观看数据分布
plt.xlabel('year')
plt.ylabel('per capita income (US$)')
4.提取数据和标签
new_df = df.drop('per capita income (US$)',axis='columns') #数据
new_df.head()
price = df['per capita income (US$)'] #标签
price.head()
5.训练+预测
reg = LinearRegression() #实例化模型
reg.fit(new_df,price) #训练
reg.predict([[2020]]) #预测2020年房价