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.