Skip to content

Instantly share code, notes, and snippets.

View risenW's full-sized avatar

Rising Odegua risenW

View GitHub Profile
#get data for classification task
X_train, X_val, y_train, y_val = get_split_data(german_cred, target_name='bad_credit')
#Train and fit these models
rand_forest_cf.fit(X_train, y_train)
extra_tree_cf.fit(X_train, y_train)
bagging_meta_cf.fit(X_train, y_train)
#check their performance
print("ACC of Random Forest is : ", get_acc(rand_forest_cf.predict(X_val), y_val))
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold, cross_val_score
# def get_mae(pred, target):
# return mean_absolute_error(true, pred)
def cross_validate(model, nfolds, feats, targets):
score = -1 * (cross_val_score(model, feats, targets, cv=nfolds, scoring='neg_mean_absolute_error'))
return np.mean(score)
from sklearn.linear_model import LinearRegression
lr_model = LinearRegression()
print("MAE Sccore: ", cross_validate(lr_model, 10, X_train, y_train))
from sklearn.tree import DecisionTreeRegressor
dt_model = DecisionTreeRegressor(max_depth=6, min_samples_leaf=2)
print("MAE Sccore: ", cross_validate(dt_model, 10, X_train, y_train))
from sklearn.neighbors import KNeighborsRegressor
knn_model = KNeighborsRegressor(n_neighbors=60)
print("MAE Sccore: ", cross_validate(knn_model, 10, X_train, y_train))
from sklearn.ensemble import RandomForestRegressor
rf_model = RandomForestRegressor(n_estimators=100, max_depth=5)
print("MAE Sccore: ", cross_validate(rf_model, 10, X_train, y_train))
from sklearn.ensemble import ExtraTreesRegressor
et_model = ExtraTreesRegressor(n_estimators=100, max_depth=5)
print("MAE Sccore: ", cross_validate(et_model, 10, X_train, y_train))
from sklearn.ensemble import BaggingRegressor
bgg_model = BaggingRegressor(et_model, n_estimators=20, random_state=2)
print("MAE Sccore: ", cross_validate(bgg_model, 10, X_train, y_train))
from sklearn.ensemble import AdaBoostRegressor
ada_model = AdaBoostRegressor(et_model, n_estimators=150)
print("MAE Sccore: ", cross_validate(ada_model, 10, X_train, y_train))
from sklearn.ensemble import GradientBoostingRegressor
gb_model = GradientBoostingRegressor(n_estimators=100)
print("MAE Sccore: ", cross_validate(gb_model, 10, X_train, y_train))