字典散列内存错误和功能散列浮动错误

时间:2017-08-10 03:58:11

标签: python scikit-learn pipeline grid-search

这是我的数据[作为熊猫df]:

print(X_train [numeric_predictors + categorical_predictors] .head()):

        bathrooms  bedrooms   price                       building_id  \
10            1.5       3.0  3000.0  53a5b119ba8f7b61d4e010512e0dfc85   
10000         1.0       2.0  5465.0  c5c8a357cba207596b04d1afd1e4f130   
100004        1.0       1.0  2850.0  c3ba40552e2120b0acfc3cb5730bb2aa   
100007        1.0       1.0  3275.0  28d9ad350afeaab8027513a3e52ac8d5   
100013        1.0       4.0  3350.0                                 0  

99993         1.0       0.0   3350.0  ad67f6181a49bde19218929b401b31b7   
99994         1.0       2.0   2200.0  5173052db6efc0caaa4d817112a70f32   


                              manager_id  
10      5ba989232d0489da1b5f2c45f6688adc  
10000   7533621a882f71e25173b27e3139d83d  
100004  d9039c43983f6e564b1482b273bd7b01  
100007  1067e078446a7897d2da493d2f741316  
100013  98e13ad4b495b9613cef886d79a6291f  
...
99993   9fd3af5b2d23951e028059e8940a55d7  
99994   d7f57128272bfd82e33a61999b5f4c42  

最后两列是分类预测变量。

同样,打印熊猫系列X_train [target]:

10        medium
10000        low
100004      high
100007       low
100013       low
...
99993        low
99994        low

我正在尝试使用管道模板并使用散列矢量化器获得错误。

首先,这是我的字典hasher,它给了我一个MemoryError:

from sklearn.feature_extraction import DictVectorizer

dv = DictVectorizer(sparse=False)
feature_dict = X_train[categorical_predictors].to_dict(orient='records')
dv.fit(feature_dict)
out = pd.DataFrame(
    dv.transform(feature_dict),
    columns = dv.feature_names_
)

所以在下一个单元格中,我使用以下代码作为我的特征哈希编码器:

from sklearn.feature_extraction import FeatureHasher

fh = FeatureHasher(n_features=2)
feature_dict = X_train[categorical_predictors].to_dict(orient='records')
fh.fit(feature_dict)
out = pd.DataFrame(fh.transform(feature_dict).toarray())
#print out.head()

注释掉的打印行为我提供了一个DataFrame,其特征行包含每行2个单元格中的-1.0,0.0或1.0浮点数。

这是我的矢量化器汇总字典&功能哈希:

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.feature_extraction import FeatureHasher, DictVectorizer

class MyVectorizer(BaseEstimator, TransformerMixin):
    """
    Vectorize a set of categorical variables
    """

    def __init__(self, cols, hashing=None):
        """
        args:
            cols: a list of column names of the categorical variables
            hashing: 
                If None, then vectorization is a simple one-hot-encoding.
                If an integer, then hashing is the number of features in the output.
        """
        self.cols = cols
        self.hashing = hashing

    def fit(self, X, y=None):

        data = X[self.cols]

        # Choose a vectorizer
        if self.hashing is None:
            self.myvec = DictVectorizer(sparse=False)
        else:
            self.myvec = FeatureHasher(n_features = self.hashing)

        self.myvec.fit(X[self.cols].to_dict(orient='records'))
        return self

    def transform(self, X):

        # Vectorize Input
        if self.hashing is None:
            return pd.DataFrame(
                self.myvec.transform(X[self.cols].to_dict(orient='records')),
                columns = self.myvec.feature_names_
            )
        else:
            return pd.DataFrame(
                self.myvec.transform(X[self.cols].to_dict(orient='records')).toarray()
            )

我把它放在我的管道中:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import FeatureUnion

pipeline = Pipeline([
    ('preprocess', FeatureUnion([
        ('numeric', Pipeline([
            ('scale', StandardScaler())
        ])
        ),
        ('categorical', Pipeline([
            ('vectorize', MyVectorizer(cols=['categorical_predictors'], hashing=None))
        ])
        )
    ])),
    ('predict', MultinomialNB(alphas))
])

和alpha参数:

alphas = {
    'predict__alpha': [.01, .1, 1, 2, 10]
}

并使用gridsearchCV,当我在第三行得到一个错误:

print X_train.head(), train_data[target]
grid_search = GridSearchCV(pipeline, param_grid=alphas,scoring='accuracy')
grid_search.fit(X_train[numeric_predictors + categorical_predictors], X_train[target])
grid_search.best_params_

ValueError:无法将字符串转换为float:d7f57128272bfd82e33a61999b5f4c42

1 个答案:

答案 0 :(得分:2)

错误是由StandardScaler引起的。您将所有数据发送到其中,这是错误的。在您的管道中,在FeatureUnion部分中,您已选择MyVectorizer的分类列,但未对StandardScaler进行任何选择,因此所有列都将进入,导致错误。此外,由于内部管道仅由单个步骤组成,因此不需要管道。

首先,将管道更改为:

pipeline = Pipeline([
    ('preprocess', FeatureUnion([
        ('scale', StandardScaler()),
        ('vectorize', MyVectorizer(cols=['categorical_predictors'], hashing=None))
    ])),
    ('predict', MultinomialNB())
])

这仍然会抛出同样的错误,但现在看起来要复杂得多。

现在我们需要的是可以选择要给StandardScaler的列(数字列),以便不抛出错误。

我们可以通过多种方式实现这一目标,但我正在遵循您的编码风格,并会根据更改创建一个新的课程MyScaler

class MyScaler(BaseEstimator, TransformerMixin):

    def __init__(self, cols):
        self.cols = cols

    def fit(self, X, y=None):

        self.scaler = StandardScaler()
        self.scaler.fit(X[self.cols])
        return self

    def transform(self, X):
        return self.scaler.transform(X[self.cols])

然后将管道更改为:

numeric_predictors=['bathrooms','bedrooms','price']
categorical_predictors = ['building_id','manager_id']

pipeline = Pipeline([
    ('preprocess', FeatureUnion([
        ('scale', MyScaler(cols=numeric_predictors)),
        ('vectorize', MyVectorizer(cols=['categorical_predictors'], hashing=None))
    ])),
    ('predict', MultinomialNB())
]) 

然后它会抛出错误,因为您已将categorical_predictors指定为MyVectorizer的字符串,而不是列表。将其更改为我在MyScaler中所做的更改:更改

MyVectorizer(cols=['categorical_predictors'], hashing=None))

来: -

MyVectorizer(cols=categorical_predictors, hashing=None)

现在您的代码已准备好在语法上执行。但是现在你已经使用MultinomialNB()作为预测器,它只需要特征中的正值。但是,由于StandardScaler将数据缩放为零均值,因此它会将某些值转换为负值,并且您的代码将再次无效。你需要决定做什么......也许把它改成MinMaxScaler。