I’ve created these step-by-step machine learning algorith implementations in Python for everyone who is new to the field and might be confused with the different steps.
Check out this page to learn about curious history of Linear Regression.
10 mins
Advanced
Provided by HolyPython.com
I’ve split up Linear Regression implementation to 2 different categories here:
(Red for the actual machine learning work and black font signifies preparation phase)
pandas can be useful for constructing dataframes and scikit learn is the ultimate library for simple machine learning operations, learning and practicing machine learning.
Reading data is simple but there can be important points such as: dealing with columns, headers, titles, constructing data frames etc.
Machine Learning models can be created with a very simple and straight-forward process using scikitlearn. In this case we will create a Linear Regression object from the Linear Regression of scikitlearn.linear_model library.
Once the model is ready, predictions can be done on the test part of the data. Furthermore, I enjoy predicting foreign values that are not in the initial dataset just to observe the outcomes the model creates. .predict method is used for predictions.
We need a nice dataset that’s sensible to analyze with machine learning techniques, particularly linear regression in this case. Scikitlearn has some cool sample data as well.
Even splitting data is made easy with Scikit-learn, for this operation we will use train_test_module from scikitlearn library.
Machine Learning models are generally fit by training data. This is the part where training of the model takes place and we will do the same for our Linear Regression model.
Finally, scikitlearn library’s metrics module is very useful to test the accuracy of the model’s predictions. This part could be done manually as well but metrics module brings lots of functionality and simplicity to the table.
First the import part for libraries:
###Importing Libraries
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split as tts
It’s time to find some data to work with. For the simplicity I will suggest using pre-included datasets library in scikitlearn. They are great for practice and everything is already taken care. So, there won’t be a complication such as missing values or invalid characters etc. while you’re learning.
One thing I’ve been learning is to keep it simple while I’m learning in fields outside my expertise and then step up gradually to avoid burn-out.
Let’s import the diabetes dataset:
###Importing Dataset
X, y = datasets.load_diabetes(return_X_y=True)
Now we can get the data ready:
In case of machine learning algorithms: you usually have feature(s) and an outcome or multiple outcomes to work with, this mean different titles and sometimes different types of data. That’s why DataFrame becomes the perfect structure to work with.
In this regression example, we’re choosing one of the features as to represent X for simplicity. (index 1)
###Constructing Data Frame
X = X[:, np.newaxis, 1] #Choosing one of the features for regression
This is another standard Machine Learning step:
We need to split data so that there are:
It’s a rather simple process (step) thanks to Scikit learn’s train_test_split module.
###Splitting train/test data
X_tr, X_ts, y_tr, y_ts = tts(X,y, test_size=30/100, random_state=None)
Linear Regression can be prone to overfit and regularization parameter can be very useful for further optimization.
You can take a look at this page regarding Regularization parameter in Linear Regression: Linear Regression Optimization Parameters
Now we can create a Linear Regression object and put machine learning to work using the training data:
###Creating Linear Regression Model (OLS)
linreg = linear_model.LinearRegression()
###Training the Model
linreg.fit(X_tr, y_tr)
###Making Predictions
y_pr = linreg.predict(X_ts)
# print(y_pr)
###Evaluating Prediction Accuracy
print('Coefficients: \n', regr.coef_)
print('Mean squared error: %.2f' % mean_squared_error(y_ts, y_pr))
print('Coefficient of determination: %.2f' % r2_score(y_ts, y_pr))
###Making Prediction with Foreign Data
linreg.predict([4.5555])
You can see the full one piece code in this page: Linear Regression Simple Implementation