xgboost로 벚꽃 개화 예측
작년 3월부터 올해 2월까지의 데이터 활용
Python
초학자
기계 학습
1. 목적 
AI 벚꽃 예측이 있고, xgboost를 사용했다는 보도가 있어 xgboost에서 벚꽃 개화 예측을 했다.
https://www.businessinsider.jp/post-186528 
2. 결론 
미묘한 결과다.위 AI 벚꽃의 예측이 우수하다는 것을 깨달았다.
 
개화 시기 영향이 가장 큰 요인은 연평균 기온, 7월 일조시간, 8월 강우량, 10월 최저기온이다.
연평균 기온은 알 수 있지만 7월의 일조 시간, 8월의 강우량, 10월의 최저 기온은 의외였다.
3. 데이터 소스 
https://www.data.jma.go.jp/gmd/risk/obsdl/index.php 
가공 후 상기 기상청의 데이터를 이용했다.
4. 코드 설명 import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from xgboost import XGBRegressor, plot_importance
from sklearn.model_selection import GridSearchCV, KFold
from tqdm import tqdm_notebook
path="./"
train = pd.read_csv(path+"kikou5train.csv")
x_test = pd.read_csv(path+"kikou5test.csv")
y_train = train.kaika.copy()
x_train = train.drop("kaika", axis=1)
train_id = x_train.Id
x_train.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
Id                                                                                  
1   1961    8.2 21.9    -0.4    106.6   181.1   6.7 6.3 14.9    26.0    ... 3.8 5.9 24.5    -2.6    13.5    195.0   4.5 4.1 NaN 193.6
2   1962    8.2 18.8    -0.8    65.5    189.8   6.3 4.7 14.1    24.5    ... 2.0 4.8 15.3    -4.1    21.3    199.9   4.1 4.9 NaN 182.3
잇닿다df = pd.concat([x_train, x_test])
df.head()
특징량 공사 연평균 기온 추가df["TotalInc"] = df.avtmp3 + df.avtmp4 + df.avtmp5 + df.avtmp6 + df.avtmp7 + df.avtmp8 + df.avtmp9 + df.avtmp10 + df.avtmp11 + df.avtmp12 + df.avtmp13 + df.avtmp14 #平均温度の
 df.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
0   1980    8.2 21.2    1.3 173.5   157.5   6   6.2 13.6    24  ... 2.9 5.3 17.2    -3.5    38  157.3   4.6 5.5 NaN 183.4
1 rows × 87 columns
 x_train = df[df.Id.isin(train_id)].set_index("Id")
x_test = df[~df.Id.isin(train_id)].set_index("Id")
최적 매개 변수 검색random_state = 0
params = {
          "learning_rate": [0.01, 0.05, 0.1],
          "min_child_weight": [0.1],
          "gamma": [0],
          "reg_alpha": [0],
          "reg_lambda": [1],
          "max_depth": [3, 5, 7],
          "max_delta_step": [0],
          "random_state": [random_state],
          "n_estimators": [50, 100, 200],
          }
reg = XGBRegressor()
cv = KFold(n_splits=3, shuffle=True, random_state=random_state)
reg_gs = GridSearchCV(reg, params, cv=cv)
reg_gs.fit(x_train, y_train)
GridSearchCV(cv=KFold(n_splits=3, random_state=0, shuffle=True),
             estimator=XGBRegressor(base_score=None, booster=None,
                                    colsample_bylevel=None,
                                    colsample_bynode=None,
                                    colsample_bytree=None, gamma=None,
                                    gpu_id=None, importance_type='gain',
                                    interaction_constraints=None,
                                    learning_rate=None, max_delta_step=None,
                                    max_depth=None, min_child_weight=None,
                                    missing=nan, monoto...
                                    num_parallel_tree=None, random_state=None,
                                    reg_alpha=None, reg_lambda=None,
                                    scale_pos_weight=None, subsample=None,
                                    tree_method=None, validate_parameters=None,
                                    verbosity=None),
             param_grid={'gamma': [0], 'learning_rate': [0.01, 0.05, 0.1],
                         'max_delta_step': [0], 'max_depth': [3, 5, 7],
                         'min_child_weight': [0.1],
                         'n_estimators': [50, 100, 200], 'random_state': [0],
                         'reg_alpha': [0], 'reg_lambda': [1]})
display(reg_gs.best_params_)
display(reg_gs.best_score_)
ax = plot_importance(reg_gs.best_estimator_, importance_type="gain")
fig = ax.figure
fig.set_size_inches(250, 250)
ax.figure.set_size_inches(18,18)
{'gamma': 0,
 'learning_rate': 0.1,
 'max_delta_step': 0,
 'max_depth': 5,
 'min_child_weight': 0.1,
 'n_estimators': 50,
 'random_state': 0,
 'reg_alpha': 0,
 'reg_lambda': 1}
0.36250088820449333
예측y_pred3 = reg_gs.predict(x_test)
계산과 정확한 라벨의 오차y_true = pd.read_csv(path+"kikou5test.csv")
preds = pd.DataFrame({"pred3": y_pred3})
df_out = pd.concat([y_true, preds], axis=1)
df_out.head()
Id  date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  ... avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   pred3   loss3
0   100 1966    9.6 21.6    1.2 99.9    150.4   7.0 6.6 13.6    ... 4.9 19.1    -4.0    43.8    162.6   5.1 5.0 30  29.816103   0.033818
RMSE df_out["loss3"] = (df_out.kaika - df_out.pred3)**2
df_out.iloc[:, -3:].mean()
kaika    24.909091
pred3    26.849123
loss3    23.966188
dtype: float64
from sklearn.metrics import mean_squared_error, mean_absolute_error
#RMSE
rmse_kaika = np.sqrt(mean_squared_error(df_out.kaika, df_out.pred3))
rmse_kaika
4.895527368155607
벚꽃 개화의 예측 정밀도는 5일이 부족하다.의외로 예측할 수 있지만 미묘하다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(xgboost로 벚꽃 개화 예측), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/corocoro541/items/8ca50720993fada4b378
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
미묘한 결과다.위 AI 벚꽃의 예측이 우수하다는 것을 깨달았다.
 개화 시기 영향이 가장 큰 요인은 연평균 기온, 7월 일조시간, 8월 강우량, 10월 최저기온이다.
연평균 기온은 알 수 있지만 7월의 일조 시간, 8월의 강우량, 10월의 최저 기온은 의외였다.
3. 데이터 소스 
https://www.data.jma.go.jp/gmd/risk/obsdl/index.php 
가공 후 상기 기상청의 데이터를 이용했다.
4. 코드 설명 import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from xgboost import XGBRegressor, plot_importance
from sklearn.model_selection import GridSearchCV, KFold
from tqdm import tqdm_notebook
path="./"
train = pd.read_csv(path+"kikou5train.csv")
x_test = pd.read_csv(path+"kikou5test.csv")
y_train = train.kaika.copy()
x_train = train.drop("kaika", axis=1)
train_id = x_train.Id
x_train.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
Id                                                                                  
1   1961    8.2 21.9    -0.4    106.6   181.1   6.7 6.3 14.9    26.0    ... 3.8 5.9 24.5    -2.6    13.5    195.0   4.5 4.1 NaN 193.6
2   1962    8.2 18.8    -0.8    65.5    189.8   6.3 4.7 14.1    24.5    ... 2.0 4.8 15.3    -4.1    21.3    199.9   4.1 4.9 NaN 182.3
잇닿다df = pd.concat([x_train, x_test])
df.head()
특징량 공사 연평균 기온 추가df["TotalInc"] = df.avtmp3 + df.avtmp4 + df.avtmp5 + df.avtmp6 + df.avtmp7 + df.avtmp8 + df.avtmp9 + df.avtmp10 + df.avtmp11 + df.avtmp12 + df.avtmp13 + df.avtmp14 #平均温度の
 df.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
0   1980    8.2 21.2    1.3 173.5   157.5   6   6.2 13.6    24  ... 2.9 5.3 17.2    -3.5    38  157.3   4.6 5.5 NaN 183.4
1 rows × 87 columns
 x_train = df[df.Id.isin(train_id)].set_index("Id")
x_test = df[~df.Id.isin(train_id)].set_index("Id")
최적 매개 변수 검색random_state = 0
params = {
          "learning_rate": [0.01, 0.05, 0.1],
          "min_child_weight": [0.1],
          "gamma": [0],
          "reg_alpha": [0],
          "reg_lambda": [1],
          "max_depth": [3, 5, 7],
          "max_delta_step": [0],
          "random_state": [random_state],
          "n_estimators": [50, 100, 200],
          }
reg = XGBRegressor()
cv = KFold(n_splits=3, shuffle=True, random_state=random_state)
reg_gs = GridSearchCV(reg, params, cv=cv)
reg_gs.fit(x_train, y_train)
GridSearchCV(cv=KFold(n_splits=3, random_state=0, shuffle=True),
             estimator=XGBRegressor(base_score=None, booster=None,
                                    colsample_bylevel=None,
                                    colsample_bynode=None,
                                    colsample_bytree=None, gamma=None,
                                    gpu_id=None, importance_type='gain',
                                    interaction_constraints=None,
                                    learning_rate=None, max_delta_step=None,
                                    max_depth=None, min_child_weight=None,
                                    missing=nan, monoto...
                                    num_parallel_tree=None, random_state=None,
                                    reg_alpha=None, reg_lambda=None,
                                    scale_pos_weight=None, subsample=None,
                                    tree_method=None, validate_parameters=None,
                                    verbosity=None),
             param_grid={'gamma': [0], 'learning_rate': [0.01, 0.05, 0.1],
                         'max_delta_step': [0], 'max_depth': [3, 5, 7],
                         'min_child_weight': [0.1],
                         'n_estimators': [50, 100, 200], 'random_state': [0],
                         'reg_alpha': [0], 'reg_lambda': [1]})
display(reg_gs.best_params_)
display(reg_gs.best_score_)
ax = plot_importance(reg_gs.best_estimator_, importance_type="gain")
fig = ax.figure
fig.set_size_inches(250, 250)
ax.figure.set_size_inches(18,18)
{'gamma': 0,
 'learning_rate': 0.1,
 'max_delta_step': 0,
 'max_depth': 5,
 'min_child_weight': 0.1,
 'n_estimators': 50,
 'random_state': 0,
 'reg_alpha': 0,
 'reg_lambda': 1}
0.36250088820449333
예측y_pred3 = reg_gs.predict(x_test)
계산과 정확한 라벨의 오차y_true = pd.read_csv(path+"kikou5test.csv")
preds = pd.DataFrame({"pred3": y_pred3})
df_out = pd.concat([y_true, preds], axis=1)
df_out.head()
Id  date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  ... avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   pred3   loss3
0   100 1966    9.6 21.6    1.2 99.9    150.4   7.0 6.6 13.6    ... 4.9 19.1    -4.0    43.8    162.6   5.1 5.0 30  29.816103   0.033818
RMSE df_out["loss3"] = (df_out.kaika - df_out.pred3)**2
df_out.iloc[:, -3:].mean()
kaika    24.909091
pred3    26.849123
loss3    23.966188
dtype: float64
from sklearn.metrics import mean_squared_error, mean_absolute_error
#RMSE
rmse_kaika = np.sqrt(mean_squared_error(df_out.kaika, df_out.pred3))
rmse_kaika
4.895527368155607
벚꽃 개화의 예측 정밀도는 5일이 부족하다.의외로 예측할 수 있지만 미묘하다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(xgboost로 벚꽃 개화 예측), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/corocoro541/items/8ca50720993fada4b378
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from xgboost import XGBRegressor, plot_importance
from sklearn.model_selection import GridSearchCV, KFold
from tqdm import tqdm_notebook
path="./"
train = pd.read_csv(path+"kikou5train.csv")
x_test = pd.read_csv(path+"kikou5test.csv")
y_train = train.kaika.copy()
x_train = train.drop("kaika", axis=1)
train_id = x_train.Id
x_train.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
Id                                                                                  
1   1961    8.2 21.9    -0.4    106.6   181.1   6.7 6.3 14.9    26.0    ... 3.8 5.9 24.5    -2.6    13.5    195.0   4.5 4.1 NaN 193.6
2   1962    8.2 18.8    -0.8    65.5    189.8   6.3 4.7 14.1    24.5    ... 2.0 4.8 15.3    -4.1    21.3    199.9   4.1 4.9 NaN 182.3
잇닿다df = pd.concat([x_train, x_test])
df.head()
특징량 공사 연평균 기온 추가df["TotalInc"] = df.avtmp3 + df.avtmp4 + df.avtmp5 + df.avtmp6 + df.avtmp7 + df.avtmp8 + df.avtmp9 + df.avtmp10 + df.avtmp11 + df.avtmp12 + df.avtmp13 + df.avtmp14 #平均温度の
 df.head()
date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  maxtmp4 ... kumo13  avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   TotalInc
0   1980    8.2 21.2    1.3 173.5   157.5   6   6.2 13.6    24  ... 2.9 5.3 17.2    -3.5    38  157.3   4.6 5.5 NaN 183.4
1 rows × 87 columns
 x_train = df[df.Id.isin(train_id)].set_index("Id")
x_test = df[~df.Id.isin(train_id)].set_index("Id")
최적 매개 변수 검색random_state = 0
params = {
          "learning_rate": [0.01, 0.05, 0.1],
          "min_child_weight": [0.1],
          "gamma": [0],
          "reg_alpha": [0],
          "reg_lambda": [1],
          "max_depth": [3, 5, 7],
          "max_delta_step": [0],
          "random_state": [random_state],
          "n_estimators": [50, 100, 200],
          }
reg = XGBRegressor()
cv = KFold(n_splits=3, shuffle=True, random_state=random_state)
reg_gs = GridSearchCV(reg, params, cv=cv)
reg_gs.fit(x_train, y_train)
GridSearchCV(cv=KFold(n_splits=3, random_state=0, shuffle=True),
             estimator=XGBRegressor(base_score=None, booster=None,
                                    colsample_bylevel=None,
                                    colsample_bynode=None,
                                    colsample_bytree=None, gamma=None,
                                    gpu_id=None, importance_type='gain',
                                    interaction_constraints=None,
                                    learning_rate=None, max_delta_step=None,
                                    max_depth=None, min_child_weight=None,
                                    missing=nan, monoto...
                                    num_parallel_tree=None, random_state=None,
                                    reg_alpha=None, reg_lambda=None,
                                    scale_pos_weight=None, subsample=None,
                                    tree_method=None, validate_parameters=None,
                                    verbosity=None),
             param_grid={'gamma': [0], 'learning_rate': [0.01, 0.05, 0.1],
                         'max_delta_step': [0], 'max_depth': [3, 5, 7],
                         'min_child_weight': [0.1],
                         'n_estimators': [50, 100, 200], 'random_state': [0],
                         'reg_alpha': [0], 'reg_lambda': [1]})
display(reg_gs.best_params_)
display(reg_gs.best_score_)
ax = plot_importance(reg_gs.best_estimator_, importance_type="gain")
fig = ax.figure
fig.set_size_inches(250, 250)
ax.figure.set_size_inches(18,18)
{'gamma': 0,
 'learning_rate': 0.1,
 'max_delta_step': 0,
 'max_depth': 5,
 'min_child_weight': 0.1,
 'n_estimators': 50,
 'random_state': 0,
 'reg_alpha': 0,
 'reg_lambda': 1}
0.36250088820449333
예측y_pred3 = reg_gs.predict(x_test)
계산과 정확한 라벨의 오차y_true = pd.read_csv(path+"kikou5test.csv")
preds = pd.DataFrame({"pred3": y_pred3})
df_out = pd.concat([y_true, preds], axis=1)
df_out.head()
Id  date    avtmp3  maxtmp3 mintmp3 ame3    nisho3  joki3   kumo3   avtmp4  ... avtmp14 maxtmp14    mintmp14    ame14   nisho14 joki14  kumo14  kaika   pred3   loss3
0   100 1966    9.6 21.6    1.2 99.9    150.4   7.0 6.6 13.6    ... 4.9 19.1    -4.0    43.8    162.6   5.1 5.0 30  29.816103   0.033818
RMSE df_out["loss3"] = (df_out.kaika - df_out.pred3)**2
df_out.iloc[:, -3:].mean()
kaika    24.909091
pred3    26.849123
loss3    23.966188
dtype: float64
from sklearn.metrics import mean_squared_error, mean_absolute_error
#RMSE
rmse_kaika = np.sqrt(mean_squared_error(df_out.kaika, df_out.pred3))
rmse_kaika
4.895527368155607
벚꽃 개화의 예측 정밀도는 5일이 부족하다.의외로 예측할 수 있지만 미묘하다.
                Reference
이 문제에 관하여(xgboost로 벚꽃 개화 예측), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/corocoro541/items/8ca50720993fada4b378텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)