python - 带有要优化的附加变量的回归
问题描述
我得到了由 X 和 Y 点组成的数据(x_1,...x_n; y1,...y_n)
。
我想使用两个基函数将 X 拟合到 Y:max(x,mu_1)
和min(x,mu_2)
换句话说,我想估计以下等式:
y_i = a_1*max(x_i,mu_1)+a_2*min(x_i,mu_2)
我想找到 mu_1
最mu_2
适合上面的那个。我的意思是这样mu_1
那样mu_2
,当我将 Y 拟合到 X 时,残差平方和被最小化。
或者我可以说我需要a_1
, a_2
, mu_1
,mu_2
以使上述拟合的残差平方和最小化。
我尝试执行以下操作:
我创建了两个参数的函数,(mu_1 and mu_2)
它返回 Y 与 X 的拟合质量。然后我尝试使用scipy.optimize.minimize
. 这是代码:
import numpy as np
from scipy.optimize import minimize
from sklearn.linear_model import LinearRegression
###Create X and Y
X = np.random.normal(10,1,size = 10000)
Y = np.random.normal(20,1,size = 10000)
###Create function that estimates quality of fit
def func(mu_1,mu_2):
### basis functions
regressor_1 = np.maximum(X,mu_1).reshape(-1,1)
regressor_2 = np.minimum(X,mu_2).reshape(-1,1)
x_train = np.hstack((regressor_1,regressor_2))
model = LinearRegression().fit(x_train,Y)
###I didnt find how to extract sum of squared residual, but I can get R
squared, so I thought that minimizing SSR is the same as maximizing R
squared and it is the same as minimizing -R^2
objective = model.score(x_train,Y)
return -1*objective
### Now I want to find such mu_1 and mu_2 that minimize "func"
minimum = minimize(func,0,0)
minimum.x
它不起作用。我将非常感谢任何帮助。
解决方案
这个图形拟合器使用您的功能,它似乎可以满足您的要求。
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import warnings
xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7])
yData = numpy.array([1.1, 20.2, 30.3, 60.4, 50.0, 60.6, 70.7])
def func(x, a_1, a_2, mu_1, mu_2):
retArray = []
for x_i in x: # process data points individually
val = a_1*max(x_i,mu_1) + a_2*min(x_i,mu_2)
retArray.append(val)
return retArray
# turn off the curve_fit() "covariance estimation" warning
warnings.filterwarnings("ignore")
# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0, 1.0])
# curve fit the test data
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)
推荐阅读
- date-range - 反应日期选择器日期范围
- java - com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌中反序列化 `com.utils.POJO[]` 的实例
- python - 在 selenium python 中,我只点击第二个按钮,假设有 3 个相同的按钮
- node.js - 对镜像状态反应 setState
- c - 单片机与PC之间通过rs232通信进行文件访问
- javascript - 将唯一 id 插入多维对象
- python - 使用 imputer 和柱式变压器后,我的谷歌合作实验室中有太多零
- windows - Windows 窗体应用程序 c++,从 2D Buffer 到托管 Bitmap 对象的更快 setPixel 操作
- ios - 按钮添加目标函数未在 CollectionView 单元格中调用
- apache-flink - Flink 修改基于 KeySelector 的 ProcessWindow