【问题标题】:Data transformation with sklearn.preprocessing in Python在 Python 中使用 sklearn.preprocessing 进行数据转换
【发布时间】:2019-08-02 08:53:05
【问题描述】:

我使用 Python 和 sklearn 为多项式回归编写了代码。我使用了预处理和 PolynomialFeatures 来转换我的数据。是否可以使用预处理和转换我的数据,以便我可以进行对数回归? 我到处找,我什么也没找到。 这是多项式回归的代码,我的问题是,如何将这段代码改为对数回归:

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))

【问题讨论】:

  • 多项式特征包括度数小于或等于degree 的特征之间的所有交互,因此这会增加一些额外的列。你想只得到每个特征的对数吗?
  • 我知道如何做线性回归和多项式回归,现在我想做对数回归,所以,如果得到每个特征的对数就可以了,那么我想要那个

标签: python machine-learning scikit-learn sklearn-pandas


【解决方案1】:

如果您想进行平稳过渡,最好的方法是使用 scikit-learn 的风格定义您自己的估算器。您可以找到更多信息here

这是一种可能性:

from sklearn.base import BaseEstimator, TransformerMixin

class LogarithmicFeatures(BaseEstimator, TransformerMixin):

    def __init__(self):
        pass

    def fit(self, X, y=None):
        self.n_features_ = X.shape[1]
        return self

    def transform(self, X, y=None):
        if X.shape[1] != self.n_features_:
            raise ValueError("X must have {:d} columns".format(self.n_features_))
        return np.log(X)

然后您可以使用以下代码将其插入您的代码中:

lf = LogarithmicFeatures()
log_variables = lf.fit_transform(variables)

【讨论】:

  • 有更简单的方法吗? sklearn 是否有某种对数变换,就像多项式特征一样?我已阅读有关 [scikit-learn.org/stable/modules/generated/… 的信息,但找不到任何内容
  • 刚刚偶然发现this。使用这个类,你可以这样:lf = FunctionTransformer(np.log); log_variables = lf.fit_transform(variables)
猜你喜欢
  • 2017-10-14
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 2022-01-22
  • 2019-05-12
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多