在模型预测中列出类中的所有输入

我有以下代码,一个非常简单的模型,使用基于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']

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注