Python 确定多项式拟合/回归的阶数实例

yipeiwu_com5年前Python基础

通过 1至10 阶来拟合对比 均方误差及R评分,可以确定最优的“最大阶数”。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression,Perceptron
from sklearn.metrics import mean_squared_error,r2_score
from sklearn.model_selection import train_test_split
 
X = np.array([-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]).reshape(-1, 1)
y = np.array(2*(X**4) + X**2 + 9*X + 2)
#y = np.array([300,500,0,-10,0,20,200,300,1000,800,4000,5000,10000,9000,22000]).reshape(-1, 1)
 
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
rmses = []
degrees = np.arange(1, 10)
min_rmse, min_deg,score = 1e10, 0 ,0
 
for deg in degrees:
	# 生成多项式特征集(如根据degree=3 ,生成 [[x,x**2,x**3]] )
	poly = PolynomialFeatures(degree=deg, include_bias=False)
	x_train_poly = poly.fit_transform(x_train)
 
	# 多项式拟合
	poly_reg = LinearRegression()
	poly_reg.fit(x_train_poly, y_train)
	#print(poly_reg.coef_,poly_reg.intercept_) #系数及常数
	
	# 测试集比较
	x_test_poly = poly.fit_transform(x_test)
	y_test_pred = poly_reg.predict(x_test_poly)
	
	#mean_squared_error(y_true, y_pred) #均方误差回归损失,越小越好。
	poly_rmse = np.sqrt(mean_squared_error(y_test, y_test_pred))
	rmses.append(poly_rmse)
	# r2 范围[0,1],R2越接近1拟合越好。
	r2score = r2_score(y_test, y_test_pred)
	
	# degree交叉验证
	if min_rmse > poly_rmse:
		min_rmse = poly_rmse
		min_deg = deg
		score = r2score
	print('degree = %s, RMSE = %.2f ,r2_score = %.2f' % (deg, poly_rmse,r2score))
		
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(degrees, rmses)
ax.set_yscale('log')
ax.set_xlabel('Degree')
ax.set_ylabel('RMSE')
ax.set_title('Best degree = %s, RMSE = %.2f, r2_score = %.2f' %(min_deg, min_rmse,score)) 
plt.show()

Python 确定多项式拟合/回归的阶数

Python 确定多项式拟合/回归的阶数

因为因变量 Y = 2*(X**4) + X**2 + 9*X + 2 ,自变量和因变量是完整的公式,看图很明显,degree >=4 的都符合,拟合函数都正确。(RMSE 最小,R平方非负且接近于1,则模型最好

如果将 Y 值改为如下:

y = np.array([300,500,0,-10,0,20,200,300,1000,800,4000,5000,10000,9000,22000]).reshape(-1, 1)

Python 确定多项式拟合/回归的阶数

Python 确定多项式拟合/回归的阶数

degree=3 是最好的,且 r 平方也最接近于1(注意:如果 R 平方为负数,则不准确,需再次测试。因样本数据较少,可能也会判断错误)。

以上这篇Python 确定多项式拟合/回归的阶数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python和Java进行DES加密和解密的实例

DES 为 Data Encryption Standard (数据加密标准)的缩写,是一种常见的对称加密算法。有关对称加密与非对称加密的特点及其应用场景,本文就不描述了,读者可以自行...

python内存监控工具memory_profiler和guppy的用法详解

python2.7在内存管理上相比python3还是有些坑的,其释放后的内存仍然保留在python的内存池中,不被系统所用。python循环引用的变量不会被回收,这会导致程序越运行,占用...

pandas DataFrame创建方法的方式

pandas DataFrame创建方法的方式

在pandas里,DataFrame是最经常用的数据结构,这里总结生成和添加数据的方法: ①、把其他格式的数据整理到DataFrame中; ②在已有的DataFrame中插入N列或者N...

浅谈Python采集网页时正则表达式匹配换行符的问题

如下所示: p1 = r'(?<=<div class="ds_cr">)(.*?)(?=<div id="pageurl">)...

Python标准库之Sys模块使用详解

sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名...