[Python] Linear Regression with scikit-learn

scikit-learn is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms including support-vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerical and scientific libraries NumPy and SciPy.

Scale/Normalize the Training Data

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_norm = scaler.fit_transform(X_train)

Create and Fit the Regression Model

from sklearn.linear_model import SGDRegressor

# Stochastic Gradient Descent Regressor
sgdr = SGDRegressor(max_iter=1000)
sgdr.fit(X_norm, y_train)

View Parameters

b_norm = sgdr.intercept_
w_norm = sgdr.coef_

Make Predictions

# make a prediction using sgdr.predict()
y_pred_sgd = sgdr.predict(X_norm)
# make a prediction using w,b. 
y_pred = np.dot(X_norm, w_norm) + b_norm  

 

Last Updated on 2023/08/16 by A1go

目錄
Bitnami