File size: 7,137 Bytes
f378701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#import the necessary dependencies
import pandas as pd
import numpy as np
import lightgbm as lgb
from lightgbm.callback import early_stopping

from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.metrics import accuracy_score

#load the data
train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")


#clean the data
cleanup_cats = {"MSZoning":     {"A": 1, "C (all)": 2, "FV": 3, "I": 4, "RH": 5, "RL": 6, "RP": 7, "RM": 0},
                "Street":       {"Grvl": 1, "Pave": 0},
                "Alley" :       {"NA": 1, "Grvl": 2, "Pave": 0}, 
                "LotShape":     {"Reg": 1, "IR1": 2, "IR2": 3, "IR3": 0},
                "LandContour":  {"Lvl": 1, "Bnk": 2, "HLS": 3, "Low": 0},
                "Utilities":    {"AllPub": 0, "NoSewr": 3, "NoSeWa" : 2, "ELO": 1},
                "LotConfig":    {"Inside": 1, "Corner": 2, "CulDSac": 3, "FR2": 4, "FR3": 0},
                "LandSlope":    {"Gtl": 1, "Mod": 2, "Sev": 0},
                "Neighborhood": {"Blmngtn": 1, "Blueste": 2, "BrDale": 3, "BrkSide": 4, "ClearCr": 5, "CollgCr": 6, "Crawfor": 7, "Edwards" : 8, "Gilbert": 9, "IDOTRR": 10, "MeadowV": 11, "Mitchel": 12, "NAmes": 13, "NoRidge": 14, "NPkVill": 15, "NridgHt": 16, "NWAmes": 17, "OldTown": 18, "SWISU": 19, "Sawyer": 20, "SawyerW": 21, "Somerst": 22, "StoneBr": 23, "Timber": 24, "Veenker": 0},
                "Condition1":   {"Artery": 1, "Feedr": 2, "Norm": 3, "RRNn": 4, "RRAn": 5, "PosN": 6, "PosA": 7, "RRNe": 8, "RRAe": 0},
                "Condition2":   {"Artery": 1, "Feedr": 2, "Norm": 3, "RRNn": 4, "RRAn": 5, "PosN": 6, "PosA": 7, "RRNe": 8, "RRAe": 0},
                "BldgType":     {"1Fam": 1, "2fmCon": 2, "Duplex": 3, "TwnhsE": 4, "TwnhsI": 5, "Twnhs": 0},
                "HouseStyle":   {"1Story": 1, "1.5Fin": 2, "1.5Unf": 3, "2Story": 4, "2.5Fin": 5, "2.5Unf": 6, "SFoyer": 7, "SLvl": 0},
                "RoofStyle":    {"Flat": 1, "Gable": 2, "Gambrel": 3, "Hip": 4, "Mansard": 5, "Shed": 0},
                "RoofMatl":     {"ClyTile": 1, "CompShg": 2, "Membran": 3, "Metal": 4, "Roll": 5, "Tar&Grv": 6, "WdShake": 7, "WdShngl": 0},
                "Exterior1st":  {"AsbShng": 1, "AsphShn": 2, "BrkComm": 3, "BrkFace": 4, "CBlock": 5, "CemntBd": 6, "HdBoard": 7, "ImStucc": 8, "MetalSd": 9, "Other": 10, "Plywood": 11, "Precast": 12, "Stone": 13, "Stucco": 14, "VinylSd": 15, "WdShing": 16, "Wd Sdng": 0},
                "Exterior2nd":  {"AsbShng": 1, "AsphShn": 2, "Brk Cmn": 3, "BrkFace": 4, "CBlock": 5, "CmentBd": 6, "HdBoard": 7, "ImStucc": 8, "MetalSd": 9, "Other": 10, "Plywood": 11, "Precast": 12, "Stone": 13, "Stucco": 14, "VinylSd": 15, "Wd Shng": 16, "Wd Sdng": 0},
                "MasVnrType":   {"None": 1, "BrkCmn": 2, "BrkFace": 3, "CBlock": 4, "Stone": 0},
                "ExterQual":    {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 0},
                "ExterCond":    {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 0},
                "Foundation":   {"BrkTil": 1, "CBlock": 2, "PConc": 3, "Slab": 4, "Stone": 5, "Wood": 0},
                "BsmtQual":     {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, "NA": 0},
                "BsmtCond":     {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, "NA": 0},
                "BsmtExposure": {"NA": 1, "No": 2, "Mn": 3, "Av": 4, "Gd": 0},
                "BsmtFinType1": {"GLQ": 1, "ALQ": 2, "BLQ": 3, "Rec": 4, "LwQ": 5, "Unf": 6, "NA": 0},
                "BsmtFinType2": {"GLQ": 1, "ALQ": 2, "BLQ": 3, "Rec": 4, "LwQ": 5, "Unf": 6, "NA": 0},
                "Heating":      {"Floor": 1, "GasA": 2, "GasW": 3, "Grav": 4, "OthW": 5, "Wall": 0},
                "HeatingQC":    {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 0},
                "CentralAir":   {"N": 0, "Y": 1},
                "Electrical":   {"SBrkr": 1, "FuseA": 2, "FuseF": 3, "FuseP": 4, "Mix": 0},
                "KitchenQual":  {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 0},
                "Functional":   {"Typ": 1, "Min1": 2, "Min2": 3, "Mod": 4, "Maj1": 5, "Maj2": 6, "Sev": 7, "Sal": 0},
                "FireplaceQu":  {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, "NA": 0},
                "GarageType":   {"2Types": 1, "Attchd": 2, "Basment": 3, "BuiltIn": 4, "CarPort": 5, "Detchd": 6, "NA": 0},
                "GarageFinish": {"NA": 1, "Unf": 2, "RFn": 3, "Fin": 0},
                "GarageQual":   {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, "NA": 0},
                "GarageCond":   {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, "NA": 0},
                "PavedDrive":   {"N": 0, "P": 1, "Y": 2},
                "PoolQC":       {"Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 0},
                "Fence":        {"NA": 1, "MnWw": 2,"GdWo": 3, "MnPrv": 4, "GdPrv": 0},
                "MiscFeature":  {"Elev": 1, "Gar2": 2, "Othr": 3, "Shed": 4, "TenC": 5, "NA": 0},
                "SaleType":     {"WD": 1, "CWD": 2, "VWD": 3, "New": 4, "COD": 5, "Con": 6, "ConLw": 7, "ConLI": 8, "ConLD": 9, "Oth": 0},
                "SaleCondition":{"Normal": 1, "Abnorml": 2, "AdjLand": 3, "Alloca": 4, "Family": 5, "Partial": 0}}


#Drop ID columns
train_data = train_data.drop("Id", axis = 1)
test_data = test_data.drop("Id", axis = 1)

#this is to encode each category with integers
train_data = train_data.replace(cleanup_cats);
test_data = test_data.replace(cleanup_cats);

#remove columns with NaN
removals = train_data.columns[train_data.isnull().any()]
Train_data = train_data.drop(removals, axis = 1)
Test_data = test_data.drop(removals,axis=1 )

#split the training data set
X_train, X_test, y_train, y_test = train_test_split(Train_data.drop('SalePrice', axis=1), Train_data['SalePrice'], test_size=0.2, shuffle = True, random_state=42)

training_data=lgb.Dataset(X_train,label=y_train)
valid_data=lgb.Dataset(X_test,label=y_test)

#parameters taken from optuna training
params = {
    "objective": "regression",
    "n_estimators": 10000,
	"learning_rate": 0.08828308704850689,
	"num_leaves": 256,
	"max_depth": 11,
	"min_data_in_leaf": 100,
	"lambda_l1": 55,
	"lambda_l2": 60,
	"min_gain_to_split": 13.827512822883651,
	"bagging_fraction": 1.0,
	"bagging_freq": 1,
	"feature_fraction": 0.4
    }
model = lgb.train(params,
                 training_data,
                 valid_sets=valid_data,
                 callbacks = [early_stopping(300)]
                 )
def make_a_prediction(M, X):
    prediction = M.predict(X)
    return prediction

predictions = make_a_prediction(model, X_test)
print(predictions)

import shap

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
shap.initjs()
shap.force_plot(explainer.expected_value, shap_values=shap_values, feature_names=X_test.columns)

shap.initjs()
shap.decision_plot(explainer.expected_value, shap_values, feature_names=np.array(X_test.columns))

shap.initjs()
shap.summary_plot(shap_values=shap_values, feature_names=X_test.columns)

interaction_values = explainer.shap_interaction_values(X_test)
interaction_values[0].round(2)
pd.DataFrame(interaction_values[0].round(2)).head(60)