我有以下代码,一个非常简单的模型,使用基于BaseEstimator和ClassifierMixin的python中的sklearn。它旨在报告一个城市(X)的预测分数(y)。在这里,作为一个简单模型,我只希望它在调用城市时报告该城市的平均分数作为其预测值。
class MeanClassifier(BaseEstimator, ClassifierMixin): def __inif__(self): self.cityid_ = [] self.cntX = [] def X3(self, X): self.cityid_, idx = np.unique(X, return_inverse = True) self.cntX = map(list(self.cityid_).index, X) return self.cntX def fit(self, X, y): self.meanclasses_, meanindicies = np.unique(y, return_inverse = True) self.cityid_, idx = np.unique(X, return_inverse = True) self.df = pd.DataFrame({"X":X, "y":y}) self.mean_ = self.df.groupby(['X'].mean()) def predict(self, X): return self.df['y']['X']
为了使用这个类,我有B,其中城市是作为X的城市列表,stars作为y在类中使用。
B = MeanClassifier()asncityid = cityB.fit(asncityid, stars)pred = B.predict(asncityid[2]) #使用城市列表中的第三个城市进行预测print(pred)
当我运行这段代码时,我收到了以下错误
`File "ml2_cp.py", line 66, in <module> pred = B.predict(asncityid[2]) File "ml2_cp.py", line 58, in predict return self.df['y']['X'] ## 使用sklearn需要所有X输入 File "/opt/conda/lib/python2.7/site-packages/pandas/core/series.py", line 583, in __getitem__ result = self.index.get_value(self, key) File "/opt/conda/lib/python2.7/site-packages/pandas/indexes/base.py", line 1980, in get_value tz=getattr(series.dtype, 'tz', None)) File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3332) File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:3035) File "pandas/index.pyx", line 161, in pandas.index.IndexEngine.get_loc (pandas/index.c:4084)KeyError: 'X'`
我感到非常困惑,但是,如何在def predict(self, X)
中保留整个X列表,我确信我的写法不对,因为我也在其中包含了y
。请告诉我任何可能的解决方案,如果我的代码和问题不清楚,我愿意进一步解释。非常感谢您。
回答:
我认为你可能希望使用
self.mean_ = self.df.groupby(['X']).mean()
而不是
self.mean_ = self.df.groupby(['X'].mean())
并且
return self.mean_.ix[X].values
而不是
return self.df['y']['X']