【问题标题】:How to write multivariate logarithmic regression with Python and sklearn?如何用 Python 和 sklearn 编写多元对数回归?
【发布时间】:2019-07-23 19:14:10
【问题描述】:

我为多元多项式回归编写了代码,我使用了 sklearn 的多项式特征和转换函数。是否可以进行多元对数回归? sklearn 是否有某种对数变换,就像多项式特征一样? 如何在python中编写多元对数回归?

这是我的多元多项式特征代码:

import numpy as np
import pandas as pd
import math
import xlrd
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures


#Reading data from excel

data = pd.read_excel("DataSet.xls").round(2)
data_size = data.shape[0]
#print("Number of data:",data_size,"\n",data.head())

def polynomial_prediction_of_future_strength(input_data, cement, blast_fur_slug,fly_ash,
                                              water, superpl, coarse_aggr, fine_aggr, days):

    variables = prediction_accuracy(input_data)[2]
    results = prediction_accuracy(input_data)[3]
    n = results.shape[0]
    results = results.values.reshape(n,1) #reshaping the values so that variables and results have the same shape

    #transforming the data into polynomial function
    Poly_Regression = PolynomialFeatures(degree=2)
    poly_variables = Poly_Regression.fit_transform(variables)

    #accuracy of prediction(splitting the dataset on train and test)
    poly_var_train, poly_var_test, res_train, res_test = train_test_split(poly_variables, results, test_size = 0.3, random_state = 4)

    input_values = [cement, blast_fur_slug, fly_ash, water, superpl, coarse_aggr, fine_aggr, days]
    input_values = Poly_Regression.transform([input_values]) #transforming the data for prediction in polynomial function

    regression = linear_model.LinearRegression() #making the linear model
    model = regression.fit(poly_var_train, res_train) #fitting polynomial data to the model

    predicted_strength = regression.predict(input_values) #strength prediction
    predicted_strength = round(predicted_strength[0,0], 2)

    score = model.score(poly_var_test, res_test) #accuracy prediction
    score = round(score*100, 2)

    accuracy_info = "Accuracy of concrete class prediction: " + str(score) + " %\n"
    prediction_info = "Prediction of future concrete class after "+ str(days)+" days: "+ str(predicted_strength) 

    info = "\n" + accuracy_info + prediction_info

    return info

#print(polynomial_prediction_of_future_strength(data, 214.9 , 53.8, 121.9, 155.6, 9.6, 1014.3, 780.6, 7))

【问题讨论】:

  • Here is a discussion on stats.stackexchange 关于拟合对数曲线,一些答案可能会有所帮助
  • 我知道如何使用 numpy 使用一个参数进行对数回归,但我不知道如何使用多个变量/参数进行对数回归。
  • 我试图对我的 pandas 值进行对数,但它不起作用

标签: python machine-learning scikit-learn regression


【解决方案1】:

如果您想拟合特征的对数,一种选择是 Box-Cox 变换然后是 OLS,您可以使用 PowerTransformer 在 sklearn 中应用它。 https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PowerTransformer.html#sklearn.preprocessing.PowerTransformer

【讨论】:

  • 我想做对数回归,我知道如何做线性回归(线性函数),我知道如何做多项式回归(多项式函数)。现在我想找到一种方法来进行对数回归
  • 对数回归是什么意思?多项式回归只是对特征的多项式变换的线性回归。如果您想要相同的东西但使用对数变换,那么 Box-Cox 变换会为您完成。
  • 谢谢,我试图在我的代码中做到这一点,但我无法做到,我不理解文档。如果您可以更改我发布的代码,只是添加您认为的内容,那就太好了,因为我试图在最后 45 分钟内这样做,但我无法做到
  • 如果您能给我写一个示例代码,我将非常感激。我有一个使用 from sklearn.preprocessing import PolynomialFeatures 发布的代码,但我不知道如何将其更改为 PowerTransformer、Box-Cox。
猜你喜欢
  • 2017-02-25
  • 2019-01-30
  • 2017-06-21
  • 2016-03-06
  • 2016-08-14
  • 2018-08-11
  • 2021-01-12
  • 2021-12-17
  • 2018-11-17
相关资源
最近更新 更多