其他分享
首页 > 其他分享> > 《机器学习的数学修炼》

《机器学习的数学修炼》

作者:互联网


目录:

 

 

 

 


 

第六章 线性回归:

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):

https://blog.csdn.net/qq_44391957/article/details/120090486?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165854506016782390589615%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165854506016782390589615&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-120090486-null-null.142^v33^pc_search_v2,185^v2^control&utm_term=X%20%3D%20np.array%28X%29.reshape%28-1%2C1%29&spm=1018.2226.3001.4187

2.1 普通最小二乘法的计算:

 

 

同时:

 

 

 

2.2关于MSE RMSE MAE R-Squared(主要看前两个):

https://blog.csdn.net/lch551218/article/details/113573931?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165854608116781683916161%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165854608116781683916161&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-113573931-null-null.142^v33^pc_search_v2,185^v2^control&utm_term=MAE%E3%80%81R-Squared&spm=1018.2226.3001.4187

2.3 线性回归模型的基本假定:

 

 

 

 

 6.4之后暂时没看,需要大把时间。

 


 (未完待续,暂时不看这本书)

标签:机器,df,2522%,修炼,数学,print,import,model,DBS
来源: https://www.cnblogs.com/MrMKG/p/16511214.html