0

피처의 평균을 전재하고 싶지만 다른 카테고리에서 같은 카테고리/명목 값을 갖는 다른 예를 기반으로 평균을 계산하면됩니다. 이것이 scikit-learn의 Imputer 클래스를 사용하여 가능했는지 궁금합니다. 그렇게하면 파이프 라인에 쉽게 추가 할 수 있습니다. 예를 들어scikit-learn 다른 피처에서 명목 값 그룹 내 피처 평균

:

kaggle에서 타이타닉 데이터 세트 사용 : source

가 어떻게 pclass 당 평균 fare을 전가에 대해 갈 것입니다. 배후에있는 생각은 다른 클래스의 사람들이 티켓 간의 비용에 큰 차이가 있다는 것입니다.

업데이트 : 어떤 사람들과 토론 후, 내가 사용해야 문구가 "클래스 내에서 평균 을 전가"했다.

아래에서 Vivek 님의 의견을 살펴본 결과 내가 원하는 것을 할 시간이되면 일반 파이프 라인 기능을 구성 할 것입니다. 나는 그것을 수행하는 방법에 대한 좋은 아이디어를 가지고 있으며 답변이 될 때 게시 할 것입니다. 끝마친.

+1

'pclass'당 데이터를 분할하고, '요금'을 대신 입력 한 다음 다시 스택하여 완전한 데이터를 만들 수 있습니다. –

+0

고마워요 @VivekKumar! 내 파이프 라인의 일환으로이 작업을 수행 할 것입니다. – TheJokersThief

+1

[이 예제] (http://scikit-learn.org/stable/auto_examples/hetero_feature_union.html#sphx-glr-auto-examples-hetero- feature-union-py) 파이프 라인에서 사용할 수있는 클래스를 구현하기위한 힌트를 얻으려면 –

답변

0

아래는 사물의 수단을 다루기위한 의미의 간단한 질문입니다. 보다 견고한 구현은 아마도 모드, 중간 값 등을 수행 할 수있는 scikit learn에서 Imputer 클래스를 사용하는 것을 포함하며 스파 스/고밀도 매트릭스를 다루는 것이 더 낫습니다.

이것은 원래의 질문에 대한 Vivek Kumar의 의견을 바탕으로 데이터를 스택으로 분할하고이를 다시 조립하는 것을 제안했습니다.

import numpy as np 
from sklearn.base import BaseEstimator, TransformerMixin 

class WithinClassMeanImputer(BaseEstimator, TransformerMixin): 
    def __init__(self, replace_col_index, class_col_index = None, missing_values=np.nan): 
     self.missing_values = missing_values 
     self.replace_col_index = replace_col_index 
     self.y = None 
     self.class_col_index = class_col_index 

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

    def transform(self, X): 
     y = self.y 
     classes = np.unique(y) 
     stacks = [] 

     if len(X) > 1 and len(self.y) = len(X): 
      if(self.class_col_index == None): 
       # If we're using the dependent variable 
       for aclass in classes: 
        with_missing = X[(y == aclass) & 
             (X[:, self.replace_col_index] == self.missing_values)] 
        without_missing = X[(y == aclass) & 
              (X[:, self.replace_col_index] != self.missing_values)] 

        column = without_missing[:, self.replace_col_index] 
        # Calculate mean from examples without missing values 
        mean = np.mean(column[without_missing[:, self.replace_col_index] != self.missing_values]) 

        # Broadcast mean to all missing values 
        with_missing[:, self.replace_col_index] = mean 

        stacks.append(np.concatenate((with_missing, without_missing))) 
      else: 
       # If we're using nominal values within a binarised feature (i.e. the classes 
       # are unique values within a nominal column - e.g. sex) 
       for aclass in classes: 
        with_missing = X[(X[:, self.class_col_index] == aclass) & 
             (X[:, self.replace_col_index] == self.missing_values)] 
        without_missing = X[(X[:, self.class_col_index] == aclass) & 
              (X[:, self.replace_col_index] != self.missing_values)] 

        column = without_missing[:, self.replace_col_index] 
        # Calculate mean from examples without missing values 
        mean = np.mean(column[without_missing[:, self.replace_col_index] != self.missing_values]) 

        # Broadcast mean to all missing values 
        with_missing[:, self.replace_col_index] = mean 
        stacks.append(np.concatenate((with_missing, without_missing))) 

      if len(stacks) > 1 : 
       # Reassemble our stacks of values 
       X = np.concatenate(stacks) 

     return X