House Price Prediction(Deep learning)
이번 프로젝트에서는 기존 머신러닝 모델에 사용했던 데이터와 변수들을 사용하여 조금 더 정확한 정확도를 이끌어 내기 위해 딥러닝과 와이드러닝 등을 활용하였고, 총 6가지 모델을 이용하여 메타러닝을 진행하였습니다. 모델은 XGBoost, lightGBM, Catboost, DNN, Ridge, SVR 을 이용하여 메타러닝을 진행하였습니다.
데이터 전처리
데이터 전처리부분은 앞의 EDA와 Machine learing부분과 동일하기 때문에 별도의 설명은 하지 않고 코드만 작성하겠다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import matplotlib.pyplot as plt import pandas as pd import numpy as np import sklearn import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.preprocessing import RobustScaler from sklearn.metrics import mean_squared_error from xgboost import XGBRegressor from lightgbm import LGBMRegressor from sklearn.model_selection import GridSearchCV import lightgbm as lgb from catboost import CatBoostRegressor, Pool, cv import catboost from keras import models from keras import layers from keras import optimizers from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, TensorBoard from keras import backend as K from keras.layers import Dense, Dropout, BatchNormalization, Activation from keras.regularizers import l2 from sklearn.svm import SVR from sklearn.linear_model import Ridge from sklearn.metrics import mean_absolute_error from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.font_manager as fm from matplotlib import font_manager font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name() # font_name = font_manager.FontProperties(fname="NanumBarunGothic.ttf").get_name() plt.rc('font', family='Malgun Gothic') plt.rcParams["font.family"] = font_name plt.rcParams['axes.unicode_minus'] = False | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | data = pd.read_csv("C:/Users/CPB06GameN/apart/train.csv") data['date'] = data['date'].apply(lambda x: x[0:8]) # 날짜 8자리 추출 data['date_year'] = data['date'].apply(lambda x: x[0:4]).astype('int64') # 년도 추출 data['date_month'] = data['date'].apply(lambda x: x[4:6]).astype('int64') # 월 추출 data['total_rooms'] = data['bedrooms'] + data['bathrooms'] # 침실의 수(bedrooms) + 화장실의 수(bathrooms) data['sqft_total_size'] = data['sqft_above'] + data['sqft_basement'] # 지하실을 제외한 평방 피트(sqft_above) + 지하실의 평방 피트(sqft_basement) data['per_price'] = data["price"] / data['sqft_total_size'] # 집의 가격(per_price) / sqft_total_size years = data['date'].map(lambda x: int(x[:4])) data['house_age'] = years - data['yr_renovated'].astype('int') # 재건축 되지 않은 경우 : 거래년도(yyyy) - 건축년도(yr_built) / 재건축 된 경우 : 거래년도(yyyy) - 재건축년도(yr_renovated) data['yr_renovated'] = data['yr_renovated'].apply(lambda x: np.nan if x == 0 else x) data['yr_renovated'] = data['yr_renovated'].fillna(data['yr_built']) data['is_renovated'] = data['yr_renovated'] - data['yr_built'] data['is_renovated'] = data['is_renovated'].apply(lambda x: 0 if x == 0 else 1) # 재건축 되지 않은 경우 : 1 / 재건축 된 경우 : 0 boolvec = data['is_renovated'] == 0 data['house_age'][boolvec] = years - data['yr_built'] | cs |
1 2 3 4 5 6 7 8 | data_log = data.copy() #데이터 복사 skew_columns = ['sqft_living', 'sqft_above', 'sqft_basement', 'lat', 'sqft_living15', 'price',"sqft_total_size"] for c in skew_columns: data_log[c] = np.log1p(data_log[c].values) data_log.drop("id", axis = 1).hist(bins = 40, figsize = (20, 20)) plt.show() | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | data_log["view_class"] = np.where(data_log["view"] >=1, 1,0) data_log["basement_class"] = np.where(data_log["sqft_basement"] >=1, 1,0) data_log['date_month'] = data_log["date_month"].astype('str') data_log['date_year'] = data_log["date_year"].astype('str') data_log['zipcode'] = data_log["zipcode"].astype('str') dummy_data = data_log[["date_month","date_year","zipcode"]] dummy_col=pd.get_dummies(dummy_data) data_log_dummy=pd.concat([data_log, dummy_col],axis=1) plt.figure(figsize=(15,15)) sns.heatmap(data = data_log.corr(), annot=True, fmt = '.2f', linewidths=.5, cmap='Blues') | cs |
1 2 3 4 5 | X = data_log_dummy.drop(["id","price","date","sqft_basement","yr_built", "zipcode","date_year", "date_month","per_price","is_renovated"],axis=1) # feature y = data_log_dummy[['price']] # target X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=2020, test_size = 0.4) # 6(train) 대 4(test) 비율로 split | cs |
1 2 3 4 5 6 | robuscaler = RobustScaler() X_train_robu = robuscaler.fit_transform(X_train) # 특성 추출, 선택하여 학습 X_test_robu = robuscaler.transform(X_test) # test 데이터에 대해선 특성 추출, 선택만 진행 X_train_df=pd.DataFrame(X_train_robu) X_test_df=pd.DataFrame(X_test_robu) | cs |
XGBOOST튜닝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test1 = { 'max_depth':range(1,10,2), 'min_child_weight':range(5,15,2) } gsearch1 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.1, n_estimators=500, max_depth=5, min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8, tree_method="gpu_hist"), param_grid = param_test1,iid=False, cv=5) gsearch1.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch1.cv_results_, gsearch1.best_params_, gsearch1.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test2 = { 'gamma':[i/100.0 for i in range(7,10)] } gsearch2 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.1, n_estimators=500, max_depth=5, min_child_weight=11, gamma=0, subsample=0.8, colsample_bytree=0.8, tree_method="gpu_hist"), param_grid = param_test2,iid=False, cv=5) gsearch2.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch2.cv_results_, gsearch2.best_params_, gsearch2.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test3 = { 'subsample':[i/10.0 for i in range(6,10)], 'colsample_bytree':[i/10.0 for i in range(6,10)] } gsearch3 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.1, n_estimators=500, max_depth=5, min_child_weight=11, gamma=0.08, subsample=0.8, colsample_bytree=0.8, tree_method="gpu_hist"), param_grid = param_test3,iid=False, cv=5) gsearch3.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch3.cv_results_, gsearch3.best_params_, gsearch3.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test4 = { 'reg_alpha':[1e-2, 1e-1, 1, 10], 'reg_lambda':[1e-2, 1e-1, 1, 10] } gsearch4 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.1, n_estimators=500, max_depth=5, min_child_weight=11, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test4,iid=False, cv=5) gsearch4.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch4.cv_results_, gsearch4.best_params_, gsearch4.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test5 = { 'learning_rate':[0.01,0.05,0.005,0.001], "n_estimators" : [300, 500, 700, 900, 1000] } gsearch5 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.1, n_estimators=500, max_depth=5, min_child_weight=11, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test5,iid=False, cv=5) gsearch5.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch5.cv_results_, gsearch5.best_params_, gsearch5.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test6 = { "n_estimators" : [6000,7000,8000] } gsearch6 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.05, n_estimators=7000, max_depth=5, min_child_weight=11, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test6,iid=False, cv=5) gsearch6.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch6.cv_results_, gsearch6.best_params_, gsearch6.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test7 = { 'max_depth':range(1,10,2), 'min_child_weight':range(3,15,2) } gsearch7 = GridSearchCV(estimator = XGBRegressor(learning_rate =0.05, n_estimators=7000, max_depth=5, min_child_weight=11, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test7,iid=False, cv=5) gsearch7.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch7.cv_results_, gsearch7.best_params_, gsearch7.best_score_ | cs |
1 2 3 4 5 6 7 8 9 | eval_set = [(X_train_robu, y_train["price"]), (X_test_robu, y_test["price"])] eval_metric = ["mae"] XGB_model= XGBRegressor(learning_rate =0.05, n_estimators=7000, max_depth=3, min_child_weight=11, gamma=0.08, subsample=0.7, colsample_bytree=0.6) #tree_method="gpu_hist") history=XGB_model.fit(X_train_robu, y_train["price"], eval_metric=eval_metric, eval_set=eval_set) np.min(XGB_model.evals_result_["validation_1"]["mae"]) | cs |
1 2 | pred_XGB=np.expm1(XGB_model.predict(X_test_robu), dtype=np.float64) pred_XGB.reshape(-1,1) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_XGB_reshape=pred_XGB.reshape(-1,) pred_XGB_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],XGB_model.predict(X_test_robu),ax=ax1) ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") ax1.set(title="XGB_log price") sns.scatterplot(y_test_np_reshape,pred_XGB_reshape,ax=ax2) ax2.set(title="XGB_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
lightGBM튜닝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test11 = { 'min_child_weight':[0.001,0.01,0.1,1,10] } gsearch11 = GridSearchCV(estimator = LGBMRegressor(learning_rate =0.1, n_estimators=500, max_depth=-1,num_leaves =31, objective="regression",min_child_weight=1, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test11,iid=False, cv=5) gsearch11.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch11.cv_results_, gsearch11.best_params_, gsearch11.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test12 = { 'num_leaves':range(30,50,5) } gsearch12 = GridSearchCV(estimator = LGBMRegressor(learning_rate =0.1, n_estimators=500, max_depth=-1,num_leaves =31, objective="regression",min_child_weight=0.01, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test12,iid=False, cv=5) gsearch12.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch12.cv_results_, gsearch12.best_params_, gsearch12.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test13 = { 'n_estimators':[100,200,300] } gsearch13 = GridSearchCV(estimator = LGBMRegressor(learning_rate =0.1, n_estimators=500, max_depth=-1,num_leaves =31, objective="regression",min_child_weight=0.01, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test13,iid=False, cv=5) gsearch13.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch13.cv_results_, gsearch13.best_params_, gsearch13.best_score_ import time target="price" predictors = [x for x in X_train_df.columns if x not in [target]] start = time.time() param_test13 = { 'learning_rate':[0.001,0.005,0.01,0.05,0.1,0.5] } gsearch13 = GridSearchCV(estimator = LGBMRegressor(learning_rate =0.05, n_estimators=200, max_depth=-1,num_leaves =31, objective="regression",min_child_weight=0.01, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist"), param_grid = param_test13,iid=False, cv=5) gsearch13.fit(X_train_df[predictors],y_train[target]) print(time.time() - start) gsearch13.cv_results_, gsearch13.best_params_, gsearch13.best_score_ | cs |
1 2 3 4 5 6 | lgb_model = LGBMRegressor(learning_rate =0.05, n_estimators=200, max_depth=-1,num_leaves =31, objective="regression",min_child_weight=0.001, gamma=0.08, subsample=0.7, colsample_bytree=0.6, tree_method="gpu_hist") history=lgb_model.fit(X_train_robu, y_train["price"], eval_metric=eval_metric, eval_set=eval_set) lgb_model.best_score_ | cs |
1 2 | pred_LGB = np.expm1(lgb_model.predict(X_test_robu)) pred_LGB.reshape(-1,1) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_LGB_reshape=pred_LGB.reshape(-1,) pred_LGB_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],lgb_model.predict(X_test_robu),ax=ax1) ax1.set(title="LGB_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,pred_LGB_reshape,ax=ax2) ax2.set(title="LGB_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
Catboost
1 2 3 4 | Cat_model = catboost.CatBoostRegressor(loss_function='MAE') Cat_model.fit(X_train_robu, y_train, eval_set=(X_test_robu,y_test)) Cat_model.get_best_score() | cs |
1 2 | pred_Cat=np.expm1(Cat_model.predict(X_test_robu)) pred_Cat.reshape(-1,1) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_Cat_reshape=pred_Cat.reshape(-1,) pred_Cat_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],Cat_model.predict(X_test_robu),ax=ax1) ax1.set(title="Catboost_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,pred_Cat_reshape,ax=ax2) ax2.set(title="Catboost_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
DNN
1 2 3 4 5 6 7 8 | epoch = 1500 patient = 50 callbacks1 = [ #EarlyStopping(monitor='val_loss', patience=patient, verbose=1), ModelCheckpoint(filepath="my_model.h5", monitor="val_loss"), ReduceLROnPlateau(monitor = 'val_loss', factor = 0.5, patience = patient, min_lr=0.000001, verbose=1) ] | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | def make_model2(): model = models.Sequential() model.add(layers.Dense(8, activation='relu',input_dim=(X_train_robu.shape[1]))) model.add(layers.Dense(8, activation='relu')) model.add(layers.Dropout(0.15)) model.add(layers.Dense(1)) optimizer = optimizers.RMSprop(lr=0.001) model.compile(optimizer= optimizer, loss = 'mae', metrics=['mae']) return model model2 = make_model2() | cs |
1 2 3 4 5 6 7 | all_mae_histories = [] history1 = model2.fit( X_train_robu, y_train["price"], validation_data=(X_test_robu, y_test["price"]), epochs=epoch, batch_size=128,callbacks=callbacks1) mae_history = history1.history["val_mean_absolute_error"] all_mae_histories.append(mae_history) | cs |
1 | average_mae_history = [np.mean([x[i] for x in all_mae_histories]) for i in range(epoch)] | cs |
1 2 3 4 | plt.plot(range(1, len(average_mae_history) + 1), average_mae_history) plt.xlabel('Epochs') plt.ylabel('Validation MAE') plt.show() | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def smooth_curve(points, factor=0.9): smoothed_points = [] for point in points: if smoothed_points: previous = smoothed_points[-1] smoothed_points.append(previous * factor + point * (1 - factor)) else: smoothed_points.append(point) return smoothed_points smooth_mae_history = smooth_curve(average_mae_history[300:]) plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history) plt.xlabel('Epochs') plt.ylabel('Validation MAE') plt.show() | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | epochs = range(301, 1501) val_loss = history1.history['val_loss'] loss = history1.history['loss'] # ‘b+’는 파란색 덧셈 기호을 의미합니다 plt.plot(epochs, val_loss[300:], 'b+', label='val_loss') # ‘bo’는 파란색 점을 의미합니다 plt.plot(epochs, loss[300:], 'bo', label='loss') plt.xlabel('Epochs') plt.ylabel('Validation loss') plt.legend() plt.show() | cs |
1 2 | pred_DNN=np.expm1(model2.predict(X_test_robu),dtype=np.float64) pred_DNN.reshape(-1,1) | cs |
1 | model2.predict(X_test_robu).reshape(-1,) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_DNN_reshape=pred_DNN.reshape(-1,) pred_DNN_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],model2.predict(X_test_robu).reshape(-1,),ax=ax1) ax1.set(title="DNN_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,pred_DNN_reshape,ax=ax2) ax2.set(title="DNN_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
SVR
1 2 | svr = SVR(C=365, gamma=0.001) svr.fit(X_train_robu, y_train) | cs |
1 2 | pred_SVR=np.expm1(svr.predict(X_test_robu)) pred_SVR.reshape(-1,1) | cs |
1 | mean_absolute_error(y_test, svr.predict(X_test_robu)) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_SVR_reshape=pred_SVR.reshape(-1,) pred_SVR_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],svr.predict(X_test_robu).reshape(-1,),ax=ax1) ax1.set(title="SVR_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,pred_SVR_reshape,ax=ax2) ax2.set(title="SVR_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
Ridge
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | pipe = Pipeline([('featureGen',PolynomialFeatures()), ('Regression', Ridge())]) param_grid = {'Regression': [Ridge()], 'Regression__alpha': [0.0001, 0.001, 0.01, 0.1, 1, 10, 100], 'featureGen': [PolynomialFeatures()], 'featureGen__degree': [1, 2, 3]} grid = GridSearchCV(pipe, param_grid=param_grid, n_jobs=-1) grid.fit(X_train_robu, y_train) print("최상의 교차 검증 정확도: {:.3f}".format(grid.best_score_)) print("훈련 세트 점수: {:.3f}".format(grid.score(X_train_robu, y_train))) print("테스트 세트 점수: {:.3f}".format(grid.score(X_test_robu, y_test))) print("최적의 매개변수:", grid.best_params_) | cs |
1 2 | ridge = Ridge(alpha = 0.1) ridge.fit(X_train_robu,y_train) | cs |
1 2 | pred_Ridge=np.expm1(ridge.predict(X_test_robu)) pred_Ridge.reshape(-1,1) | cs |
1 | mean_absolute_error(y_test, ridge.predict(X_test_robu)) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | pred_Ridge_reshape=pred_Ridge.reshape(-1,) pred_Ridge_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(y_test["price"],ridge.predict(X_test_robu).reshape(-1,),ax=ax1) ax1.set(title="Ridge_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,pred_Ridge_reshape,ax=ax2) ax2.set(title="Ridge_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |
Meta learning
1 2 3 4 5 6 7 8 9 | y_test_np=np.array(np.expm1(y_test),dtype=np.float64) pred_Cat=pred_Cat.reshape(-1,1) pred_LGB=pred_LGB.reshape(-1,1) pred_SVR=pred_SVR.reshape(-1,1) pred_XGB=pred_XGB.reshape(-1,1) | cs |
1 2 | meta_train=np.hstack([pred_Cat,pred_DNN,pred_LGB, pred_Ridge, pred_SVR, pred_XGB]) meta_train | cs |
1 2 | meta_model = Ridge() meta_model.fit(meta_train, y_test_np) | cs |
1 | meta_model.predict(meta_train) | cs |
1 2 | from sklearn.metrics import r2_score r2_score(y_test_np, meta_model.predict(meta_train)) | cs |
1 | mean_absolute_error(np.log1p(y_test_np), np.log1p(meta_model.predict(meta_train))) | cs |
1 2 3 | from sklearn.linear_model import LinearRegression meta_model2 = LinearRegression() meta_model2.fit(meta_train, y_test_np) | cs |
1 | meta_model2.predict(meta_train) | cs |
1 2 | from sklearn.metrics import r2_score r2_score(y_test_np, meta_model2.predict(meta_train)) | cs |
1 | mean_absolute_error(np.log1p(y_test_np), np.log1p(meta_model2.predict(meta_train))) | cs |
1 2 3 | from sklearn.svm import LinearSVR meta_model3 = LinearRegression() meta_model3.fit(meta_train, y_test_np) | cs |
1 | meta_model3.predict(meta_train) | cs |
1 | mean_absolute_error(np.log1p(y_test_np), np.log1p(meta_model3.predict(meta_train))) | cs |
1 2 3 4 5 6 7 8 9 10 | alpha = np.arange(0.001,100,0.01) best_mae = [] for n in alpha: meta_model.set_params(alpha=n) meta_model.fit(meta_train, y_test_np) best_mae.append(mean_absolute_error(np.log1p(y_test_np), np.log1p(meta_model.predict(meta_train)))) plt.title("Effect of alpha") plt.xlabel("alpha") plt.ylabel("mae") plt.plot(alpha, best_mae) | cs |
1 | mean_absolute_error(y_test_np, meta_model.predict(meta_train)) | cs |
1 | mean_absolute_error(np.log1p(y_test_np), np.log1p(meta_model.predict(meta_train))) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | meta_reshape=meta_model.predict(meta_train).reshape(-1,) meta_reshape y_test_np_reshape = y_test_np.reshape(-1,) y_test_np_reshape fig,(ax1,ax2)= plt.subplots(ncols=2) fig.set_size_inches(16,5) sns.scatterplot(np.log1p(y_test_np).reshape(-1,),np.log1p(meta_model.predict(meta_train)).reshape(-1,),ax=ax1) ax1.set(title="Meta_log price") ax1.set(xlabel="실제 집값") ax1.set(ylabel="모델 예측값") sns.scatterplot(y_test_np_reshape,meta_reshape,ax=ax2) ax2.set(title="Meta_price") ax2.set(xlabel="실제 집값") ax2.set(ylabel="모델 예측값") | cs |




















댓글
댓글 쓰기