【问题标题】:i try a multiple regression我尝试多元回归
【发布时间】:2020-05-28 22:08:46
【问题描述】:

我似乎找不到任何执行多重回归的 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】:

    嗯,Python 可以执行多元回归以及多项式回归。

    但如果你愿意,你可以执行梯度下降-
    梯度下降:here

    你也可以使用正规方程法进行多元回归-
    正规方程:here

    但是,如果您不想为正规方程或梯度下降编写代码,只需使用 python 的 sklearn 库,它适用于单个特征和多个特征。
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    X_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)

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 2015-03-09
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2014-12-24
      • 2011-10-11
      • 2018-11-03
      • 1970-01-01
      相关资源
      最近更新 更多