Python 基于机器学习的类logistic曲线拟合
我在《数据科学线程》上问了这个问题,但没有得到答案。因此在这里发布 我有一组函数的点Python 基于机器学习的类logistic曲线拟合,python,scikit-learn,logistic-regression,Python,Scikit Learn,Logistic Regression,我在《数据科学线程》上问了这个问题,但没有得到答案。因此在这里发布 我有一组函数的点k(x)。我正在尝试做一些曲线拟合来找到精确的k(x)函数。数据点似乎只适合于一条逻辑曲线,只是有一点位移和压力 到目前为止,我已经尝试过多项式回归,但我觉得拟合不正确。我在这里附上了拟合曲线的快照 所以我的问题是,逻辑回归只用于分类任务吗?或者可以用于曲线拟合 如果没有,还有什么其他可用的技术可以将类似逻辑的曲线拟合到一组数据点? 编辑 下面是代码。(x,y)是数据点 import matplotlib.py
k(x)
。我正在尝试做一些曲线拟合来找到精确的k(x)
函数。数据点似乎只适合于一条逻辑曲线,只是有一点位移和压力
到目前为止,我已经尝试过多项式回归,但我觉得拟合不正确。我在这里附上了拟合曲线的快照
所以我的问题是,逻辑回归只用于分类任务吗?或者可以用于曲线拟合
如果没有,还有什么其他可用的技术可以将类似逻辑的曲线拟合到一组数据点?
编辑
下面是代码。(x,y)是数据点
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LogisticRegression
x = np.array([0.3, 0.4, 0.5, 0.6, 0.65, 0.67, 0.8])
y = np.array([-936, -892, -178.33, -50.7, -65.7, -70.44, -9])
degree = 5
model = make_pipeline(PolynomialFeatures(degree), Ridge(alpha=1E-10, fit_intercept=False))
# model = LogisticRegression(random_state=0, solver='lbfgs')
model.fit(x[:, None], y)
ridge = model.named_steps['ridge']
print(ridge.coef_)
coef = ridge.coef_
poly_mse = mean_squared_error(model.predict(x[:, None]), y)
print 'RMSE', math.sqrt(poly_mse)
predictions = model.predict(np.arange(0.28,0.85,0.0001).reshape(-1, 1))
plt.plot(x, y, 'ro', label='Measurement Data')
plt.plot(np.arange(0.28,0.85,0.0001), predictions, label="Best Fit: %.2f$X^4$ %.2f$X^3$ + %.2f$X^2$ + %.2fX %.2f" % (coef[-1],coef[-2],coef[-3],coef[-4],coef[-5]))
plt.title('K vs Barium Proportion (X) at 10kHz')
plt.xlabel('Barium Proportion (X)')
plt.ylabel('K')
plt.show()
这是一个使用你的数据和一个简单的三参数逻辑类型方程的图形装配工,我觉得这个拟合相当好
曲线拟合基本上是回归问题。如果您只想在数据点集中拟合曲线,您应该寻找插值。您可以共享此绘图的数据点吗?(或与您一起工作的人)我已经附上了数据点和代码。我找不到一种方法来拟合这些数据点的逻辑曲线
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import warnings
xData = numpy.array([0.3, 0.4, 0.5, 0.6, 0.65, 0.67, 0.8])
yData = numpy.array([-936.0, -892.0, -178.33, -50.7, -65.7, -70.44, -9.0])
def func(x, a, b, c): # Logistic B equation from zunzun.com
return a / (1.0 + numpy.power(x/b, c))
# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])
# curve fit the test data, ignoring warning due to initial parameter estimates
warnings.filterwarnings("ignore")
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
modelPredictions = func(xData, *fittedParameters)
absError = modelPredictions - yData
SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()
##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')
# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)