当前位置:网站首页>sklearn线性回归完成多次项函数和正弦函数拟合
sklearn线性回归完成多次项函数和正弦函数拟合
2022-07-16 20:38:00 【raylu666】
这样两个式子,使用sklearn 线性回归进行拟合


直接上代码
import random
import numpy as np
from sklearn.linear_model import LinearRegression,Ridge
from sklearn.metrics import accuracy_score, mean_squared_error, r2_score,mean_squared_log_error
from sklearn.utils.multiclass import type_of_target
from sklearn.preprocessing import PolynomialFeatures #多项式特征处理
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
plt.figure()
#data prepare
X = np.linspace(-50,50,100)
# X = np.sin(X)
print(X.shape)
y = 0.015*np.power(X,3)+0.07*np.power(X,2)+6*X+3+np.random.normal(0,100,100)
X = X.reshape(-1,1)
y = y.reshape(-1,1)
#生成N阶多项式
degree = 3
ploy_features = PolynomialFeatures(degree=degree, include_bias=False)
model = LinearRegression(normalize=True) # 线性归回模型实例化,病进行数据标准化
pipline = Pipeline([('ploy_features', ploy_features),('model',model)])
pipline.fit(X,y)
y_predict = pipline.predict(X)
train_score = pipline.score(X,y)
mse = mean_squared_error(y,y_predict)
print(train_score)
print(mse)
print(pipline.get_params())
plt.scatter(X,y, marker='o', c='r')
plt.scatter(X,y_predict,marker='o',c='g')
plt.show()得到结果:
score : 0.9902512046606555
mse : 7940.310765934783
画图结果:

对于正玄曲线原始数据画图


degree定成三阶拟合图

degree定成二阶拟合图

degree定成六阶拟合图,效果非常好,但不知道是不是有点过拟合了、?

话不多说,直接上代码:
import random
import numpy as np
from sklearn.linear_model import LinearRegression,Ridge
from sklearn.metrics import accuracy_score, mean_squared_error, r2_score,mean_squared_log_error
from sklearn.utils.multiclass import type_of_target
from sklearn.preprocessing import PolynomialFeatures #多项式特征处理
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
plt.figure()
#data prepare
X = np.linspace(-5,5,150)
# X = np.sin(X)
print(X.shape)
y = 0.5*np.sin(X)+np.random.normal(0,0.05,150)
X = X.reshape(-1,1)
y = y.reshape(-1,1)
#生成N阶多项式
degree = 6
ploy_features = PolynomialFeatures(degree=degree, include_bias=False)
model = LinearRegression(normalize=True) # 线性归回模型实例化,病进行数据标准化
pipline = Pipeline([('ploy_features', ploy_features),('model',model)])
pipline.fit(X,y)
y_predict = pipline.predict(X)
train_score = pipline.score(X,y)
mse = mean_squared_error(y,y_predict)
print(train_score)
print(mse)
print(pipline.get_params())
plt.scatter(X,y, marker='o', c='r')
plt.scatter(X,y_predict,marker='o',c='g')
plt.show()
边栏推荐
- How to deal with time series event data? [doctoral thesis of Munich University of technology] neural time series point process (ntpp): continuous time event data modeling
- Tencent is all around. It's silly to ask
- Dajiang school recruitment evaluation question -- round robin problem
- Quickly solve the problem of error or garbled code when inserting Chinese data into mysql
- Technical dry goods | I want all the accuracy and speed of model optimization! Mindspire model accuracy Optimization Practice (II)
- [advanced C language] ⑨ dynamic memory allocation knowledge summary is super detailed
- 7月7日易用性SIG技术分享活动精彩回顾
- 华大110时钟校准
- Innovation Center launch! Congratulations to Jiahua Wulian cloud for obtaining mindspire certification!
- LINGO求解分段函数最大(小)值
猜你喜欢

Innovation Center launch! Congratulations to Jiahua Wulian cloud for obtaining mindspire certification!

修改服务器密码
![[applet project development -- JD mall] product list page of uni app (Part 1)](/img/e2/3af126edf091b3b39954686d63f7c7.png)
[applet project development -- JD mall] product list page of uni app (Part 1)

溢流阀力士乐ZDB10VP2-4X/315V

活动回顾 | 与MindSpore深度畅聊:开源如何探索AI领域全场景

私域运营很火,私域运营是否适合所有企业?

After reading this article, I will teach you to play with vulnhub, the penetration test target machine -- evilbox one

Error in WordPress establishing database connection

Peking University and Microsoft jointly proposed a super time series representation learning framework, which significantly improved the effect of multiple time series tasks

LINGO求解分段函数最大(小)值
随机推荐
技术干货 | 模型优化精度、速度我全都要!MindSpore模型精度调优实战(二)
Arduino窗口乱码问题
[AI Engineering] 02-ai Engineering
LINGO求解分段函数最大(小)值
2 - Conan Binary package Dependence Management Scheme
BurpSuite工具响应结果中文乱码解决方法
Leetcode48. rotate image
7月7日易用性SIG技术分享活动精彩回顾
Leetcode45. Jumping game II
Dajiang school recruitment evaluation question -- round robin problem
After reading this article, I will teach you to play with vulnhub, the penetration test target machine -- evilbox one
远程购买商品Solidity合约
SIEMENS模块6DD1661-0AE1
Nike, the leader of sports, is "red and black" in the trend of consumption recovery
哪个证券公司开户股票手续费低,哪个股票开户安全
活动回顾 | 与MindSpore深度畅聊:开源如何探索AI领域全场景
创新中心首发!恭喜佳华物链云获MindSpore认证!
5g NR notes
[UCOS III source code analysis] - mutually exclusive semaphores (mutually exclusive locks)
uniapp基础知识