0
点赞
收藏
分享

微信扫一扫

【机器学习类神经网路】Week3 Homework 线性回归预测气温

1kesou 2022-03-14 阅读 49
python

题目

  1. 讀取溫度差距資料檔案,並以每隔10年的年代(Decade)和溫度差(Change from 1880)繪製成散佈圖。资料来源
  2. 依據1880年起的資料建立並訓練線性迴歸模型,完成後進行「2030-2040」及「2050-2060」的十年均溫趨勢預測。
  3. 刪除資料框中1880-1960的資料,再繪製從1970年起的年代與溫度差散佈圖。
  4. 以近50年(1970-2020)的資料建立並訓練線性迴歸模型,並且進行「2030-2040」及「2050~2060」的十年均溫的趨勢預測。
  5. 儲存結果:程式碼Penguin08.ipynb。

1. 讀取溫度差距資料檔案,並以每隔10年的年代(Decade)和溫度差(Change from 1880)繪製成散佈圖。

import pandas as pd
df = pd.read_csv('D:/大學本科/110-2/001机器学习类神经网路/Data/1880-2020年均溫.csv')
df

运行结果
在这里插入图片描述

df.plot(kind='scatter', x='Decade', y='Change from 1880')

在这里插入图片描述

2. 依據1880年起的資料建立並訓練線性迴歸模型,完成後進行「2030-2040」及「2050-2060」的十年均溫趨勢預測。

df_X = df[['Decade']]
df_y = df['Change from 1880']
df_X.head()

在这里插入图片描述

#载入模组sklearn中的线性回归模型
from sklearn.linear_model import LinearRegression
#建模
lm =LinearRegression()
#训练模型
lm.fit(df_X,df_y)
temp = [[2030],[2040]]
p = lm.predict(temp)
print(p)

执行结果:
[0.94518095 1.01877024]

print("通过线性归回预测,2030~2040升温",round(p[1]-p[0],3),"℃")

执行结果:
通过线性归回预测,2030~2040升温 0.074 ℃

temp = [[2050],[2060]]
p = lm.predict(temp)
print(p)

执行结果:
[1.09235952 1.16594881]

print("通过线性归回预测,2050~2060升温",round(p[1]-p[0],3),"℃")

执行结果:
通过线性归回预测,2050~2060升温 0.074 ℃

3. 刪除資料框中1880-1960的資料,再繪製從1970年起的年代與溫度差散佈圖。

import numpy as np
#np.arange(起点,终点+1,步长)
df_1 = df.drop(np.arange(0,9,1),axis =0)
df_1.rename(columns={'Change from 1880':'Change from 1970'},inplace=True)
df_1

在这里插入图片描述

df_1.plot(kind='scatter', x='Decade', y='Change from 1970')

在这里插入图片描述

4. 以近50年(1970-2020)的資料建立並訓練線性迴歸模型,並且進行「2030-2040」及「2050-2060」的十年均溫的趨勢預測。

df_1X = df_1[['Decade']]
df_1y = df_1['Change from 1970']
df_1X.head()

在这里插入图片描述

#载入模组sklearn中的线性回归模型
from sklearn.linear_model import LinearRegression
#建模
lm =LinearRegression()
#训练模型
lm.fit(df_1X,df_1y)
temp = [[2030],[2040]]
p = lm.predict(temp)
print(p)

执行结果:
[1.2686 1.43291429]

print("通过线性归回预测,2030~2040升温",round(p[1]-p[0],3),"℃")

执行结果:
通过线性归回预测,2030~2040升温 0.164 ℃

temp = [[2050],[2060]]
p = lm.predict(temp)
print(p)

执行结果:
[1.59722857 1.76154286]

print("通过线性归回预测,2050~2060升温",round(p[1]-p[0],3),"℃")

执行结果:
通过线性归回预测,2050~2060升温 0.164 ℃

举报

相关推荐

0 条评论