<jupyter_start><jupyter_text>회귀분석 (1010개 상권코드) - 업종코드 X<jupyter_code>import pandas as pd
import numpy as np

df = pd.read_csv('../raw_data/df_grouped_rate.csv') #폴더 위치는 상이할 수 있음
y=df.iloc[:,3:4]
X= df.iloc[:,1:].drop(['sales_total','code'],axis=1)
X['district'] = X['district'].astype('object') 
X_dum = pd.get_dummies(X)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import LinearRegression

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2) 

# 로버스트 스케일링
rb = RobustScaler()
Xs_train = rb.fit_transform(X_train)
Xs_test = rb.transform(X_test)

# 회귀분석
regressor = LinearRegression()
model = regressor.fit(Xs_train, y_train)
# 정확도 결과값
print(model.score(Xs_train, y_train))
print(model.score(Xs_test, y_test))

print('='*50)
print('='*50)
# 오차 결과값
from sklearn import metrics
y_pred = model.predict(Xs_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))  
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))  
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#OLS summary를 보기위해 
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2, random_state=0) # y 값이 정의되지 않아 df.sales_total 으로 대체하겠습니다.
# 로버스트 스케일링
rb = RobustScaler()
Xs_train = rb.fit_transform(X_train)
Xs_test = rb.transform(X_test)

model = sm.OLS(y_train, Xs_train)
result = model.fit()
print(result.summary())<jupyter_output><empty_output><jupyter_text>```                           OLS Regression Results                            ==============================================================================Dep. Variable:            sales_total   R-squared:                       0.334Model:                            OLS   Adj. R-squared:                  0.327Method:                 Least Squares   F-statistic:                     43.98Date:                Sat, 22 Feb 2020   Prob (F-statistic):               0.00Time:                        12:11:50   Log-Likelihood:            -2.0356e+06No. Observations:               90612   AIC:                         4.073e+06Df Residuals:                   89588   BIC:                         4.083e+06Df Model:                        1023                                         Covariance Type:            nonrobust                                         ============================================================================================================================================================Omnibus:                   108173.037   Durbin-Watson:                   2.007Prob(Omnibus):                  0.000   Jarque-Bera (JB):         49901725.822Skew:                           5.883   Prob(JB):                         0.00Kurtosis:                     117.363   Cond. No.                         721.==============================================================================Warnings:[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.```<jupyter_code>from sklearn.metrics import r2_score
R2_lr = []
MAE_lr = []
MSE_lr = []
RMSE_lr = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2) 

    # 로버스트 스케일링
    rb = RobustScaler()
    Xs_train = rb.fit_transform(X_train)
    Xs_test = rb.transform(X_test)

    # 회귀분석
    regressor = LinearRegression()
    model = regressor.fit(Xs_train, y_train)

    print(i+1,"번째 시도")
    # 정확도 결과값
    print(model.score(Xs_train, y_train))
    print(model.score(Xs_test, y_test))

    # 오차 결과값
    y_pred = model.predict(Xs_test)
    R2_lr.append(r2_score(y_test , y_pred))
    MAE_lr.append(metrics.mean_absolute_error(y_test, y_pred))
    MSE_lr.append(metrics.mean_squared_error(y_test, y_pred))
    RMSE_lr.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2_lr)
print(MAE_lr)
print(MSE_lr)
print(RMSE_lr)
import pickle
with open('pkl_files/R2_lr.pkl', 'wb') as f:
    pickle.dump(R2_lr, f) #피클파일 저장
with open('pkl_files/MAE_lr.pkl', 'wb') as f:
    pickle.dump(MAE_lr, f) #피클파일 저장
with open('pkl_files/MSE_lr.pkl', 'wb') as f:
    pickle.dump(MSE_lr, f) #피클파일 저장

with open('pkl_files/RMSE_lr.pkl', 'wb') as f:
    pickle.dump(RMSE_lr, f) #피클파일 저장
#load
with open('pkl_files/R2_lr.pkl', 'rb') as f:
    data = pickle.load(f) # 단 한줄씩 읽어옴
data<jupyter_output><empty_output><jupyter_text>회귀분석 업종코드만<jupyter_code>import pandas as pd
import numpy as np

df = pd.read_csv('../raw_data/df_grouped_rate.csv') #폴더 위치는 상이할 수 있음

y=df.iloc[:,3:4]
X= df.iloc[:,1:].drop(['sales_total','district'],axis=1)
X['code'] = X['code'].astype('object') 
X_dum = pd.get_dummies(X)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import LinearRegression

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2) 

# 로버스트 스케일링
rb = RobustScaler()
Xs_train = rb.fit_transform(X_train)
Xs_test = rb.transform(X_test)

# 회귀분석
regressor = LinearRegression()
model = regressor.fit(Xs_train, y_train)
# 정확도 결과값
print(model.score(Xs_train, y_train))
print(model.score(Xs_test, y_test))

print('='*50)
print('='*50)
# 오차 결과값
from sklearn import metrics
y_pred = model.predict(Xs_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))  
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))  
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#OLS summary를 보기위해 
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2, random_state=0) # y 값이 정의되지 않아 df.sales_total 으로 대체하겠습니다.
# 로버스트 스케일링
rb = RobustScaler()
Xs_train = rb.fit_transform(X_train)
Xs_test = rb.transform(X_test)

model = sm.OLS(y_train, Xs_train)
result = model.fit()
print(result.summary())
from sklearn.metrics import r2_score
R2_lr_s = []
MAE_lr_s = []
MSE_lr_s = []
RMSE_lr_s = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2) 

    # 로버스트 스케일링
    rb = RobustScaler()
    Xs_train = rb.fit_transform(X_train)
    Xs_test = rb.transform(X_test)

    # 회귀분석
    regressor = LinearRegression()
    model = regressor.fit(Xs_train, y_train)

    print(i+1,"번째 시도")
    # 정확도 결과값
    print(model.score(Xs_train, y_train))
    print(model.score(Xs_test, y_test))

    # 오차 결과값
    y_pred = model.predict(Xs_test)
    R2_lr_s.append(r2_score(y_test , y_pred))
    MAE_lr_s.append(metrics.mean_absolute_error(y_test, y_pred))
    MSE_lr_s.append(metrics.mean_squared_error(y_test, y_pred))
    RMSE_lr_s.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2_lr_s)
print(MAE_lr_s)
print(MSE_lr_s)
print(RMSE_lr_s)
import pickle
with open('pkl_files/R2_lr_s.pkl', 'wb') as f:
    pickle.dump(R2_lr_s, f) #피클파일 저장
with open('pkl_files/MAE_lr_s.pkl', 'wb') as f:
    pickle.dump(MAE_lr_s, f) #피클파일 저장
with open('pkl_files/MSE_lr_s.pkl', 'wb') as f:
    pickle.dump(MSE_lr_s, f) #피클파일 저장

with open('pkl_files/RMSE_lr_s.pkl', 'wb') as f:
    pickle.dump(RMSE_lr_s, f) #피클파일 저장<jupyter_output><empty_output><jupyter_text>시군구 OLS 업종코드 없이<jupyter_code>df = pd.read_csv("../raw_data/df_grouped_rate.csv", encoding ='utf-8')
cgoongu = pd.read_csv("../raw_data/cgoongoo1.csv", encoding ='utf-8')
df_merged = pd.merge(df, cgoongu, on=['district'])
df_merged.drop(['district','year'], axis=1, inplace =True)

y=df_merged.iloc[:,1:2]
X= df_merged.drop(['sales_total'],axis=1)
X= X.iloc[:,1:]
X.head(3)
# 더미변수화
X['cgoongoo'] = X['cgoongoo'].astype('object')
X_dum = pd.get_dummies(X)
X_dum.columns[:17]
#OLS summary를 보기위해 
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2, random_state=0) # y 값이 정의되지 않아 df.sales_total 으로 대체하겠습니다.
# 로버스트 스케일링
rb = RobustScaler()
Xs_train = rb.fit_transform(X_train.iloc[:,:17])
Xs_train = np.hstack((Xs_train, X_train.iloc[:,17:]))

Xs_test = rb.transform(X_test.iloc[:,:17])
Xs_test = np.hstack((Xs_test, X_test.iloc[:,17:]))
model = sm.OLS(y_train, Xs_train)
result = model.fit()
print(result.summary())
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn import metrics

R2_cgoongu = []
MAE_cgoongu = []
MSE_cgoongu = []
RMSE_cgoongu = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X_dum, y, test_size=0.2) 

    # 로버스트 스케일링
    rb = RobustScaler()
    Xs_train = rb.fit_transform(X_train)
    Xs_test = rb.transform(X_test)

    # 회귀분석
    regressor = LinearRegression()
    model = regressor.fit(Xs_train, y_train)

    print(i+1,"번째 시도")
    # 정확도 결과값
    print(model.score(Xs_train, y_train))
    print(model.score(Xs_test, y_test))

    # 오차 결과값
    y_pred = model.predict(Xs_test)
    R2_cgoongu.append(r2_score(y_test , y_pred))
    MAE_cgoongu.append(metrics.mean_absolute_error(y_test, y_pred))
    MSE_cgoongu.append(metrics.mean_squared_error(y_test, y_pred))
    RMSE_cgoongu.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2_cgoongu) 
print(MAE_cgoongu) 
print(MSE_cgoongu) 
print(RMSE_cgoongu) 
import pickle
with open('R2_cgoongu.pkl', 'wb') as f:
    pickle.dump(R2_cgoongu, f) #피클파일 저장
with open('MAE_cgoongu.pkl', 'wb') as f:
    pickle.dump(MAE_cgoongu, f) #피클파일 저장
with open('MSE_cgoongu.pkl', 'wb') as f:
    pickle.dump(MSE_cgoongu, f) #피클파일 저장

with open('RMSE_cgoongu.pkl', 'wb') as f:
    pickle.dump(RMSE_cgoongu, f) #피클파일 저장<jupyter_output><empty_output><jupyter_text>Random Forest (상권코드 1010개)<jupyter_code>import pandas as pd 
df = pd.read_csv('../raw_data/df_grouped_rate.csv') #폴더 위치는 상이할 수 있음

# 더미변수화
y=df.iloc[:,3:4]
X= df.iloc[:,1:].drop(['sales_total','code'],axis=1)
X['district'] = X['district'].astype('object') 
# X_dum = pd.get_dummies(X)
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
R2_RF = []
MAE_RF = []
MSE_RF = []
RMSE_RF = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

    Xs_train = X_train
    Xs_test = X_test
    regr = RandomForestRegressor(n_estimators=50,max_depth=20, n_jobs=-1)
    model=regr.fit(Xs_train, y_train)
    y_pred=model.predict(Xs_test)
    
    print(i+1,"번째 시도")
    # 정확도 결과값
    print(model.score(Xs_train, y_train))
    print(model.score(Xs_test, y_test))

    # 오차 결과값
    R2_RF.append(r2_score(y_test , y_pred))
    MAE_RF.append(metrics.mean_absolute_error(y_test, y_pred))
    MSE_RF.append(metrics.mean_squared_error(y_test, y_pred))
    RMSE_RF.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2_RF) 
print(MAE_RF) 
print(MSE_RF) 
print(RMSE_RF) 
import pickle
with open('sample_data/R2_RF.pkl', 'wb') as f:
    pickle.dump(R2_RF, f) #피클파일 저장
with open('sample_data/MAE_RF.pkl', 'wb') as f:
    pickle.dump(MAE_RF, f) #피클파일 저장
    
with open('sample_data/MSE_RF.pkl', 'wb') as f:
    pickle.dump(MSE_RF, f) #피클파일 저장

with open('sample_data/RMSE_RF.pkl', 'wb') as f:
    pickle.dump(RMSE_RF, f) #피클파일 저장<jupyter_output><empty_output><jupyter_text>Random Forest (시군구)<jupyter_code>import pandas as pd

df = pd.read_csv("../raw_data/df_grouped_rate.csv", encoding ='utf-8')
cgoongu = pd.read_csv("../raw_data/cgoongoo1.csv", encoding ='utf-8')
df_merged = pd.merge(df, cgoongu, on=['district'])
df_merged.drop(['district','year'], axis=1, inplace =True)

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
y=df_merged.iloc[:,1:2]
X= df_merged.drop(['sales_total','code'],axis=1)
X['cgoongoo'] = X['cgoongoo'].astype('object') 

from sklearn.ensemble import RandomForestRegressor
from sklearn import metrics
from sklearn.metrics import r2_score
import numpy as np 

R2_RF_c = []
MAE_RF_c = []
MSE_RF_c = []
RMSE_RF_c = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

    Xs_train = X_train
    Xs_test = X_test
    regr = RandomForestRegressor(n_estimators=50,max_depth=20, n_jobs=-1)
    model=regr.fit(Xs_train, y_train)
    y_pred=model.predict(Xs_test)
    
    print(i+1,"번째 시도")
    # 정확도 결과값
    print(model.score(Xs_train, y_train))
    print(model.score(Xs_test, y_test))

    # 오차 결과값
    R2_RF_c.append(r2_score(y_test , y_pred))
    MAE_RF_c.append(metrics.mean_absolute_error(y_test, y_pred))
    MSE_RF_c.append(metrics.mean_squared_error(y_test, y_pred))
    RMSE_RF_c.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2_RF_c) 
print(MAE_RF_c) 
print(MSE_RF_c) 
print(RMSE_RF_c) 
import pickle
with open('pkl_files/R2_RF_c.pkl', 'wb') as f:
    pickle.dump(R2_RF_c, f) #피클파일 저장
with open('pkl_files/MAE_RF_c.pkl', 'wb') as f:
    pickle.dump(MAE_RF_c, f) #피클파일 저장
with open('pkl_files/MSE_RF_c.pkl', 'wb') as f:
    pickle.dump(MSE_RF_c, f) #피클파일 저장

with open('pkl_files/RMSE_RF_c.pkl', 'wb') as f:
    pickle.dump(RMSE_RF_c, f) #피클파일 저장<jupyter_output><empty_output><jupyter_text>LightGBM (1010)<jupyter_code>import pandas as pd 
df = pd.read_csv('../raw_data/df_grouped_rate.csv') #폴더 위치는 상이할 수 있음
y=df.iloc[:,3:4]
X= df.iloc[:,1:].drop(['sales_total','code'],axis=1)
X['district'] = X['district'].astype('object')

# # 더미변수화
X = pd.get_dummies(X)  
# X_dum2 = pd.get_dummies(X.iloc[:,1:])
# X = pd.concat([X_dum1, X_dum2],axis=1)
import lightgbm as lgb
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
from sklearn.model_selection import train_test_split
R2_lgb_gbdt = []
MAE_lgb_gbdt = []
MSE_lgb_gbdt = []
RMSE_lgb_gbdt = []
for i in range(10):
    train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.2)


#     model = lgb.LGBMClassifier(n_estimators=100, max_depth=1, random_state=0)
   # 데이터셋으로 변환
    train_ds = lgb.Dataset(train_x, label = train_y) 
    test_ds = lgb.Dataset(test_x, label = test_y) 
   
  #파라미터 설정
    params = {'learning_rate': 0.01, 
              'max_depth': 16, 
              'boosting': 'gbdt', 
              'objective': 'regression', 
              'metric': 'mse', 
              'is_training_metric': True, 
              'feature_fraction': 0.9, 
              'bagging_fraction': 0.7, 
              'bagging_freq': 5, 
              'device': 'cpu'
             }
    # 훈련
    model = lgb.train(params, train_ds, 1000, test_ds, verbose_eval=100, early_stopping_rounds=100)
#     predict_train = model.predict(train_x)
    y_pred = model.predict(test_x)
    print(i+1,"번째 시도")

    # 오차 결과값
    R2_lgb_gbdt.append(r2_score(test_y , y_pred))
    MAE_lgb_gbdt.append(metrics.mean_absolute_error(test_y, y_pred))
    MSE_lgb_gbdt.append(metrics.mean_squared_error(test_y, y_pred))
    RMSE_lgb_gbdt.append(np.sqrt(metrics.mean_squared_error(test_y, y_pred)))
    print('='*50)
    print('='*50)

print(R2_lgb_gbdt) 
print(MAE_lgb_gbdt) 
print(MSE_lgb_gbdt) 
print(RMSE_lgb_gbdt) 
import pickle
with open('pkl_files/R2_lgb_gbdt_2.pkl', 'wb') as f:
    pickle.dump(R2_lgb_gbdt, f) #피클파일 저장
with open('pkl_files/MAE_lgb_gbdt_2.pkl', 'wb') as f:
    pickle.dump(MAE_lgb_gbdt, f) #피클파일 저장
with open('pkl_files/MSE_lgb_gbdt_2.pkl', 'wb') as f:
    pickle.dump(MSE_lgb_gbdt, f) #피클파일 저장

with open('pkl_files/RMSE_lgb_gbdt_2.pkl', 'wb') as f:
    pickle.dump(RMSE_lgb_gbdt, f) #피클파일 저장<jupyter_output><empty_output><jupyter_text>LightGBM (시군구)<jupyter_code>import pandas as pd 
df = pd.read_csv("../raw_data/df_grouped_rate.csv", encoding ='utf-8')
cgoongu = pd.read_csv("../raw_data/cgoongoo1.csv", encoding ='utf-8')
df_merged = pd.merge(df, cgoongu, on=['district'])
df_merged.drop(['district','year'], axis=1, inplace =True)

y=df_merged.iloc[:,1:2]
X= df_merged.drop(['sales_total','code'],axis=1)

X= X.iloc[:,1:]


X['cgoongoo'] = X['cgoongoo'].astype('object')
# # 더미변수화
X = pd.get_dummies(X)  
import lightgbm as lgb
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
from sklearn.model_selection import train_test_split
R2_lgb_gbdt_c = []
MAE_lgb_gbdt_c = []
MSE_lgb_gbdt_c = []
RMSE_lgb_gbdt_c = []
for i in range(10):
    train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.3)


#     model = lgb.LGBMClassifier(n_estimators=100, max_depth=1, random_state=0)
   # 데이터셋으로 변환
    train_ds = lgb.Dataset(train_x, label = train_y) 
    test_ds = lgb.Dataset(test_x, label = test_y) 
   
  #파라미터 설정
    params = {'learning_rate': 0.01, 
              'max_depth': 16, 
              'boosting': 'gbdt', 
              'objective': 'regression', 
              'metric': 'mse', 
              'is_training_metric': True, 
              'feature_fraction': 0.9, 
              'bagging_fraction': 0.7, 
              'bagging_freq': 5, 
              'device': 'cpu'
             }
    # 훈련
    model = lgb.train(params, train_ds, 1000, test_ds, verbose_eval=100, early_stopping_rounds=100)
#     predict_train = model.predict(train_x)
    y_pred = model.predict(test_x)
    print(i+1,"번째 시도")

    # 오차 결과값
    R2_lgb_gbdt_c.append(r2_score(test_y , y_pred))
    MAE_lgb_gbdt_c.append(metrics.mean_absolute_error(test_y, y_pred))
    MSE_lgb_gbdt_c.append(metrics.mean_squared_error(test_y, y_pred))
    RMSE_lgb_gbdt_c.append(np.sqrt(metrics.mean_squared_error(test_y, y_pred)))
    print('='*50)
    print('='*50)

print(R2_lgb_gbdt_c) 
print(MAE_lgb_gbdt_c) 
print(MSE_lgb_gbdt_c) 
print(RMSE_lgb_gbdt_c) 
import pickle
with open('pkl_files/R2_lgb_gbdt_c2.pkl', 'wb') as f:
    pickle.dump(R2_lgb_gbdt_c, f) #피클파일 저장
with open('pkl_files/MAE_lgb_gbdt_c2.pkl', 'wb') as f:
    pickle.dump(MAE_lgb_gbdt_c, f) #피클파일 저장
with open('pkl_files/MSE_lgb_gbdt_c2.pkl', 'wb') as f:
    pickle.dump(MSE_lgb_gbdt_c, f) #피클파일 저장

with open('pkl_files/RMSE_lgb_gbdt_c2.pkl', 'wb') as f:
    pickle.dump(RMSE_lgb_gbdt_c, f) #피클파일 저장<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1510919385279051, 'fraction_numerical': 0.020167160959827447, 'mean_word_length': 4.038304808475957, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '129902', 'license': 'MIT', 'n_tokens_mistral': 8416, 'n_tokens_neox': 8675, 'n_words': 1332, 'path': 'dayoung_trial1/20_compare_regressions_no_service.ipynb', 'repo_name': 'dayoungMM/Regression_project', 'size': '18545'}