执行此python代码时出错

时间:2020-12-22 21:50:35

I am trying to excute this piece of pythoncode.but this is showing an error.Help me to resolve this error.

我试图执行这段pythoncode.but这显示错误。帮助我解决此错误。

  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()

ERROR IS SHOWING LIKE THIS

错误就像这样

   TypeError: 'tuple' object is not callable

2 个解决方案

#1


0  

I'm guessing you wanted:

我猜你想要的是:

d = d.reshape(28,28)

d.shape is a tuple of the shape, and obviously can't be called with two parameters (28 and 28). Also reshape returns the new array, it does not reshape in place.

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

#2


0  

Error in this line:

这行错误:

d.shape(28,28)

Just run:

d.shape

Shape is an attribute that return a tuple representing the dimensionality of the DataFrame.

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

If you want to change the shape use:

如果要更改形状,请使用:

d = d.reshape(28,28)

#1


0  

I'm guessing you wanted:

我猜你想要的是:

d = d.reshape(28,28)

d.shape is a tuple of the shape, and obviously can't be called with two parameters (28 and 28). Also reshape returns the new array, it does not reshape in place.

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

#2


0  

Error in this line:

这行错误:

d.shape(28,28)

Just run:

d.shape

Shape is an attribute that return a tuple representing the dimensionality of the DataFrame.

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

If you want to change the shape use:

如果要更改形状,请使用:

d = d.reshape(28,28)