Skip to content

Instantly share code, notes, and snippets.

@yvki
Created April 22, 2024 05:59
Show Gist options
  • Save yvki/de0be682367c3189c34421c083308bf7 to your computer and use it in GitHub Desktop.
Save yvki/de0be682367c3189c34421c083308bf7 to your computer and use it in GitHub Desktop.
Sample Spaceship Titanic πŸš€ scikit-learn logistic regression pipeline with GridSearch hyperparameter tuning βš™οΈ
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
pipe_lgrg = make_pipeline(
PolynomialFeatures(include_bias=False),
StandardScaler(),
SelectPercentile(score_func=f_classif),
LogisticRegression(max_iter=2000, random_state=22)
)
param_grid_lgrg = {
'polynomialfeatures__degree': [2],
'logisticregression__C': [0.1],
'selectpercentile__percentile': [45]
}
grid_search_lgrg = GridSearchCV(estimator=pipe_lgrg, param_grid=param_grid_lgrg, cv=5, scoring='accuracy', n_jobs=-1)
grid_search_lgrg.fit(X_train, y_train)
print("Best Params:", grid_search_lgrg.best_params_)
accuracy_train = grid_search_lgrg.best_estimator_.score(X_train, y_train)
print("Accuracy on train set:", accuracy_train)
accuracy_test = grid_search_lgrg.best_estimator_.score(X_test, y_test)
print("Accuracy on test set:", accuracy_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment