[PYTHON] Machine learning linear regression

○ The main points of this article Note that I learned linear regression

Linear regression: ・ Algorithm for predicting regression problems ・ Expressed as a straight line of y = b + ax ・ Find the parameters that minimize the error (loss) of each data and the straight line that is predicted. -Use the mean square error to measure the error (the mean square of the distance between the straight line and each data) -It is possible to model the relationship in which the objective variable becomes larger (smaller) as the explanatory variable becomes larger. ・ Supervised learning

○ Source code (Python)

Linear regression model


#Linear regression
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline

#Training data
X = [[10.0], [8.0], [13.0], [9.0], [11.0], [14.0], [6.0], [4.0], [12.0], [7.0], [5.0]]
y = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]

#Model generation, learning, and evaluation
model = LinearRegression()
model.fit(X, y) #Learning
print(model.coef_) #Slope of a regression line
print(model.intercept_) #Intercept of regression line
print('y = 0.5x + 3')
y_pred = model.predict(X) #Forecast

#graph display
fig, ax = plt.subplots()
plt.xlabel("X")
plt.ylabel("y")
ax.scatter(X, y, color='blue', marker='s', label='data')
plt.plot(X, y_pred, "r-")

result [0.50009091] 3.0000909090909094 y = 0.5x + 3 ダウンロード.png

・ Although the number of data is small, I think it expresses the data as it is. ・ Algorithms for predicting regression problems include support vector machines, regularization, and neural networks, but this algorithm is the easiest to understand. ・ I think it's easy to understand because I learned with linear functions when I was a student. I would like you to add more study of these algorithms to the essential education.

Recommended Posts

Machine learning linear regression
Machine Learning: Supervised --Linear Regression
Machine learning beginners try linear regression
Machine learning logistic regression
Machine learning algorithm (generalization of linear regression)
<Course> Machine Learning Chapter 1: Linear Regression Model
Machine learning algorithm (linear regression summary & regularization)
EV3 x Python Machine Learning Part 2 Linear Regression
Understand machine learning ~ ridge regression ~.
Machine learning
Linear regression
Supervised machine learning (classification / regression)
Machine learning stacking template (regression)
Machine learning algorithm (logistic regression)
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Machine learning algorithm (multiple regression analysis)
Machine learning algorithm (simple regression analysis)
Classification and regression in machine learning
Machine Learning: Supervised --Linear Discriminant Analysis
[Memo] Machine learning
Machine learning classification
Machine Learning sample
<Course> Machine Learning Chapter 3: Logistic Regression Model
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
[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 tutorial summary
About machine learning overfitting
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning support vector machine
Studying Machine Learning ~ matplotlib ~
Machine learning course memo
Machine learning library dlib
(Machine learning) I tried to understand Bayesian linear regression carefully with implementation.
Machine learning (TensorFlow) + Lotto 6
[Python3] Let's analyze data using machine learning! (Regression)
Regression with linear model
Somehow learn machine learning
Supervised learning (regression) 1 Basics
Python: Supervised Learning (Regression)
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Machine learning model considering maintainability
Machine learning learned with Pokemon
Data set for machine learning
Japanese preprocessing for machine learning
Machine learning in Delemas (practice)
An introduction to machine learning
Basics of Machine Learning (Notes)
Machine learning beginners tried RBM
[Machine learning] Understanding random forest
Machine learning with Python! Preparation