3 Star 0 Fork 1

chenghui03/Calib_RT

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
regression.py 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
import statsmodels.api as sm
import numpy as np
def fit_by_lowess(x, y):
data=np.column_stack((x,y))
data = data[data[:, 0] != 0]
x,y=data[:,0],data[:,1]
# 挑选合适frac
frac=choose_frac(x,y)
# 拟合 by lowess
lowess = sm.nonparametric.lowess
y_lowess = lowess(y, x, frac)
x_fit, y_fit = zip(*y_lowess)
x_fit, y_fit = np.array(x_fit), np.array(y_fit)
return x_fit, y_fit
def choose_frac(x,y,delta_k_threshold=3):
import statsmodels.api as sm
class InputDataError(Exception):
def __init__(self, message) -> None:
super().__init__(message)
for frac in np.linspace(0,1,20)[2:]:
lowess = sm.nonparametric.lowess(y, x, frac)
x_smoothed,y_smoothed =map(np.array,zip(*lowess))
delta_x=x_smoothed[1:]-x_smoothed[:-1]
delta_x = np.where(delta_x == 0, np.inf,delta_x)
delta_y=y_smoothed[1:]-y_smoothed[:-1]
k_list=delta_y/delta_x
k_delta=k_list[1:]-k_list[:-1]
if abs(np.max(k_delta)) < delta_k_threshold:
return frac
else:
raise InputDataError("Please check if your filtered data is approiate")
class Predictor:
def __init__(self,x_fit,y_fit) -> None:
def make_x_unique(x_fit,y_fit):
data=np.column_stack((x_fit,y_fit))
unique_data=dict()
for x,y in data:
if not unique_data.get(x):
unique_data[x]=y
else:
pass
return np.array(list(unique_data.items()))
from scipy.interpolate import interp1d
data=make_x_unique(x_fit,y_fit)
x_fit,y_fit=data[:,0],data[:,1]
self.f = interp1d(x_fit, y_fit, kind='linear', fill_value='extrapolate')
def predict(self,x_interp):
return self.f(x_interp)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/chenghui03/calib_-rt.git
git@gitee.com:chenghui03/calib_-rt.git
chenghui03
calib_-rt
Calib_RT
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385