<jupyter_start><jupyter_text>>> import DF <<<jupyter_code>## 데이터 불러오기
df = pd.read_csv('df_all.csv')
df
len(np.unique(df['district'])), len(np.unique(df['code']))
## 불러온 dataframe에서 상권코드, 연도 제거하기 / 독립 종속 변수 분리하기
y = df[['sales_total']]
X = df[df.columns[1:]]
X.drop(["year","code","sales_total","district","xmap","ymap","C","H"],axis=1,inplace=True)
X
X.columns<jupyter_output><empty_output><jupyter_text>T-sne+LR<jupyter_code>## Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
len(X_train)
X_train.tail()
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, 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)
print(R2_lr)
print(MAE_lr)
print(MSE_lr)
print(RMSE_lr)
import pickle
with open('pkl_file/R2_lr.pkl', 'wb') as f:
    pickle.dump(R2_lr, f) #피클파일 저장
with open('pkl_file/MAE_lr.pkl', 'wb') as f:
    pickle.dump(MAE_lr, f) #피클파일 저장
with open('pkl_file/MSE_lr.pkl', 'wb') as f:
    pickle.dump(MSE_lr, f) #피클파일 저장

with open('pkl_file/RMSE_lr.pkl', 'wb') as f:
    pickle.dump(RMSE_lr, f) #피클파일 저장<jupyter_output>[0.2954119384227951, 0.30869879408396794, 0.28733380579498347, 0.2595207929645317, 0.27844436477725176, 0.3133399125861718, 0.28768749866705146, 0.27258583814560255, 0.30931920039465166, 0.2982099283865438]
[716232538.2561558, 709510227.8483752, 714322714.7021623, 714611257.5218986, 714118479.3545312, 699503742.3834443, 711681880.026313, 711267447.3334944, 706670602.8198372, 717880327.0319976]
[1.9542018494281252e+18, 1.9634841012029292e+18, 1.9720352128197722e+18, 2.074891512616211e+18, 2.2023705763439839e+18, 1.9132707749835046e+18, 2.147252071052736e+18, 1.8549002172487928e+18, 1.9154082322104397e+18, 2.069987287290742e+18]
[1397927698.2119372, 1401243769.3716712, 1404291712.1523478, 1440448372.075935, 1484038603.3873863, 1383210314.8052015, 1465350494.2684314, 1361947215.2946284, 1383982742.743001, 1438745039.0151627]<jupyter_text>T-sne + Random Forest<jupyter_code>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('pkl_file/R2_RF.pkl', 'wb') as f:
    pickle.dump(R2_RF, f) #피클파일 저장
with open('pkl_file/MAE_RF.pkl', 'wb') as f:
    pickle.dump(MAE_RF, f) #피클파일 저장
    
with open('pkl_file/MSE_RF.pkl', 'wb') as f:
    pickle.dump(MSE_RF, f) #피클파일 저장

with open('pkl_file/RMSE_RF.pkl', 'wb') as f:
    pickle.dump(RMSE_RF, f) #피클파일 저장
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

cv = KFold(10, shuffle=True, random_state=0)
cross_val_score(model, Xs_test, y_test, n_jobs=-1, cv=cv)<jupyter_output><empty_output><jupyter_text>tsne X 업종코드 + LR<jupyter_code>a=pd.get_dummies(df["code"])
X1 = pd.concat([X, a],axis=1)
X1
X1
## Train-test split
X_train, X_test, y_train, y_test = train_test_split(X1, y, test_size=0.2, random_state=0)
from sklearn.metrics import r2_score
R2s_lr = []
MAEs_lr = []
MSEs_lr = []
RMSEs_lr = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X1, 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)
    R2s_lr.append(r2_score(y_test , y_pred))
    MAEs_lr.append(metrics.mean_absolute_error(y_test, y_pred))
    MSEs_lr.append(metrics.mean_squared_error(y_test, y_pred))
    RMSEs_lr.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    print('='*50)
    print('='*50)
print(R2s_lr)
print(MAEs_lr)
print(MSEs_lr)
print(RMSEs_lr)
import pickle
with open('pkl_file/R2s_lr.pkl', 'wb') as f:
    pickle.dump(R2s_lr, f) #피클파일 저장
with open('pkl_file/MAEs_lr.pkl', 'wb') as f:
    pickle.dump(MAEs_lr, f) #피클파일 저장
with open('pkl_file/MSEs_lr.pkl', 'wb') as f:
    pickle.dump(MSEs_lr, f) #피클파일 저장
with open('pkl_file/RMSEs_lr.pkl', 'wb') as f:
    pickle.dump(RMSEs_lr, f) #피클파일 저장
with open('pkl_file/R2_lr.pkl', 'rb') as f:
    data = pickle.load(f) # 단 한줄씩 읽어옴
data<jupyter_output><empty_output><jupyter_text>tsne+업종코드+RF<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
R2s_RF = []
MAEs_RF = []
MSEs_RF = []
RMSEs_RF = []
for i in range(10):
    # Train-test split
    X_train, X_test, y_train, y_test = train_test_split(X1, 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))

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

with open('pkl_file/RMSEs_RF.pkl', 'wb') as f:
    pickle.dump(RMSE_RF, f) #피클파일 저장<jupyter_output>[0.6670353796327335, 0.6592087177965658, 0.6232520912499382, 0.6564740923496729, 0.6637486050651222, 0.6433463180476262, 0.6556021005293436, 0.6575578211854567, 0.6534624863462293, 0.6561110138842279]
[470249085.2528592, 462340928.49387217, 469077365.85689783, 467963232.25159055, 466441719.4324085, 465894660.588373, 464948668.04484624, 470939688.8788437, 469230333.48345953, 465812881.1054873]
[9.689917930949695e+17, 8.961524221829281e+17, 1.0965046534614804e+18, 8.95683464275106e+17, 9.24419474285832e+17, 9.908346054818679e+17, 9.919598140665501e+17, 9.600122816057868e+17, 9.495249502892251e+17, 9.456187699155715e+17]
[984373807.6030718, 946653274.5324067, 1047141181.2461014, 946405549.5796218, 961467354.7686535, 995406753.7855406, 995971793.8107234, 979802164.5239342, 974435708.6484593, 972429313.5830344]
StarcoderdataJupyterStructuredCleanDedup