我想創建我自己的變壓器以與sklearn管道一起使用。因此,我創建了一個實現擬合和變換方法的類。變壓器的目的是從矩陣中移除具有超過指定數量的NaN的行。所以我面臨的問題是如何改變傳遞給變換器的X和Y矩陣?我相信這必須以適合的方法來完成,因爲它可以訪問X和Y.由於python通過賦值傳遞參數,所以我將X重新分配給一個具有較少行的新矩陣,原始X的引用將丟失(當然對於y也是如此)。是否有可能保持這一參考?用於sklearn的定製變壓器改變X和y的管道
我正在使用熊貓DataFrame輕鬆刪除具有太多NaN的行,這可能不是正確的方式來做我的用例。當前的代碼如下所示:
class Dropna():
# thresh is max number of NaNs allowed in a row
def __init__(self, thresh=0):
self.thresh = thresh
def fit(self, X, y):
total = X.shape[1]
# +1 to account for 'y' being added to the dframe
new_thresh = total + 1 - self.thresh
df = pd.DataFrame(X)
df['y'] = y
df.dropna(thresh=new_thresh, inplace=True)
X = df.drop('y', axis=1).values
y = df['y'].values
return self
def transform(self, X):
return X
這是待辦事項。 – 2014-08-28 18:09:02
A [相關鏈接](https://github.com/scikit-learn/scikit-learn/issues/3855)到@AndreasMueller提到的待辦事項。 – zaxliu 2015-11-18 00:26:50