【问题标题】:Getting an error while executing this python code related to the shape of the matrix执行此与矩阵形状相关的 python 代码时出错
【发布时间】:2018-02-15 12:55:28
【问题描述】:

我正在尝试执行这段pythoncode。但这显示一个错误。帮我解决这个错误。

  import numpy as np
  import matplotlib.pyplot as pt
  import pandas as pd
  from sklearn.tree import DecisionTreeClassifier

  data=pd.read_csv('train.csv').as_matrix()
  clf=DecisionTreeClassifier()
   xtrain=data[0:21000,1:]
   train_label=data[0:21000,0]
   clf.fit(xtrain,train_label)

  xtest=data[21000:,1:]
  actual_label=data[21000:,0]

  d=xtest[8]
  d.shape(28,28)
  pt.imshow(255-d,cmap='gray')
  print(clf.predict([xtest[8]]))
  pt.show()

错误显示如下

   TypeError: 'tuple' object is not callable

【问题讨论】:

  • 请发布完整代码,以便我们尝试了解您的问题。现在,它是不完整的。同时发布错误的完整跟踪
  • 由代码@EdwinvanMierlo 提供
  • 我们能否也看到完整的堆栈跟踪,因为它通常会引用导致特定错误的行或方​​法。谢谢
  • 也有缩进问题
  • 不存在缩进问题@Narendra

标签: python python-3.x machine-learning jupyter


【解决方案1】:

我猜你想要:

d = d.reshape(28,28)

d.shape 是形状的元组,显然不能用两个参数(28 和 28)调用。 reshape 也返回新数组,它不会原地重塑。

【讨论】:

    【解决方案2】:

    这一行有错误:

    d.shape(28,28)
    

    只要运行:

    d.shape
    

    Shape 是一个属性,它返回一个表示 DataFrame 维度的元组。

    如果你想改变形状使用:

    d = d.reshape(28,28)
    

    【讨论】:

    • 再次将错误显示为 TypeError: Invalid dimensions for image data @Cezary.Sz
    • 所以新的错误在下一行,你能显示什么形状有'd'吗?
    • 我得到了答案,应该是 d= d.reshape(28,28)@Cezary.Sz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多