Sklearn SGDClassifier - Training a Classification Model With Stochastic Gradient Descent
Python
1| from sklearn.linear_model import SGDClassifier 2| from sklearn.metrics import classification_report, log_loss, roc_auc_score 3| 4| # Step 1: Initialise and fit SGD Classifier model 5| model = SGDClassifier(max_iter=100, 6| tol=1e-3, 7| n_jobs=-1, 8| learning_rate='optimal', 9| n_iter_no_change=10, 10| random_state=101) 11| model.fit(X_train, y_train) 12| 13| # Step 2: Output feature coefficients and number of iterations 14| # trained for before stopping 15| coef = dict(zip(X_train.columns, model.coef_.T)) 16| print('Feature Coefficients:',coef) 17| print('Number of Iterations:',model.n_iter_) 18| 19| # Step 3: Make predictions for test data & evaluate performance 20| y_pred = model.predict(X_test) 21| print('Classification Report:',classification_report(y_test, y_pred)) 22| print('Log Loss:',log_loss(y_test, y_pred)) 23| print('ROC AUC:',roc_auc_score(y_test, y_pred))
149
132
127
119