代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。