Skip to content

Instantly share code, notes, and snippets.

@mertbozkir
Last active May 31, 2021 21:40
Show Gist options
  • Save mertbozkir/722aac4899fefe2d929f9c8d7a737312 to your computer and use it in GitHub Desktop.
Save mertbozkir/722aac4899fefe2d929f9c8d7a737312 to your computer and use it in GitHub Desktop.
Function that run ML Algorithms easily
# Function that runs the requested algorithm and returns the accuracy metrics
def fit_ml_algo(algo, X_train, y_train, cv):
# One Pass
model = algo.fit(X_train, y_train)
acc = round(model.score(X_train, y_train) * 100, 2)
# Cross Validation
train_pred = model_selection.cross_val_predict(algo,
X_train,
y_train,
cv = cv,
n_jobs = -1)
# Cross-Validatiob accuracy metric
acc_cv = round(metrics.accuracy_score(y_train, train_pred) * 100, 2)
return train_pred, acc, acc_cv
start_time = time.time()
train_pred_log, acc_log, acc_cv_log = fit_ml_algo(LogisticRegression(),
X_train,
y_train,
10)
log_time = (time.time() - start_time)
print("Accuracy: %s " % acc_log)
print("Accuracy CV 10-Fold: %s" % acc_cv_log)
print("Running Time: %s" % datetime.timedelta(seconds=log_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment