Data Wrangling
Importing Data Select, Drop & Rename Filter, Sort & Sample Add Columns Cleaning Data Dates & Time Join Data Aggregate & Transform
Data Analysis
Exploring Data Plotting Continuous Variables Plotting Discrete Variables
Machine Learning
Data Preparation Linear Models
Other Tutorials & Content
Learn Python for Data Science Learn Alteryx Blog



Linear Models with Scikit-Learn

Linear Regression

Here we initialise and train a Linear Regression model before using the model to make predictions. Finally we find the intercept and cofficients of the model along with analysing the models performance on test data using mean squared error and mean absolute error metrics.

#Import Library
from sklearn.linear_model import LinearRegression

#Initialise & Fit Model
model = LinearRegression()
model.fit(X_train, y_train)

#Use Model to Make Predictions
y_pred = model.predict(X_test)

#Get Intercept & Coefficients
print(model.intercept_)
coef = pd.DataFrame(model.coef_, X_train.columns, columns=['Coef'])

#Get MSE & MAE
from sklearn.metrics import mean_squared_error, mean_absolute_error
print('MSE:',mean_squared_error(y_test,y_pred))
print('MAE:',mean_absolute_error(y_test,y_pred))

For more detail, see the Scikit-Learn documentation here

Logistic Regression

Here we initialise and train a Logistic Regression model before using the model to make predictions. Finally we find the intercept and cofficients of the model along with analysing the models performance on test data using the Classification Report.

#Import Library
from sklearn.linear_model import LogisticRegression

#Initialise & Train Model
model = LogisticRegression()
model.fit(X_train, y_train)

#Use Model to Make Predictions
y_pred = model.predict(X_test)

#Get the Model Intercept & Coefficients
print('Intercept:',model.intercept_)
coef = pd.DataFrame(model.coef_.reshape(30,1), cols, columns=['Coefficients'])
print(coef)

#Get the Classification Report
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))

For more detail, see the Scikit-Learn documentation here