[PYTHON] Simple regression analysis implementation in Keras

1.First of all

(Caution: The author does not specialize in this field, so there may be lack of explanation or mistakes.) This article messes up the discussion of implementing simple regression analysis in Keras. Jupyter Notebook will be published on GitHub, so you can try it immediately if you have an environment. For simple regression analysis, we refer to Udemy's [Kikagaku style] artificial intelligence / machine learning de-black box course-beginner's edition-. Those who want to see detailed explanations are recommended to attend. [Kikagaku style] Artificial intelligence / machine learning de-black box course-Beginner- https://www.udemy.com/course/kikagaku_blackbox_1/learn/lecture/8258758#overview

Click here for notebook Run SimpleRegressionAnalysis_2.ipynb. The execution environment etc. are described at the beginning of the notebook. https://github.com/moriitkys/SimpleRegressionAnalysis

2. Simple regression analysis miscellaneous commentary

The explanation of simple regression analysis is summarized as follows in an easy-to-understand manner. "One objective variable is predicted by one explanatory variable, and the relationship between the two variables is expressed in the form of a linear equation Y = aX + b. If a (slope) and b (y-intercept) are known, , Y (weight) can be predicted from X (height) " https://www.albert2005.co.jp/knowledge/statistics_analysis/multivariate_analysis/single_regression

Also, this page explains in detail. Thorough explanation of regression analysis (simple regression analysis) in an easy-to-understand manner! https://udemy.benesse.co.jp/ai/regression-analysis.html

Below is a diagram of what to do this time. sra_flow.png

3. Simple regression analysis Keras

Simple regression analysis is performed using a library for neural networks called Keras. It is written in Python and can be executed on Tensorflow. This time it's a simple regression analysis Model creation: model=Sequential() model.add() (Caution: Does not activate) Model initialization model.init() Start learning the model model.fit() Model guess model.prediction() It becomes a flow like. The code is this part.

SimpleRegressionAnalysis.ipynb


# Build model
model = Sequential()
model.add(Dense(1, input_shape=(s, ), use_bias=False))
opt = keras.optimizers.Adam(lr=0.04, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0003)
model.compile(optimizer=opt,
      loss='mean_squared_error',
      metrics=['mae'])

# Start training
history = model.fit(x_normalized, y_normalized, epochs=50, batch_size=20, verbose=1)

It has the following simple form. sra_sra_nodes.png

Therefore, the result of model.summary () also has one parameter. sra_modelsummary.PNG

If it becomes a multiple regression analysis, it will look like the following. sra_mra_nodes.png

As a result of learning, the loss value changed as follows, and it seems that it was fitted immediately. The number of epochs is on the horizontal axis. sra_loss.png

4. Actually run Jupyter Notebook

The model of the result of finding a by modeling by calculation, setting and minimizing the evaluation function, and the result of finding the model by Keras are as follows. sra_result.png

The points plotted in green are the results of guessing by inputting the 98th to 108th RMs of the house price data set into the model of the simple regression analysis created this time. Since it is a straight line model, you can see that it is guessed on the straight line. By the way, when using Keras, I set the data in the range of 0 to 1, but be careful because you can not learn if you enter it normally (you can not learn with this notebook method). Therefore, the value of a is also very different from the one calculated by the formula, but it seems that both are fitted to the data in the same way.

bonus

In order not to forget the original intention, I put a rewrite of the data reading in pandas with a for loop in SimpleRegressionAnalysis_3.ipynb. If you're just starting machine learning and don't know what you're doing here, just for reference.

SimpleRegressionAnalysis_3.ipynb


with open('boston.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        x_orig.append(row[6])
        y_orig.append(row[14])

It is like this. Articles may be updated.

reference https://matplotlib.org/ https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplot.html https://pandas.pydata.org/pandas-docs/stable/reference/frame.html https://scikit-learn.org/stable/ https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_boston.html#sklearn.datasets.load_boston https://seaborn.pydata.org/ https://keras.rstudio.com/articles/tutorial_basic_regression.html https://www.kaggle.com/xgdbigdata/keras-regression-tutorial https://github.com/KatsuhiroMorishita/machine_leaning_samples https://www.udemy.com/course/kikagaku_blackbox_1/learn/lecture/8258758#overview

Recommended Posts

Simple regression analysis implementation in Keras
Simple regression analysis in Python
First simple regression analysis in Python
Regression analysis in Python
Multiple regression analysis with Keras
2. Multivariate analysis spelled out in Python 1-1. Simple regression analysis (scikit-learn)
2. Multivariate analysis spelled out in Python 1-2. Simple regression analysis (algorithm)
Machine learning algorithm (simple regression analysis)
Simple Regression Analysis in High School Mathematics-Verification of Moore's Law
Machine learning with python (2) Simple regression analysis
Poisson regression analysis
Shakedrop's Keras implementation
RNN implementation in python
Write DCGAN in Keras
ValueObject implementation in Python
Merge sort implementation / complexity analysis and experiment in Python
Implemented hard-swish in Keras
2. Multivariate analysis spelled out in Python 7-3. Decision tree [regression tree]
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Calculate the regression coefficient of simple regression analysis with python
Association analysis in Python
Regression analysis with NumPy
SVM implementation in python
[Introduction to Data Scientists] Descriptive Statistics and Simple Regression Analysis ♬
[Reinforcement learning] Explanation and implementation of Ape-X in Keras (failure)
2. Multivariate analysis spelled out in Python 5-3. Logistic regression analysis (stats models)
Implement LSTM AutoEncoder in Keras
Multiple regression expressions in Python
Visualize Keras model in Python 3.5
What is Logistic Regression Analysis?
Axisymmetric stress analysis in Python
Time-series analysis Implementation is stuck-Notes-
Implementation of independent component analysis
Neural network implementation in python
Online linear regression in Python
Implementation of quicksort in Python
Simple IRC client in python
A simple data analysis of Bitcoin provided by CoinMetrics in Python
2. Multivariate analysis spelled out in Python 6-2. Ridge regression / Lasso regression (scikit-learn) [Ridge regression vs. Lasso regression]
[Statistical test 2nd grade / quasi 1st grade] Regression analysis training in Python (1)