当前位置:网站首页>Sklearn linear regression completes the fitting of multiple term function and sine function
Sklearn linear regression completes the fitting of multiple term function and sine function
2022-07-18 22:49:00 【raylu666】
These two formulas , Use sklearn Linear regression is used for fitting


Go straight to the code
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 # Polynomial feature processing
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)
# Generate N Order polynomial
degree = 3
ploy_features = PolynomialFeatures(degree=degree, include_bias=False)
model = LinearRegression(normalize=True) # Linear regression model instantiation , Disease data standardization
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()Get the results :
score : 0.9902512046606555
mse : 7940.310765934783
Drawing results :

Draw a picture of the original data of the Zhengxuan curve


degree Make a third-order fitting diagram

degree Make a second-order fitting diagram

degree Determine the sixth order fitting diagram , The effect is very good , But I don't know if it's a little too fitting 、?

Don't talk much , Go straight to the code :
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 # Polynomial feature processing
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)
# Generate N Order polynomial
degree = 6
ploy_features = PolynomialFeatures(degree=degree, include_bias=False)
model = LinearRegression(normalize=True) # Linear regression model instantiation , Disease data standardization
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()
边栏推荐
- Marvell88q5192 switch debugging record (bsta1000b platform)
- Open source ten questions, a quick start guide for community newcomers
- 远程购买商品Solidity合约
- [advanced C language] ⑨ dynamic memory allocation knowledge summary is super detailed
- Nike, the leader of sports, is "red and black" in the trend of consumption recovery
- 时序事件数据怎么处理?【慕尼黑工业大学博士论文】神经时序点过程(NTPP): 连续时间事件数据建模
- npm 常用命令 使用命令删除 node_modules 包
- [deployment] redis
- BurpSuite工具响应结果中文乱码解决方法
- NASA首次拍到宇宙大爆炸后一瞬间的清晰照片
猜你喜欢

Leetcode46. Full arrangement

Can't go on, mend the foundation -- C thread develops output string program

The 100 billion yuan universe market is the new driving force of soul and Yingke

10分钟自定义搭建行人分析系统,检测跟踪、行为识别、人体属性All-in-One

UCOSII里面为什么调用OS_ENTER_CRITICAL()或OS_EXIT_CRITICAL()会出错,出错信息为:undeclared identifier `cpu_sr‘

北大&微软联合提出超强时间序列表示学习框架,显著提升多项时间序列任务效果

Activity review | in depth chat with mindspire: how open source explores the whole scene of AI field

Tencent is all around. It's silly to ask

第3章业务功能开发(查看市场活动明细)

Jiulian technology development board is officially integrated into the openharmony backbone
随机推荐
Innovation Center launch! Congratulations to Jiahua Wulian cloud for obtaining mindspire certification!
[AI Engineering] 02-ai Engineering
Leetcode48. rotate image
[deployment] redis
[play with es] es batch / full import data
lun的概念
openstack 相关博客
leetcode--242. 有效的字母异位词
【小程序项目开发-- 京东商城】uni-app之商品列表页面 (上)
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
语音转换主要涉及技术记录
leetcode--49字母异位词分组
训练过程中出现loss为nan的问题
解题-->在线OJ(十六)
5g NR notes
Gym报错 The observation returned by the `reset()` method is not contained with the .......
Mapbox GL loading 3dfiles gradient model (video)
The distant savior obeys the objective law (V) -- cultural attribute
Problem solving -- > online OJ (16)
C语言自定义类型:结构体,枚举,联合