파이프 라인의 데이터를 정리하기 위해 일련의 사용자 지정 변환을 sklearn
에 작성했습니다. 각 사용자 정의 변환은 fit
및 transform
의 매개 변수로 두 개의 팬더 DataFrame을 사용하므로 transform
은 두 개의 DataFrames도 반환합니다 (아래 예 참조). DataFrames in과 DataFrames out : 파이프 라인에 단 하나의 Transformer가있는 경우이 방법이 유용합니다. 이런 두 Rransformers가 파이프 라인에서 조합 그러나 sklearn 변환은 어떤 데이터에서 작동합니까?
RemoveMissingRowsBasedOnTarget
pipeline = Pipeline ([
('remove_missing_columns', RemoveAllMissing (['mailing_address_str_number'])),
('remove_rows_based_on_target', RemoveMissingRowsBasedOnTarget()),
])
X, y = pipeline.fit_transform (X, y)
==>TypeError: tuple indices must be integers or slices, not Series
클래스. 나는이
pipeline = Pipeline ([
('remove_rows_based_on_target', RemoveMissingRowsBasedOnTarget()),
('remove_missing_columns', RemoveAllMissing (['mailing_address_str_number'])),
])
==> AttributeError: 'tuple' object has no attribute 'apply'
같은 변압기의 위치를 전환 할 때이 오류는 클래스 RemoveAllMissing
발생합니다. 두 경우 모두 오류 메시지는 오류가 발생한 행 위에 ==> 표시됩니다. 정확하게 무슨 일이 벌어 질 수 있는지 정확히 읽은 것 같지만이 주제에 대해서는 아무 것도 찾을 수 없습니다. 누군가 내가 뭘 잘못하고 있는지 말해 줄 수 있니? 아래에서 격리 된 문제에 대한 코드를 찾을 수 있습니다.
import numpy as np
import pandas as pd
import random
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
def create_data (rows, cols, frac_nan, random_state=42):
random.seed (random_state)
X = pd.DataFrame (np.zeros ((rows, cols)),
columns=['col' + str(i) for i in range (cols)],
index=None)
# Create dataframe of (rows * cols) with random floating points
y = pd.DataFrame (np.zeros ((rows,)))
for row in range(rows):
for col in range(cols):
X.iloc [row,col] = random.random()
X.iloc [row,1] = np.nan # column 1 exists colely of NaN's
y.iloc [row] = random.randint (0, 1)
# Assign NaN's to a fraction of X
n = int(frac_nan * rows * cols)
for i in range (n):
row = random.randint (0, rows-1)
col = random.randint (0, cols-1)
X.iloc [row, col] = np.nan
# Same applies to y
n = int(frac_nan * rows)
for i in range (n):
row = random.randint (0, rows-1)
y.iloc [row,] = np.nan
return X, y
class RemoveAllMissing (BaseEstimator, TransformerMixin):
# remove columns containg NaN only
def __init__ (self, requested_cols=[]):
self.all_missing_data = requested_cols
def fit (self, X, y=None):
# find empty columns == columns with all missing data
missing_cols = X.apply (lambda x: x.count(), axis=0)
for idx in missing_cols.index:
if missing_cols [idx] == 0:
self.all_missing_data.append (idx)
return self
def transform (self, X, y=None):
print (">RemoveAllMissing - X shape: " + str (X.shape), " y shape: " + str (y.shape), 'type (X):', type(X))
for all_missing_predictor in self.all_missing_data:
del X [all_missing_predictor]
print ("<RemoveAllMissing - X shape: " + str (X.shape), " y shape: " + str (y.shape), 'type (X):', type(X))
return X, y
def fit_transform (self, X, y=None):
return self.fit (X, y).transform (X, y)
class RemoveMissingRowsBasedOnTarget (BaseEstimator, TransformerMixin):
# remove each row where target contains one or more NaN's
def __init__ (self):
self.missing_rows = []
def fit (self, X, y = None):
# remove all rows where the target value is missing data
print (type (X))
if y is None:
print ('RemoveMissingRowsBasedOnTarget: target (y) cannot be None')
return self
self.missing_rows = np.array (y.notnull()) # false = missing data
return self
def transform (self, X, y=None):
print (">RemoveMissingRowsBasedOnTarget - X shape: " + str (X.shape), " y shape: " + str (y.shape), 'type (X):', type(X))
if y is None:
print ('RemoveMissingRowsBasedOnTarget: target (y) cannot be None')
return X, y
X = X [self.missing_rows].reset_index()
del X ['index']
y = y [self.missing_rows].reset_index()
del y ['index']
print ("<RemoveMissingRowsBasedOnTarget - X shape: " + str (X.shape), " y shape: " + str (y.shape), 'type (X):', type(X))
return X, y
def fit_transform (self, X, y=None):
return self.fit (X, y).transform (X, y)
pipeline = Pipeline ([
('RemoveAllMissing', RemoveAllMissing()),
('RemoveMissingRowsBasedOnTarget', RemoveMissingRowsBasedOnTarget()),
])
X, y = create_data (25, 10, 0.1)
print ("X shape: " + str (X.shape), " y shape: " + str (y.shape), 'type (X):', type(X))
X, y = pipeline.fit_transform (X, y)
#X, y = RemoveAllMissing().fit_transform (X, y)
#X, y = RemoveMissingRowsBasedOnTarget().fit_transform (X, y)
@Vivek으로 편집 내가 문제가 격리되어 실행이 독립적 코드에 의해 원래의 코드를 교체 한 요청했다. 튜플이 DataFrame 대신 매개 변수로 전송되기 때문에 코드가 어딘가에 충돌합니다. 파이프 라인은 데이터 유형을 변경하므로 문서에서 찾을 수 없습니다. 하나는 파이프 라인에 대한 호출을 코멘트와 같이, 잘 작동 everyting 변압기의 개별 호출하기 전에 주석을 제거 할 때 :
#X, y = pipeline.fit_transform (X, y)
X, y = RemoveAllMissing().fit_transform (X, y)
X, y = RemoveMissingRowsBasedOnTarget().fit_transform (X, y)
: 또한, 나는 당신이 한 번 봐 가질 수있는 파이프 라인 내부의 목표 변수를
y
변환하기위한 몇 가지 관련 문제가있는 것을 발견 print (type (X))'그 시점에 인쇄합니까?(클래스'RemoveMissingRowsBasedOnTarget', 처음 호출했을 때) 'X '는 다음 클래스 ('RemoveAllMissing')를 호출하기위한 DataFrame이되어야하지만 그 시점에서 튜플이 된 것 같습니다 ... – Eskapp이것은에 따라 달라집니다 호출 순서 : RemoveMissingRowsBasedOnTarget이 먼저 호출되면 DataFrame이 인쇄되고 두 번째 호출되면 터플이 인쇄됩니다. 또한 오류 메시지는 rfeferred 메소드가없는 튜플에 대해 불평합니다. – Arnold
샘플 데이터와 함께 코드를 복사하기 쉽도록 추가해야합니다. –