GVKun编程网logo

AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'(python报错对象没有属性)

1

这篇文章主要围绕AttributeError:'numpy.ndarray'对象没有属性'fromarray'和python报错对象没有属性展开,旨在为您提供一份详细的参考资料。我们将全面介绍Attr

这篇文章主要围绕AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'python报错对象没有属性展开,旨在为您提供一份详细的参考资料。我们将全面介绍AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'的优缺点,解答python报错对象没有属性的相关问题,同时也会为您带来'<=' 在 'numpy.ndarray' 和 'numpy.ndarray' 的实例之间不受支持但 LHS 是 pd.Timestamp、'numpy.ndarray' 对象没有属性 'backprop'、'numpy.ndarray' 对象没有属性 'bar'、'numpy.ndarray' 对象没有属性 'D'的实用方法。

本文目录一览:

AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'(python报错对象没有属性)

AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'(python报错对象没有属性)

如何解决AttributeError: ''numpy.ndarray'' 对象没有属性 ''fromarray''

我有一个形状为 (256,256)

的图像数组
  1. array([[ 1.83929426,2.27074315,2.5218986,...,5.58716559,7.68013398,8.48954239],[ 7.7554863,1.80131326,4.08562626,11.43703752,10.6897957,2.21642752],[ 3.5976206,17.32215998,3.94093576,2.92133073,5.27458118,6.51157218],[12.61920987,5.42883218,4.91278887,11.86499776,1.95860585,1.96206125],[ 4.18047027,2.85057039,11.86617946,3.56434097,8.87239311,2.14273459],[ 5.36091217,9.16876533,2.52525961,8.17177924,3.85081341,6.46705723]])

当我尝试使用 Image.fromarray() 加载时出现错误 AttributeError: ''numpy.ndarray'' object has no attribute ''fromarray''

代码:

  1. import numpy as np
  2. from scipy.io import loadmat
  3. import matplotlib.pyplot as plt
  4. matcontent = loadmat(''/content/PIfile_new.mat'',appendmat=True)
  5. matcontent.keys()
  6. highest = np.max(matcontent[''I32''])
  7. lowest = np.min(matcontent[''I32''])
  8. print(highest,lowest)
  9. Image = np.divide(matcontent[''I32''],highest )
  10. highest = np.max(Image)
  11. lowest = np.min(Image)
  12. print(highest,lowest)
  13. Image_255 = Image*255

输出: 283.52991767049167 0.09813473030519577 1.0 0.000346117725817014

  1. Image.fromarray(Image_255)

错误:

  1. AttributeError Traceback (most recent call last)
  2. <ipython-input-28-698d66d71bc6> in <module>()
  3. ----> 1 Image.fromarray(Image_255)
  4. AttributeError: ''numpy.ndarray'' object has no attribute ''fromarray''

'<=' 在 'numpy.ndarray' 和 'numpy.ndarray' 的实例之间不受支持但 LHS 是 pd.Timestamp

'<=' 在 'numpy.ndarray' 和 'numpy.ndarray' 的实例之间不受支持但 LHS 是 pd.Timestamp

如何解决''<='' 在 ''numpy.ndarray'' 和 ''numpy.ndarray'' 的实例之间不受支持但 LHS 是 pd.Timestamp

代码:

print(min_time.__class__,min_time,times.__class__)
print(max_time.__class__,max_time)
mask = (times >= min_time) * (times <= max_time)

产量:

''pandas._libs.tslibs.timestamps.Timestamp''> 2020-02-12 00:00:00+00:00

''pandas._libs.tslibs.timestamps.Timestamp''> 2020-02-12 00:00:00+00:00

''

什么给? LHS 显然是 pandas.Timestamp,RHS 显然是 numpy.ndarray。我的类型发生了什么神奇的事情吗?

'numpy.ndarray' 对象没有属性 'backprop'

'numpy.ndarray' 对象没有属性 'backprop'

如何解决''numpy.ndarray'' 对象没有属性 ''backprop''

我正在尝试计算 CrossEntropy 损失的导数。下面是代码部分。我收到以下错误。是什么原因?

AttributeError: ''numpy.ndarray'' 对象没有属性 ''backprop''

你能帮我吗?非常感谢!

  1. class CrossEntropyLoss:
  2. def __init__(self):
  3. """Initialize the loss module."""
  4. self.pred = None
  5. def __call__(self,pred,target):
  6. """This is needed to be able to use the object like a function. The task
  7. is handled by the forward function.
  8. pred: A module object (layer or non-linearity)
  9. target: Numpy array storing the correct/target values.
  10. """
  11. return self.forward(pred,target)

我计算了 CrossEntroy 损失。

  1. def forward(self,target):
  2. """Feedforward processing through the loss.
  3. pred: A module object (layer or non-linearity). Assumed to be scores.
  4. target: Numpy array storing the correct/target values.
  5. Returns: The scalar loss value.
  6. """
  7. self.pred = pred # save input object for later use
  8. self.target = target # save target for later use
  9. self.scores = pred.out # get the scores from the input object
  10. self.N = self.scores.shape[0]
  11. self.probs = None # Save calculated probabilities into this
  12. loss = None # Save calculated loss into this
  13. ###########################################################################
  14. # @Todo: (i) Convert scores to probabilities and save it in self.probs #
  15. # (ii) Calculate Cross-entropy loss. Note that this will be a #
  16. # scalar. #
  17. # Don''t add weight regularization here. We implement it directly as #
  18. # weight decay. Don''t forget to normalize the loss. #
  19. ###########################################################################
  20. self.target=self.scores.shape[0]
  21. self.pred=self.scores
  22. self.probs=np.exp(self.pred)/np.sum(np.exp(self.pred))
  23. log=-np.log(self.probs)
  24. loss=np.sum(np.dot(self.target,log))
  25. loss+=(self.target-self.pred)/self.N
  26. pass
  27. ##########################################################################
  28. # END OF YOUR CODE #
  29. ##########################################################################
  30. return loss

**我想计算损失的导数,但我在 self.pred.backprop(self.dscores) **

  1. def backprop(self):
  2. """Start backpropagate with this loss deFinition. Calculate the derivative
  3. wrt. the scores and pass that to the earlier module
  4. via self.inp.backprop(self.dscores).
  5. """
  6. self.dscores = None # Calculate and save the derivative wrt the scores here
  7. ###########################################################################
  8. # @Todo: Calculate self.dscores. Don''t forget to normalize the gradient. #
  9. ###########################################################################
  10. self.dscores=self.pred-1
  11. self.dscores/=self.N
  12. pass
  13. ##########################################################################
  14. # END OF YOUR CODE #
  15. ##########################################################################
  16. self.pred.backprop(self.dscores)

错误

  1. > >
  2. > --------------------------------------------------------------------------- AttributeError Traceback (most recent call
  3. > last) <ipython-input-270-9c36ff3edc52> in <module>()
  4. > 39 preds = model(X_val)
  5. > 40 loss_fn(preds,y_val)
  6. > ---> 41 loss_fn.backprop()
  7. > 42 grad_numerical = grad_check_sparse(f,x=model.fc1.W,analytic_grad=model.fc1.dW,num_checks=10)
  8. > 43
  9. >
  10. > <ipython-input-269-559ef6f7f0c9> in backprop(self)
  11. > 62 # END OF YOUR CODE #
  12. > 63 ##########################################################################
  13. > ---> 64 self.pred.backprop(self.dscores)
  14. > 65
  15. > 66 def update(self,learning_rate):
  16. >
  17. > AttributeError: ''numpy.ndarray'' object has no attribute ''backprop''

'numpy.ndarray' 对象没有属性 'bar'

'numpy.ndarray' 对象没有属性 'bar'

如何解决''numpy.ndarray'' 对象没有属性 ''bar''

我正在尝试为 uni 任务制作堆叠条形图。我不断收到错误消息(''numpy.ndarray'' 对象没有属性 ''bar'')。有人可以帮我吗:D。 找到下面的代码:

x = np.array(x)
data = x
attack,work,= data.shape
fig,ax = plt.subplots(nrows=1,ncols=2,figsize=(14,5),dpi=100)

x_pos = np.arange(work)
width = 0.2

colours =  [''red'',''yellow'',''blue'',''green'']
labels = [''Cyber incident'',''Theft of paperwork or data  storagedevice'',''Rogue employee'',''Social engineering and impersonation'']
rotation = 90
for i in range(attack):
    ax.bar(x_pos + i*width,data[:,],width=width,color=colours[i],label=labels[i],align=''center'')
    i=i+1
    
ax.legend()

ax.set_xlabel("the top five industry sectors")
ax.set_ylabel("Number of attack")
ax.set_title("Type of attack by top five industry sectors")

ax.set_xticks(x_pos+width)
ax.set_xticklabels(labels,rotation)

'numpy.ndarray' 对象没有属性 'D'

'numpy.ndarray' 对象没有属性 'D'

如何解决''numpy.ndarray'' 对象没有属性 ''D''

  1. import numpy as np
  2. rowlist = np.array([[0,2,3,4,5],[0,2],[1,6,7],9,9]])
  3. new_rowlist = []
  4. rows_left = set(range(len(rowlist)))
  5. col_label_list = sorted(rowlist[0].D,key=hash)
  6. for c in col_label_list:
  7. rows_with_non_zero = [ r for r in rows_left if rowlist[r][c] != 0 ]
  8. if rows_with_non_zero != []:
  9. pivot = rows_with_non_zero[0]
  10. new_rowlist.append(rowlist[pivot])
  11. rows_left.remove(pivot)
  12. for r in new_rowlist:
  13. print(r)

所以我正在学习 Philip Klein 书籍课程中的 Coding the Matrix 以及关于 Gaussian Elimination 的一章,这一直在错误“numpy.ndarray”对象没有属性“D” 我想要的是能够对名为 rowlist 的矩阵进行排序。知道如何克服这个吗?如果有任何帮助,我会在 jupyter notebook 上执行此操作 提前致谢!

解决方法

有一个错字。没有rowlist[0].D,换行如下​​

  1. col_label_list = sorted(rowlist[0],key=hash)

另外,也许从下次学习过程开始,通过定位导致错误的行来尝试自己调试代码(如果您正确阅读了错误消息)。

我们今天的关于AttributeError: 'numpy.ndarray' 对象没有属性 'fromarray'python报错对象没有属性的分享已经告一段落,感谢您的关注,如果您想了解更多关于'<=' 在 'numpy.ndarray' 和 'numpy.ndarray' 的实例之间不受支持但 LHS 是 pd.Timestamp、'numpy.ndarray' 对象没有属性 'backprop'、'numpy.ndarray' 对象没有属性 'bar'、'numpy.ndarray' 对象没有属性 'D'的相关信息,请在本站查询。

本文标签: