最近機械学習を使うことが多くなってきたので、アウトプットがてらScikit Learnを使ってエストニアの天気を予測してみます。
データ
インプットデータは、Ninja というサイトから取ってきています。
今回はエストニアを選択しましたが、他の国を選ぶこともできますし風力発電や太陽光発電のデータもあるのでみてみると良いかもしれません。
1年間のエストニアの気温をインプットデータとして使います。とりあえず、以下のライブラリをインポートします。
今回のスクリプトでは使わないライブラリもありますが、コピペすれば問題ないです。
プラットフォームは、Colabを使っています。
from math import sqrt from numpy import concatenate import matplotlib.pyplot as plt import pandas as pd from pandas import Series from datetime import datetime import numpy as np import time from google.colab import auth from oauth2client.client import GoogleCredentials from google.colab import drive from google.colab import files import os.path !pip install pydrive import pydrive from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive %matplotlib inline drive.mount('/content/gdrive')
データインポート・プロット
Colabのデータインポート方法が特殊なので、インプットから公開します。
def dataDriveExtract(id, file): auth.authenticate_user()#Auth check gauth = GoogleAuth() #google Auth Instance gauth.credentials = GoogleCredentials.get_application_default() # credential part drive = GoogleDrive(gauth)# put the credential information to drive downloaded = drive.CreateFile({'id':str(id)})# Specify the file location downloaded.GetContentFile(file)# download the file with the file name data = pd.read_csv(file, encoding = 'utf-8', index_col='time') # load the data via pandas data.index = pd.to_datetime(data.index) #Date Index formatting return data con = "weather" if con == "weather": data_weather = dataDriveExtract('sharable-id', 'EE-Weather-1985-2016.csv') data_2016weather = data_weather["2016-01-01":"2016-12-31"] data_2016weather.info() # show data info # print(data_2016weather.head()) # show data info # print(data_2016weather.head(1)) elif con == "pv": data_pv = dataDriveExtract() data_2016pv = data_weather["2016-01-01":"2016-12-31"] data.info() # show data info
データを取り込んで、Pandasでスライスしていきます。
これによって得られるデータの詳細は以下になります。
data_temp = data_2016weather.loc[:, 'temperature'] data_temp.plot() data_temp.describe()
description オブジェクトによって、エストニアの気温の詳細がわかります。
最高気温は、24.9度
最低気温は、-16度
平均気温は、6.23度
ですね。
気温の上昇率を計算
#データ数をcount_sに代入 count_s = len(data_temp) # 上昇率を格納するリストを作成 inc_rate =[] #気温の上昇率を計算 for i in range(1, count_s): try: #上昇率を計算する式 inc_rate.append(float(data_temp[i] - data_temp[i-1]) / float(data_temp[i-1])) except ZeroDivisionError: #ゼロが分母にきた場合は、0に変換 inc_rate.append(0) # 気温上昇率のデータ数を格納 count_m = len(inc_rate) # 気温上昇率のデータ数を表示 print(count_m)
上昇率のデータ数は、8783となります。
Scikit-Learnによって学習・評価
#データ数 n = len(successive_data) print(n) m = len(answers) print(m)
#線形サポートベクタマシーン clf = svm.LinearSVC() # トレーニングするデータ数を指定 tr= 8500 # Training clf.fit(successive_data[:tr], answers[:tr]) # テストデータ数を取得 te = n - tr # Test expected = answers[te:] # predict predicted = clf.predict(successive_data[-te:]) correct = 0 wrong = 0 # 正解率計算 for i in range(te): #予測と実測が一致したら、correctに追加 if expected[i] == predicted[i]: correct += 1 else: wrong += 1 #正解率を計算 print("正解率: " + str(correct / (correct+wrong) * 100) + "%")
アウトプットは
正解率: 45.65217391304348%
となります。
あまり良い正解率を得ることはできませんでした。
データのスクリーニングやモデリングを変更すると良いかもしれません。
コメント