【问题标题】:logistic like curve fitting using machine learning使用机器学习进行逻辑类曲线拟合
【发布时间】:2019-07-14 00:39:21
【问题描述】:

我在数据科学线程中问过这个问题,但没有得到答案。因此在这里发帖。

我有一组函数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()

【问题讨论】:

  • 曲线拟合基本上是回归问题。如果您只想在一组数据点中拟合曲线,您应该寻找插值。
  • 你能分享这个情节的数据点吗? (或与您合作的人)
  • 我已经附上了数据点和代码。我找不到将逻辑曲线拟合到这些数据点的方法

标签: python scikit-learn logistic-regression


【解决方案1】:

这是一个使用您的数据和一个简单的三参数逻辑类型方程的图形拟合器,拟合度对我来说似乎相当不错。

plot

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多