第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用附加變量進行回歸以進行優(yōu)化

使用附加變量進行回歸以進行優(yōu)化

慕慕森 2022-01-05 19:37:28
我得到了由 X 和 Y 點組成的數(shù)據(jù)(x_1,...x_n; y1,...y_n)。我想使用兩個基函數(shù)將 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以便最小化上述擬合的殘差平方和。我嘗試執(zhí)行以下操作:我創(chuàng)建了兩個參數(shù)的函數(shù)(mu_1 and mu_2),返回 Y 到 X 的擬合質(zhì)量。然后我嘗試使用scipy.optimize.minimize. 這是代碼:import numpy as npfrom scipy.optimize import minimizefrom sklearn.linear_model import LinearRegression###Create X and YX = np.random.normal(10,1,size = 10000)Y = np.random.normal(20,1,size = 10000)###Create function that estimates quality of fitdef 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 回答

?
守候你守候我

TA貢獻1802條經(jīng)驗 獲得超10個贊

這個圖形擬合器使用您的功能,它似乎可以滿足您的要求。


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)


查看完整回答
反對 回復(fù) 2022-01-05
  • 1 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號