对python实现二维函数高次拟合的示例详解
作者:赵迁的博客 时间:2021-06-30 11:24:06
在参加“数据挖掘”比赛中遇到了关于函数高次拟合的问题,然后就整理了一下源码,以便后期的学习与改进。
在本次“数据挖掘”比赛中感觉收获最大的还是对于神经网络的认识,在接近一周的时间里,研究了进40种神经网络模型,虽然在持续一周的挖掘比赛把自己折磨的惨不忍睹,但是收获颇丰。现在想想也挺欣慰自己在这段时间里接受新知识的能力。关于神经网络方面的理解会在后续博文中补充(刚提交完论文,还没来得及整理),先分享一下高次拟合方面的知识。
# coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import csv
from scipy.stats import norm
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
''''' 数据导入 '''
def loadDataSet(fileName):
dataMat = []
labelMat = []
csvfile = file(fileName, 'rb')
reader = csv.reader(csvfile)
b = 0
for line in reader:
if line[50] is '':
b += 1
else:
dataMat.append(float(line[41])/100*20+30)
labelMat.append(float(line[25])*100)
csvfile.close()
print "absence time number: %d" % b
return dataMat,labelMat
xArr,yArr = loadDataSet('data.csv')
x = np.array(xArr)
y = np.array(yArr)
# x = np.arange(0, 1, 0.002)
# y = norm.rvs(0, size=500, scale=0.1)
# y = y + x ** 2
def rmse(y_test, y):
return sp.sqrt(sp.mean((y_test - y) ** 2))
def R2(y_test, y_true):
return 1 - ((y_test - y_true) ** 2).sum() / ((y_true - y_true.mean()) ** 2).sum()
def R22(y_test, y_true):
y_mean = np.array(y_true)
y_mean[:] = y_mean.mean()
return 1 - rmse(y_test, y_true) / rmse(y_mean, y_true)
plt.scatter(x, y, s=5)
#分别进行1,2,3,6次拟合
degree = [1, 2,3, 6]
y_test = []
y_test = np.array(y_test)
for d in degree:
#普通
# clf = Pipeline([('poly', PolynomialFeatures(degree=d)),
# ('linear', LinearRegression(fit_intercept=False))])
# clf.fit(x[:, np.newaxis], y)
# 岭回归
clf = Pipeline([('poly', PolynomialFeatures(degree=d)),
('linear', linear_model.Ridge())])
clf.fit(x[:, np.newaxis], y)
y_test = clf.predict(x[:, np.newaxis])
print('多项式参数%s' %clf.named_steps['linear'].coef_)
print('rmse=%.2f, R2=%.2f, R22=%.2f, clf.score=%.2f' %
(rmse(y_test, y),
R2(y_test, y),
R22(y_test, y),
clf.score(x[:, np.newaxis], y)))
plt.plot(x, y_test, linewidth=2)
plt.grid()
plt.legend(['1', '2','3', '6'], loc='upper left')
plt.show()
来源:https://blog.csdn.net/QianZhaoVic/article/details/70545119
标签:python,二维,高次,拟合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分析描述CentOS mysql安装系统
2010-10-25 20:34:00
基于python和flask实现http接口过程解析
2022-06-01 11:46:36
机器学习经典算法-logistic回归代码详解
2021-05-06 23:56:12
![](https://img.aspxhome.com/file/2023/4/117914_0s.jpg)
Django调用百度AI接口实现人脸注册登录代码实例
2023-10-13 23:07:14
python面向对象之类属性和类方法案例分析
2022-01-18 06:00:36
![](https://img.aspxhome.com/file/2023/1/118761_0s.png)
浅谈flask中的before_request与after_request
2021-05-10 01:29:11
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
2023-03-01 14:24:38
Dreamweaver使用中的7个常见问题与解答
2007-11-03 11:34:00
css彩色虚线表格及JS鼠标指向单元格变色制作方法
2007-08-10 13:08:00
![](https://img.aspxhome.com/file/UploadPic/20078/10/2007810125820865s.jpg)
Django处理Ajax发送的Get请求代码详解
2023-06-29 08:40:40
![](https://img.aspxhome.com/file/2023/7/78787_0s.png)
Django学习笔记之为Model添加Action
2021-04-09 16:32:26
利用Python多处理库处理3D数据详解
2021-03-05 18:11:18
![](https://img.aspxhome.com/file/2023/7/101267_0s.jpg)
Python如何实现强制数据类型转换
2022-10-18 10:08:02
Python笔记之观察者模式
2023-04-05 18:37:20
jupyter读取错误格式文件的解决方案
2022-04-02 18:56:57
![](https://img.aspxhome.com/file/2023/1/85061_0s.jpg)
Python捕捉和模拟鼠标事件的方法
2022-02-14 11:17:24
WPF自定义搜索框代码分享
2023-07-18 23:31:04
![](https://img.aspxhome.com/file/2023/1/79061_0s.jpg)
Python数学建模StatsModels统计回归模型数据的准备
2021-10-08 09:19:24
![](https://img.aspxhome.com/file/2023/1/79511_0s.png)
vscode 远程调试python的方法
2021-09-07 23:00:59
![](https://img.aspxhome.com/file/2023/7/83717_0s.png)
eWebEditor不支持IE8/IE7的解决方法
2010-02-28 10:27:00
![](https://img.aspxhome.com/file/UploadPic/20102/28/4270100787_166776e513_o-72s.jpg)