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年房價