Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning

I will write about how to read csv data in Python and perform linear regression and non-linear regression. Python ver is 3.7.3. In windows10 environment.

The code is as follows.

◆ Read csv data and perform linear simple regression analysis

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
 
#variable
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
 
#y-axis csv data read
data = pd.read_csv('cp.csv') 
y = np.array(data)#Arrangement
y = y.reshape(-1,)#Dimension change(2 to 1 dimension)
 
#Create date x-axis
x = pd.date_range('2019-08-30 00:00:00', periods=16, freq='d')
x1=np.arange(16)
x2 = [[x1] for x1 in x1]
 
#Linear simple regression
from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit(x2,y) #Create a predictive model
 
print("Regression coefficient= ", clf.coef_)
print("Intercept= ", clf.intercept_)
print("Coefficient of determination= ", clf.score(x2,y))
 
#Graph creation
ax.plot(x2,y,'b')
ax.plot(x2, clf.predict(x2),'k')
 
#Specifying the graph format
plt.xlabel("Days elapsed")#Horizontal axis label
plt.ylabel("plice")#Vertical label
plt.grid(True)#Scale display
plt.tight_layout()#All plots in a box
 
plt.show()

◆ Read csv data and perform nonlinear simple regression analysis

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
 
#variable
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
 
#y-axis csv data read
data = pd.read_csv('cp.csv') 
y = np.array(data)#Arrangement
y = y.reshape(-1,)#Dimension change(2 to 1 dimension)
 
#Create date x-axis
x = pd.date_range('2019-08-30 00:00:00', periods=16, freq='d')
 
#Nonlinear regression order
x1=np.arange(16)
fit = np.polyfit(x1,y,5)
y2 = np.poly1d(fit)(x1)
 
#Coefficient of determination
from sklearn.metrics import r2_score
print(r2_score(y,y2))
 
#Forecast
x_30 = np.poly1d(fit)(30)
print(x_30)
 
 
#Graph creation
ax.plot(x,y,'bo')
ax.plot(x,y2,'--k')
 
#Specifying the graph format
days = mdates.DayLocator() 
daysFmt = mdates.DateFormatter('%m-%d')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(daysFmt)
plt.xlabel("date")#Horizontal axis label
plt.ylabel("price")#Vertical label
plt.grid(True)#Scale display
plt.tight_layout()#All plots in a box
 
plt.show()

Click here for a technical explanation of regression analysis and a detailed explanation of each code (´ ・ ω ・ `) ☟☟☟ https://kgrneer.com/python-numpy-scikit-kaiki/

I am also studying multiple regression.

Recommended Posts

Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Machine learning with python (2) Simple regression analysis
Machine learning algorithm (simple regression analysis)
Machine learning linear regression
[Machine learning] Understanding linear simple regression from both scikit-learn and mathematics
EV3 x Python Machine Learning Part 2 Linear Regression
Machine Learning: Supervised --Linear Regression
Simple regression analysis in Python
[Python] Linear regression with scikit-learn
2. Multivariate analysis spelled out in Python 1-1. Simple regression analysis (scikit-learn)
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
First simple regression analysis in Python
Machine learning beginners try linear regression
Machine learning algorithm (multiple regression analysis)
Machine Learning: Supervised --Linear Discriminant Analysis
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
Machine learning algorithm (generalization of linear regression)
Linear regression in Python (statmodels, scikit-learn, PyMC3)
<Course> Machine Learning Chapter 1: Linear Regression Model
[Python] First data analysis / machine learning (Kaggle)
[Machine learning] Understanding linear multiple regression from both scikit-learn and mathematics
<Course> Machine Learning Chapter 2: Nonlinear Regression Model
Machine learning algorithm (linear regression summary & regularization)
[Machine learning] Regression analysis using scikit learn
[Python] Data analysis, machine learning practice (Kaggle) -Data preprocessing-
[Python3] Let's analyze data using machine learning! (Regression)
Machine learning logistic regression
Python: Supervised Learning (Regression)
Regression analysis in Python
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Calculate the regression coefficient of simple regression analysis with python
2. Multivariate analysis spelled out in Python 2-1. Multiple regression analysis (scikit-learn)
2. Multivariate analysis spelled out in Python 1-2. Simple regression analysis (algorithm)
Machine learning with Python! Preparation
Understand machine learning ~ ridge regression ~.
Python data analysis learning notes
Python Machine Learning Programming> Keywords
Machine learning algorithm (simple perceptron)
Supervised machine learning (classification / regression)
Beginning with Python machine learning
Online linear regression in Python
Machine learning stacking template (regression)
Machine learning algorithm (logistic regression)
Robust linear regression with scikit-learn
Coursera Machine Learning Challenges in Python: ex7-2 (Principal Component Analysis)
[Machine learning] Understanding logistic regression from both scikit-learn and mathematics
Python: Application of supervised learning (regression)
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
Machine learning with python (1) Overall classification
Machine learning summary by Python beginners
Classification and regression in machine learning
Try machine learning with scikit-learn SVM
Predictive Statistics (Practice Simple Regression) Python
<For beginners> python library <For machine learning>
Python: Preprocessing in Machine Learning: Overview
2. Multivariate analysis spelled out in Python 6-2. Ridge regression / Lasso regression (scikit-learn) [Ridge regression vs. Lasso regression]
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
Simple regression analysis implementation in Keras
"Gaussian process and machine learning" Gaussian process regression implemented only with Python numpy
Python: Unsupervised Learning: Principal Component Analysis
Logistic regression analysis Self-made with python