Multivariate/Multiple Linear Regression in Scikit Learn?

前端 未结 2 2022
心在旅途
心在旅途 2021-02-01 06:42

I have a dataset (dataTrain.csv & dataTest.csv) in .csv file with this format:

Temperature(K),Pressure(ATM),CompressibilityFactor(Z)
273.1,24.675,0.806677258         


        
相关标签:
2条回答
  • 2021-02-01 06:58

    If your code above works for univariate, try this

    import pandas as pd
    from sklearn import linear_model
    
    dataTrain = pd.read_csv("dataTrain.csv")
    dataTest = pd.read_csv("dataTest.csv")
    # print df.head()
    
    x_train = dataTrain[['Temperature(K)', 'Pressure(ATM)']].to_numpy().reshape(-1,2)
    y_train = dataTrain['CompressibilityFactor(Z)']
    
    x_test = dataTest[['Temperature(K)', 'Pressure(ATM)']].to_numpy().reshape(-1,2)
    y_test = dataTest['CompressibilityFactor(Z)']
    
    ols = linear_model.LinearRegression()
    model = ols.fit(x_train, y_train)
    
    print model.predict(x_test)[0:5]
    
    0 讨论(0)
  • 2021-02-01 07:11

    That's correct you need to use .values.reshape(-1,2)

    In addition if you want to know the coefficients and the intercept of the expression:

    CompressibilityFactor(Z) = intercept + coefTemperature(K) + coefPressure(ATM)

    you can get them with:

    Coefficients = model.coef_
    intercept = model.intercept_

    0 讨论(0)
提交回复
热议问题