首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取切片:“(TypeError( None,None,None),0)”是无效的键

获取切片:“(TypeError( None,None,None),0)”是无效的键
EN

Stack Overflow用户
提问于 2019-03-22 09:39:11
回答 12查看 131K关注 0票数 34

尝试绘制k-NN分类器的决策边界,但无法获得TypeError: '(slice(None, None, None), 0)' is an invalid key

代码语言:javascript
复制
h = .01  # step size in the mesh

# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#AFAFAF'])
cmap_bold  = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#AFAFAF'])

for weights in ['uniform', 'distance']:
    # we create an instance of Neighbours Classifier and fit the data.
    clf = KNeighborsClassifier(n_neighbors=6, weights=weights)
    clf.fit(X_train, y_train)

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("4-Class classification (k = %i, weights = '%s')"
              % (n_neighbors, weights))

plt.show()

我在运行时得到了这个,不太确定这意味着什么我不认为clf.fit有问题,但我不确定

代码语言:javascript
复制
  TypeError                                 Traceback (most recent call last)
<ipython-input-394-bef9b05b1940> in <module>
     12         # Plot the decision boundary. For that, we will assign a color to each
     13         # point in the mesh [x_min, x_max]x[y_min, y_max].
---> 14         x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
     15         y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
     16         xx, yy = np.meshgrid(np.arange(x_min, x_max, h),

~\Miniconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

~\Miniconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2654                                  'backfill or nearest lookups')
   2655             try:
-> 2656                 return self._engine.get_loc(key)
   2657             except KeyError:
   2658                 return self._engine.get_loc(self._maybe_cast_indexer(key))

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(slice(None, None, None), 0)' is an invalid key
EN

回答 12

Stack Overflow用户

发布于 2019-08-09 17:31:46

由于您尝试以数组的形式直接访问,因此会遇到这个问题。试试这个:

代码语言:javascript
复制
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean',verbose=0)
imputer = imputer.fit(X.iloc[:, 1:3])
X.iloc[:, 1:3] = imputer.transform(X.iloc[:, 1:3])

使用iloc/loc可以解决此问题。

票数 41
EN

Stack Overflow用户

发布于 2019-05-19 20:16:38

您需要使用iloc/loc来访问df。尝试将iloc添加到X,以便X.iloc[:, 0]

票数 25
EN

Stack Overflow用户

发布于 2019-09-20 04:41:21

我遇到了以下相同的问题

代码语言:javascript
复制
X = dataset.iloc[:,:-1]

然后我添加了.values属性,之后它就可以正常工作了

代码语言:javascript
复制
X = dataset.iloc[:,:-1].values
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55291667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档