# Sklearn SGDRegressor - Training a Regression Model With Stochastic Gradient Descent

Python

``` 1|  from sklearn.linear_model import SGDRegressor
2|  from sklearn.metrics import mean_squared_error, mean_absolute_error, max_error, explained_variance_score, mean_absolute_percentage_error
3|
4|  # Step 1: Initialise and fit SGD regression model
5|  model = SGDRegressor(max_iter=20000,
6|                       tol=1e-3,
7|                       learning_rate='invscaling',
8|                       n_iter_no_change=5,
9|                       random_state=101)
10|  model.fit(X_train, y_train)
11|
12|  # Step 2: Output feature coefficients and number of iterations
13|  # trained for before stopping
14|  coef = dict(zip(X_train.columns, model.coef_.T))
15|  print('Feature Coefficients:',coef)
16|  print('Number of Iterations:',model.n_iter_)
17|
18|
19|  # make prediction for test data & evaluate performance
20|  y_pred = model.predict(X_test)
21|  print('RMSE:',mean_squared_error(y_test, y_pred, squared = False))
22|  print('MAE:',mean_absolute_error(y_test, y_pred))
23|  print('MAPE:',mean_absolute_percentage_error(y_test, y_pred))
24|  print('Max Error:',max_error(y_test, y_pred))
25|  print('Explained Variance Score:',explained_variance_score(y_test, y_pred))
```
Did you find this snippet useful?