Univariate Linear Regression in Python
Univariate data is the type of data in which the result depends only on one variable. For instance, dataset of points on a line can be considered as a univariate data where abscissa can be considered as input feature and ordinate can be considered as output/result.
For line Y = 2X + 3;
Input feature will be X and Y will be the result.
For univariate linear regression, there is only one input feature vector. The line of regression will be in the form of:
Y = b0 + b1 * X
b0 and b1 are the coefficients of regression.
Hence, it is being tried to predict regression coefficients b0 and b1 by training a model.
predict(x, b0, b1):
"""Predicts the value of prediction based on
current value of regression coefficients when input is x"""
# Y = b0 + b1 * X
- Cost function :
Cost function computes the error percentage with the current value of regression coefficients. It quantitatively defines how far the model is wrt actual regression coefficients which has lowest rate of error.
cost(x, y, b0, b1):
# y is a list of expected value
predict(x, b0, b1)
# Now, we have errors for all the observations,
# for some input, the value of error might be positive
# and for some input might be negative,
# and if we directly add them up,
# the values might cancel out leading to wrong output."
# Hence, we use concept of mean squared error.
# in mse, we return mean of square of all the errors.
- Cost Derivative
After each iteration, the cost is upgraded in proportion to the error. The nature of error is very data sensitive. By data sensitive i mean the error value changes very fast, because we had square in error function. Hence, to make it more tolerant to high values of errors, we derivate the error function.
The mathematics is as follows:
cost_derivative(x, y, b0, b1, i):
(predict(xi, b0, b1)
(predict(xi, b0, b1)
- Update Coefficients :
At each iteration (epoch), the values of the regression coefficient are updated by a specific value wrt to the error from the previous iteration. This updation is very crucial and is the crux of the machine learning applications that you write.
Updating the coefficients with exact an update of a coefficient is done by penalizing its value with a fraction of error that it’s previous values caused.
This fraction is called as learning rate. This defines how fast our model reaches out to point of convergence(point where error is ideally 0).
Python function for the same is as follows:
update_coeff(x, y, b0, b1, i, alpha):
cost_derivative(x, y, b0, b1, i)
- Stop Iterations:
This is the function which is used to specify when should the iterations should stop.
As per user, the algorithm stop_iteration generally returns true in following conditions:
- Max Iteration : Model is trained for a specified number of iterations.
- Error value : Depending upon the value of previous error, the algorithm decides whether to continue or stop.
- Accuracy : Depending upon the last accuracy of the model, if it is larger than the mentioned accuracy, the algorithm returns True,
- Hybrid : This is more often used. This combines more than one above mentioned conditions along with an exceptional break option. Exceptional break is condition where training continues until when something bad happens. Something bad might include overflow of result, time constraints exceeded, etc.
Having all the utility functions defined, lets see the pseudo code followed by its implementation:
Final Oop implementation :
Python Example for Beginners
Two Machine Learning Fields
There are two sides to machine learning:
- Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
- Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes
Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!
Latest end-to-end Learn by Coding Recipes in Project-Based Learning:
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.