2016-08-17 3 views
0

방금 ​​sklearn을 사용하기 시작 했으므로 제품을 분류하고 싶습니다. 상품은 주문 행에 표시되며 설명, 가격, 제조업체, 주문 수량 등의 속성을 갖습니다. 이러한 속성 중 일부는 텍스트이고 다른 것은 숫자 (정수 또는 부동 소수점)입니다. 이 속성을 사용하여 제품 유지 관리가 필요한지 예측할 수 있습니다. 우리가 구입하는 제품은 엔진, 펌프 등과 같은 것일 수도 있지만 견과류, 호스, 필터 등일 수도 있습니다. 지금까지 가격과 수량을 토대로 예측을했는데 설명이나 제조업체를 기반으로 다른 예측을했습니다. 이제 이러한 예측을 결합하고 싶지만 어떻게해야할지 모르겠습니다. 필자는 Pipeline 및 FeatureUnion 페이지를 보았지만 혼란 스럽습니다. 누구든지 텍스트와 숫자 열이 동시에있는 데이터를 예측하는 방법에 대한 간단한 예제가 있습니까?문자와 숫자가 모두 포함 된 기능 벡터와 함께 sklearn을 사용하는 방법을 모르십니까?

order_lines.head(5) 

    Part No Part Description Quantity Price/Base Supplier Name Purch UoM Category 
0 1112165 Duikwerkzaamheden 1.0 750.00 Duik & Bergingsbedrijf Europa B.V. pcs 0 
1 1112165 Duikwerkzaamheden bij de helling 1.0 500.00 Duik & Bergingsbedrijf Europa B.V. pcs 0 
2 1070285 Inspectie boegschroef, dd. 26-03-2012 1.0 0.01 Duik & Bergingsbedrijf Europa B.V. pcs 0 
3 1037024 Spare parts Albanie Acc. List 1.0 3809.16 Lastechniek Europa B.V. - 0 
4 1037025 M_PO:441.35/BW_INV:0 1.0 0.00 Exalto pcs 0 

category_column = order_lines['Category'] 
order_lines = order_lines[['Part Description', 'Quantity', 'Price/Base', 'Supplier Name', 'Purch UoM']] 

from sklearn.cross_validation import train_test_split 
features_train, features_test, target_train, target_test = train_test_split(order_lines, category_column, test_size=0.20) 

from sklearn.base import TransformerMixin, BaseEstimator 

class FeatureTypeSelector(TransformerMixin, BaseEstimator): 
    FEATURE_TYPES = { 
     'price and quantity': [ 
      'Price/Base', 
      'Quantity', 
     ], 
     'description, supplier, uom': [ 
      'Part Description', 
      'Supplier Name', 
      'Purch UoM', 
     ], 
    } 
    def __init__(self, feature_type): 
     self.columns = self.FEATURE_TYPES[feature_type] 

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

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

from sklearn.feature_extraction.text import CountVectorizer 
from sklearn.naive_bayes import MultinomialNB 
from sklearn.svm import LinearSVC 
from sklearn.pipeline import make_union, make_pipeline 
from sklearn.preprocessing import RobustScaler 

preprocessor = make_union(
    make_pipeline(
     FeatureTypeSelector('price and quantity'), 
     RobustScaler(), 
    ), 
    make_pipeline(
     FeatureTypeSelector('description, supplier, uom'), 
     CountVectorizer(), 
    ), 
) 
preprocessor.fit_transform(features_train) 

를 그리고이 오류가있어 :

지금이

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-51-f8b0db33462a> in <module>() 
----> 1 preprocessor.fit_transform(features_train) 

C:\Anaconda3\lib\site-packages\sklearn\pipeline.py in fit_transform(self, X, y, **fit_params) 
    500   self._update_transformer_list(transformers) 
    501   if any(sparse.issparse(f) for f in Xs): 
--> 502    Xs = sparse.hstack(Xs).tocsr() 
    503   else: 
    504    Xs = np.hstack(Xs) 

C:\Anaconda3\lib\site-packages\scipy\sparse\construct.py in hstack(blocks, format, dtype) 
    462 
    463  """ 
--> 464  return bmat([blocks], format=format, dtype=dtype) 
    465 
    466 

C:\Anaconda3\lib\site-packages\scipy\sparse\construct.py in bmat(blocks, format, dtype) 
    579     else: 
    580      if brow_lengths[i] != A.shape[0]: 
--> 581       raise ValueError('blocks[%d,:] has incompatible row dimensions' % i) 
    582 
    583     if bcol_lengths[j] == 0: 

ValueError: blocks[0,:] has incompatible row dimensions 
+0

매우 비슷한 질문에 대답했습니다. 이게 도움이 되니? http://stackoverflow.com/questions/39001956/sklearn-pipeline-transformation-on-only-certain-features – maxymoo

답변

2

나는 결합 후 다른 기능 유형에 대한 예측 등을 공개하고 싶지 않아 제안을. 제안대로 FeatureUnion을 사용하는 것이 더 낫습니다. 각 기능 유형에 대해 별도의 사전 처리 파이프 라인을 만들 수 있습니다.

import pandas as pd 

# create a pandas dataframe that contains your features 
X = pd.DataFrame({'quantity': [13, 7, 42, 11], 
        'item_name': ['nut', 'bolt', 'bolt', 'chair'], 
        'item_type': ['hardware', 'hardware', 'hardware', 'furniture'], 
        'item_price': [1.95, 4.95, 2.79, 19.95]}) 

# create corresponding target (this is often just one of the dataframe columns) 
y = pd.Series([0, 1, 1, 0], index=X.index) 

내가 함께 PipelineFeatureUnion (또는 오히려 자신의 간단한 단축키 make_pipelinemake_union를 사용하여 모든 접착제 : 내가 자주 사용하는 구조는 다음과 같은 ...

이의이 놀러 장난감 예를 들어 데이터 집합을 정의 할 수 있습니다) : 여기

from sklearn.pipeline import make_union, make_pipeline 
from sklearn.feature_extraction import DictVectorizer 
from sklearn.preprocessing import RobustScaler 
from sklearn.linear_model import LogisticRegression 

# create your preprocessor that handles different feature types separately 
preprocessor = make_union(
    make_pipeline(
     FeatureTypeSelector('continuous'), 
     RobustScaler(), 
    ), 
    make_pipeline(
     FeatureTypeSelector('categorical'), 
     RowToDictTransformer(), 
     DictVectorizer(sparse=False), # set sparse=True if you get MemoryError 
    ), 
) 

# example use of your combined preprocessor 
preprocessor.fit_transform(X) 

# choose some estimator 
estimator = LogisticRegression() 

# your prediction model can be created as follows 
model = make_pipeline(preprocessor, estimator) 

# and training is done as follows 
model.fit(X, y) 

# predict (preferably not on training data X) 
model.predict(X) 

, 내가 정의 내 자신의 사용자 정의 변압기 FeatureTypeSelectorRowToDictTransformer 다음과 같이

from sklearn.base import TransformerMixin, BaseEstimator 


class FeatureTypeSelector(TransformerMixin, BaseEstimator): 
    """ Selects a subset of features based on their type """ 

    FEATURE_TYPES = { 
     'categorical': [ 
      'item_name', 
      'item_type', 
     ], 
     'continuous': [ 
      'quantity', 
      'item_price', 
     ] 
    } 

    def __init__(self, feature_type): 
     self.columns = self.FEATURE_TYPES[feature_type] 

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

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


class RowToDictTransformer(TransformerMixin, BaseEstimator): 
    """ Prepare dataframe for DictVectorizer """ 

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

    def transform(self, X): 
     return (row[1] for row in X.iterrows()) 

이 예제는 기능 통합 작업을 수행하는 방법에 대한보다 선명한 이미지를 그려주기를 바랍니다.

-Kris

+0

대단히 감사합니다! –

+0

Dict –

+0

에 대한 부분을 제거했습니다. 예제에서는 다음과 같은 오류가 발생합니다. 'TypeError : fit_transform()은 2 개의 위치 인수를 갖지만 3은 주어진 것입니다.' –

관련 문제