#python #scikit-learn #scipy #linear-regression #mathematical-optimization
#python #scikit-learn #scipy #линейная регрессия #математическая оптимизация
Вопрос:
Мне даны данные, которые состоят из точек 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
Это не работает. Я буду очень признателен за любую помощь.
Ответ №1:
Этот графический монтажник использует вашу функцию, похоже, он делает то, о чем вы просите.
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)