簡體   English   中英

執行此與矩陣形狀相關的 python 代碼時出錯

[英]Getting an error while executing this python code related to the shape of the matrix

我正在嘗試執行這段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

我猜你想要:

d = d.reshape(28,28)

d.shape是形狀的元組,顯然不能用兩個參數(28 和 28)調用。 reshape返回新數組,它不會就地重新整形。

此行中的錯誤:

d.shape(28,28)

只需運行:

d.shape

Shape 是一個屬性,它返回一個表示 DataFrame 維度的元組。

如果要更改形狀,請使用:

d = d.reshape(28,28)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM