Python集成库的线性模型形式,参数值与属性值对应,斜率值取Arr,偏移值取浮点数,建模参数输出值取coef_[m],intercept_
y = w[0]*x[0] + w[1]*x[1] + w[2]*x[2] + ... w[p]*x[p] + b
建模预测,模型既定的情况下根据不同的算法,会有不同的输出值,这里以定性的概念进行介绍,所有算法都已集成实现
#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: import matplotlib.pyplot as plt # In[3]: from sklearn.linear_model import Lasso # In[4]: from sklearn.model_selection import train_test_split # In[5]: import mglearn # In[6]: X,y = mglearn.datasets.load_extended_boston() # In[7]: X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 0) # In[8]: lasso_set = Lasso().fit(X_train,y_train) # In[9]: print ("{}".format(lasso_set.score(X_test,y_test))) # In[10]: print ("{}".format(lasso_set.score(X_train,y_train))) # In[11]: lasso_set_1 = Lasso(alpha = 1).fit(X_train,y_train) lasso_set_2 = Lasso(alpha = 0.01,max_iter = 100000).fit(X_train,y_train) lasso_set_3 = Lasso(alpha = 0.0001,max_iter = 100000).fit(X_train,y_train) #lasso_set_4 = Lasso(alpha = 1,max_iter = 101000).fit(X_train,y_train) #print ("{}".format(lasso_set.score(X_test,y_test))) #print ("{}".format(lasso_set.score(X_train,y_train))) # In[12]: plt.figure(figsize = (16,16)) plt.plot(lasso_set_1.coef_,'s',label = 'alpha = 1') plt.plot(lasso_set_2.coef_,'^',label = 'alpha = 0.01') plt.plot(lasso_set_3.coef_,'v',label = 'alpha = 0.0001') plt.legend(ncol = 2,loc = (0,1.05)) plt.xlabel('Index') plt.ylabel('Magnitude') # In[ ]:
定性的参数调节与在训练集和测试集上面的精度测试,参数还是挺重要的一个环节,对于工程性环节而言,定性使用足矣,不需要过于在意算法内部的具体实现,只需要将算法的模型进行常规移植即可
该L1线性回归算法模型,取泛化能力最高的那个,将线性参数输出(如果支持在PC上运行,写一个输入预测接口就行,如果最终的应用是一些常规非操作系统应用,需要移植,这里作为非操作系统假设)
print ('{}'.format(lasso_set_2.coef_))
print ('{}'.format(lasso_set_2.intercept_))
[ -0. -0. -0. 0. -0.
0. -0. -1.43260465 10.94771183 0.
0. 0. -0.39260971 -0. -0.
-0. 0. -0. -0. -0.
-0. -8.75628457 -0. -0. -0.
-0. 2.10027365 -0. 0. -0.
0. -0. 0. 0. -0.
0. -0. -0. 0. 0.
0. -0. 0. -3.96958293 0.
6.61845793 -0. -0. -0. 0.
-4.42086828 -2.10371434 3.79607992 -0. 4.38591262
0. 0. 0.1795777 -0. -1.1614282
-4.33485764 -0. -0. -2.13549022 -0.
-1.85967636 -0. -0. 29.81957225 -2.05624806
0. -11.98034348 -11.14870694 -11.67147204 12.974385
-10.93053676 -0. -0. 3.441574 0.
-0. -0. -8.56750394 0. 0.
-0. 0. -7.26115293 -0. 0.
0.94254631 0. 0. -7.62603559 1.65255854
0. 0. -17.39462971 0. 0.
-0. 0.34404124 -8.24645566 17.5606721 ]
20.47637168564104
根据最先给出的公式进行移植即可,对于多传感器阵列的多源数据融合使用与数据分析,具有参考意义(单一传感器也可以,而且模型更简单)
y = w[0]*x[0] + b