目录:
第六章 线性回归:
1.1三种方法实现:
import numpy as np import pandas as pd from scipy import stats df = pd.read_csv("DBS_SingDollar.csv") # X = df[df.columns[0]] # y = df[df.columns[1]] X = df["DBS"] Y = df["SGD"] slope,intercept,r_value,p_value,std_err= stats.linregress(Y,X) print(slope,intercept)
1 import numpy as np 2 import matplotlib.pyplot as plt 3 import pandas as pd 4 from sklearn import linear_model 5 6 df_DBS = pd.read_csv("DBS_SingDollar.csv") 7 model = linear_model.LinearRegression() 8 9 X = df_DBS['DBS'] 10 Y = df_DBS['SGD'] 11 12 X = np.array(X).reshape(-1,1) 13 model.fit(X,Y) 14 Y_predict = model.predict(X) 15 print(Y_predict) 16 17 plt.scatter(X,Y,color = (0,0,0)) 18 19 plt.plot(X,Y_predict,color = "blue",linewidth = 2) 20 plt.xlabel(" ",fontsize = 16) 21 plt.ylabel(" ",fontsize = 16) 22 plt.show()
import pandas as pd df = pd.read_csv("DBS_SingDollar.csv") #print(dir(pd)) X = df.loc[:,["SGD"]] Y = df.loc[:,["DBS"]] from sklearn import linear_model model = linear_model.LinearRegression() model.fit(X,Y) a = model.coef_ b = model.intercept_ a = float(a) b = float(b) print("the output of the trained model is") print("Y = ",a,"*X + ",b) pred = model.predict(X) print(pred) #rmse from sklearn.metrics import mean_squared_error rmse = mean_squared_error(Y,pred)**0.5 print(rmse)
1.2 相关链接:
python中loc函数的用法:
https://blog.csdn.net/weixin_29288653/article/details/113500824
python中 .reshape 的用法:reshape(1,-1):
2.1 普通最小二乘法的计算:
同时:
2.2关于MSE RMSE MAE R-Squared(主要看前两个):
2.3 线性回归模型的基本假定:
6.4之后暂时没看,需要大把时间。
(未完待续,暂时不看这本书)
标签:机器,df,2522%,修炼,数学,print,import,model,DBS From: https://www.cnblogs.com/MrMKG/p/16511214.html