Machine learning beginners try linear regression

Introduction

This is the third time following. This time I would like to do linear regression. As usual, it is implemented in Python, detailed explanation is impossible, so please go to another site (laugh) It feels like it's getting messy, but please watch with warm eyes.

The point

There are two terms:

Mean squared error

In the case of linear regression, a line of y = Θ_1x + Θ_2 is drawn for the distributed data, and drawing that line to take the error from the actual data is called mean square error.

In the sample, it is defined as follows.

T.dot () is a function of the inner product. It is the sum of the intercepts minus y of the actual data, squared, totaled, and divided by the number of training data (m).

#Cost function
j = T.sum(T.sqr(t[0] + T.dot(data, t[1:]) - target)) / (2 * m)

The image of the figure is Please refer to the Machine Learning Algorithm Implementation Series [Linear Regression]! !!

Gradient method

The gradient method is a method for correcting Θ_1 and Θ_2 of y = Θ_1x + Θ_2 in the direction that will be more correct.

The code is as follows. It defines T.grad () that differentiates a shared variable (np.array ([0,0])) called t (= theta = Θ). Partially differentiate the mean square error function defined above with respect to Θ1 and Θ2, respectively. Then define a function called train () and update the value of t with ʻupdates = ({})` on every run.

#Partial differential
dt = T.grad(cost=j, wrt=t)

#Gradient method(Update of Θ)
train = theano.function(
    inputs  = [],
    outputs = [j],
    updates = ({t: t - (alpha*dt)})
    )

The image is [4th Gradient Descent Method](https://github.com/levelfour/machine-learning-2014/wiki/%E7%AC%AC4%E5%9B%9E ---% E5 % 8B% BE% E9% 85% 8D% E6% B3% 95% EF% BC% 88% E6% 9C% 80% E6% 80% A5% E9% 99% 8D% E4% B8% 8B% E6% B3 Please refer to% 95% EF% BC% 89)! !!

Sample (python)

Show me the code and put it on for people. I use a library called theano for implementation, so if you don't understand, please gg! I don't apply the code per prediction (I will update the code if I have time)

# -*- coding: utf-8 -*-

import numpy as np
import theano
import theano.tensor as T

class Regression:

    def __init__(self):
        self.t = None


    def fit(self, data, target, alpha=0.1):
        #Calculate the length of the explanatory variable
        if isinstance(data[0], (np.ndarray, np.generic)):
            m = len(data[0])
        else:
            m = len(data)

        #Shared variables
        t = theano.shared(np.zeros(m+1), name='theta')

        #Cost function
        j = T.sum(T.sqr(t[0] + T.dot(data, t[1:]) - target)) / (2 * m)
        #Partial differential
        dt = T.grad(cost=j, wrt=t)
        #Gradient method(Update of Θ)
        train = theano.function(
            inputs  = [],
            outputs = [j],
            updates = ({t: t - (alpha*dt)})
            )
        #Learning
        for i in range(100):
            train()


if __name__ == '__main__':

    from sklearn import datasets

    iris = datasets.load_iris()

    reg = Regression()
    reg.fit(data=iris.data, target=iris.target)

reference

The following site was very helpful

Recommended Posts

Machine learning beginners try linear regression
Machine learning linear regression
Machine Learning: Supervised --Linear Regression
Machine learning algorithm (generalization of linear regression)
<Course> Machine Learning Chapter 1: Linear Regression Model
Machine learning algorithm (linear regression summary & regularization)
Machine learning logistic regression
EV3 x Python Machine Learning Part 2 Linear Regression
Machine learning beginners tried RBM
Understand machine learning ~ ridge regression ~.
Supervised machine learning (classification / regression)
Try machine learning with Kaggle
Machine learning stacking template (regression)
Machine learning algorithm (logistic regression)
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Machine learning beginners try to make a decision tree
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Try to evaluate the performance of machine learning / regression model
[Machine learning] Try studying decision trees
Machine learning summary by Python beginners
Machine learning algorithm (multiple regression analysis)
Machine learning algorithm (simple regression analysis)
Classification and regression in machine learning
Machine learning
Try machine learning with scikit-learn SVM
Machine learning beginners try to reach out to Naive Bayes (2) --Implementation
<For beginners> python library <For machine learning>
Linear regression
[Machine learning] Try studying random forest
Linear regression (for beginners) -Code edition-
Machine Learning: Supervised --Linear Discriminant Analysis
Machine learning beginners try to reach out to Naive Bayes (1) --Theory
[Machine learning] Understanding linear simple regression from both scikit-learn and mathematics
[Machine learning] Understanding linear multiple regression from both scikit-learn and mathematics
Machine learning beginners take Coursera's Deep learning course
<Course> Machine Learning Chapter 3: Logistic Regression Model
First Steps for Machine Learning (AI) Beginners
Machine learning with python (2) Simple regression analysis
<Course> Machine Learning Chapter 2: Nonlinear Regression Model
Stock price forecast using machine learning (regression)
[Machine learning] Regression analysis using scikit learn
[Python] [Machine learning] Beginners without any knowledge try machine learning for the time being
(Machine learning) I tried to understand Bayesian linear regression carefully with implementation.
[Memo] Machine learning
Machine learning classification
Machine Learning sample
Recommended study order for machine learning / deep learning beginners
Try to forecast power demand by machine learning
List of links that machine learning beginners are learning
[Python3] Let's analyze data using machine learning! (Regression)
Try using Jupyter Notebook of Azure Machine Learning
[For beginners] Introduction to vectorization in machine learning
Machine learning tutorial summary
About machine learning overfitting
Linear regression with statsmodels
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Try to predict forex (FX) with non-deep machine learning
Machine learning support vector machine
Studying Machine Learning ~ matplotlib ~