我尝试多元回归

问题描述 投票:-1回答:1

我似乎找不到任何进行多元回归的python库。我发现的唯一的东西只是做简单的回归。我需要针对几个自变量(x1,x2,x3等)对因变量(y)进行回归。

在我的代码中找不到错误:

       #data['PROFIT','HI','LO'] = data.target 
        #ycenter=pd.DataFrame(np.c_[data['PROFIT'],data['HI'],data['LO']], columns=['PROFIT','HI','LO'])
        #Xcenter=pd.DataFrame(np.c_[data['ROLL'],data['DICE'],data['FREE'],data['STAKE'],data['MULT']],        columns=['ROLL','DICE','FREE','STAKE','MULT'])
       Xcenter=data['ROLL','DICE','FREE','STAKE','MULT']
       Ycenter=data['PROFIT','HI','LO'] 
   # split into 70:30 ration 
       X_traincenter, X_testcenter, y_traincenter, y_testcenter = train_test_split(Xcenter, ycenter, test_size = 0.3, random_state = 0) 
# describes info about traincenter and testcenter set 
print("Number transactions X_traincenter dataset: ", X_traincenter.shape) 
print("Number transactions y_traincenter dataset: ", y_traincenter.shape) 
print("Number transactions X_testcenter dataset: ", X_testcenter.shape) 
print("Number transactions y_testcenter dataset: ", y_testcenter.shape)

print("Before OverSampling, counts of positive profitcenter: {}".format(sum(y_traincenter > 0))) 
print("Before OverSampling, counts of negative profitcenter: {} \n".format(sum(y_traincenter < 0))) 
 #Initialize the linear model center
regcenter = LinearRegression()

 #Train the model with our training datacenter
regcenter.fit(X_traincenter,y_traincenter)

 #Print the coeffiscients/weights for each feature/column of our model
print(regcenter.coef_) #f(x,a) = mx + da + b =y

 #Print the predictions on our test datacenter
y_predcenter = regcenter.predict(X_testcenter)
print(y_predcenter)

 #Print the actual values center
print(y_testcenter)

 #Check the model performance/accuracy using Mean Squared Error (MSE)
print(np.mean((y_predcenter - y_testcenter)**2))

 #Check the model performance/accuracy using Mean Squared Error (MSE) and sklearn.metrics
print(mean_squared_error(y_testcenter,y_predcenter))
 #check the predictions against the actual values by using the RMSE and R-2 metrics
test_setcenter_rmse = (np.sqrt(mean_squared_error(y_testcenter, y_predcenter)))
print('test_setcenter_rmse = ',test_setcenter_rmse)
test_setcenter_r2 = r2_score(y_testcenter, y_predcenter)
print('test_setcenter_r2 = ',test_setcenter_r2)

我将如何在python中对它们进行回归,以获得线性回归公式:

Y(x3,x5) = a1x1 + a2x2 + a3x4 + a4x6 + a5x7 + a6x8 + +a7x9 + c
python machine-learning linear-regression
1个回答
0
投票

嗯,Python可以执行多项式回归以及多项式回归。但是,如果您愿意,可以执行梯度下降-梯度下降:here您也可以使用法线方程法执行多元回归-正态方程:here但是,如果您不想为正态方程式或梯度下降编写代码,则只需使用python的sklearn库即可,它可用于单个功能和多个功能。from sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)reg = linear_model.LinearRegression()reg.fit(X_train, y_train)reg.predict(X_test)

© www.soinside.com 2019 - 2024. All rights reserved.