使用机器学习进行逻辑式曲线拟合

时间:2019-02-20 10:42:55

标签: python scikit-learn logistic-regression

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

我有一组函数k(x)的点。我正在尝试进行一些曲线拟合以找到确切的k(x)函数。数据点似乎只适合于逻辑分布曲线,只是稍微移动并施加压力。

到目前为止,我已经尝试了多项式回归,但是我觉得拟合不正确。我在这里附上了拟合曲线的快照。

所以我的问题是,逻辑回归仅用于分类任务吗?还是可以用于曲线拟合?

如果不是,还有什么其他可用的技术可以将逻辑对数曲线拟合到一组数据点? Polynomial regression

编辑

以下是代码。 (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()

1 个答案:

答案 0 :(得分: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)
相关问题