AttributeError:"numpy.ndarray"对象没有属性"cos" [英] AttributeError: 'numpy.ndarray' object has no attribute 'cos'

查看:152
本文介绍了AttributeError:"numpy.ndarray"对象没有属性"cos"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么我的代码中出现上述错误.该程序假设使用给定两个输入参数的牛顿拉夫森法来查找4 bar连杆中连杆的位置.

I'm trying to figure out why the above error came up in my code. The program is suppose to use the Newton Raphson Method to find the position of links in a 4 bar linkage given two input parameters.

此行中发生错误

g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3

预先感谢您的帮助.

import numpy as np

L1=1
L2=1.5 * L1
theta = 40 * np.pi / 180

#initial guesses

L3 = 1.5
alpha = 30 * np.pi / 180
epsilon = 1
n = 0

while epsilon > 0.0001:
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
    dg1dalpha = -L2 * np.sin(alpha)
    dg1dL3 = -1;

    g2 = L1 * np.sin(theta) - L2 * np.sin(alpha)
    dg2dalpha = -L2 * np.cos(alpha);
    dg2dL3 = 0

    J = np.array([[dg1dalpha, dg1dL3], [dg2dalpha, dg2dL3]])

    s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])

    epsilon_alpha = abs(s[0] - alpha)

    epsilon_L3 = abs(s[1] - L3)

    epsilon = max(epsilon_alpha.all, epsilon_L3.all)

    alpha = s[0]

    L3 = s[1]

    n = n + 1

print(n, alpha, L3)

推荐答案

在Python2.7中,在循环开始时添加print('alpha',alpha)会产生:

In Python2.7 adding a print('alpha',alpha) at the start of the loop produces:

('alpha', 0.5235987755982988)
('alpha', array([ 1.85083849,  2.29325173]))
('alpha', array([[array([ 1.98227296,  1.95343536]), 1.7138098231972174],
       [array([ 1.81303794,  1.7604074 ]), 2.2932517265367176]], dtype=object))
Traceback (most recent call last):
  File "stack32444132.py", line 17, in <module>
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
AttributeError: 'numpy.ndarray' object has no attribute 'cos'

因此,错误是由调用np.cos(alpha)引起的,其中alpha是对象数组. alpha(2,2);第一列包含长度为2的数组;第二个包含浮点数.

So the error is caused by calling np.cos(alpha) where alpha is an object array. alpha is (2,2); the 1st column contains length 2 arrays; the 2nd contains floats.

因此,在循环中的某个时刻,您将追加或合并不同长度的数组或列表.

So at some point in the loop you are appending or conconatenating arrays or lists of different length.

s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])
alpha = s[0]

添加更多打印件(在s之前)

adding some more prints (before s)

('J', (2, 2), dtype('float64'))
('alpha', 0.5235987755982988)
('L3', 1.5)
....
('J', (2, 2), dtype('O'))   
('alpha', array([ 1.85083849,  2.29325173]))
('L3', array([-10.61649234,   1.5       ]))

在第二个循环中,J从2x2的float矩阵变为2x2的对象.

In the 2nd loop J changes from a 2x2 matrix of floats to a 2x2 of objects.

在第一次遇到epsilon = max(epsilon_alpha.all, epsilon_L3.all)表达式时,Python3会引发一个错误. epsilon_alpha.all是一种方法; epsilon_alpha.all()是布尔值.但是,即使当epsilon_alpha成为数组时,也会产生错误.

Python3 raises an error earlier, at the first encounter of the epsilon = max(epsilon_alpha.all, epsilon_L3.all) expression. epsilon_alpha.all is a method; epsilon_alpha.all() is a boolean. But even that produces an error when epsilon_alpha becomes an array.

好,此循环运行(alpha仍然是标量);它不会停止,可能是因为epsilon不够小.但我会留给你.

OK, this loop runs (alpha remains a scalar); it doesn't stop, presumably because epsilon does not get small enough; but I'll leave that to you.

while epsilon > 0.0001:
    # print('alpha', alpha)
    g1 = L1 * np.cos(theta) + L2 * np.cos(alpha) - L3
    dg1dalpha = -L2 * np.sin(alpha)
    dg1dL3 = -1;

    g2 = L1 * np.sin(theta) - L2 * np.sin(alpha)
    dg2dalpha = -L2 * np.cos(alpha);
    dg2dL3 = 0

    J = np.array([[dg1dalpha, dg1dL3], [dg2dalpha, dg2dL3]])
    print('J', J.shape,J.dtype)  # (2,2) floats
    s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])
    s = s[:,0]  # fudge to turn (2,2) array into a (2,) array

    epsilon_alpha = abs(s[0] - alpha)
    epsilon_L3 = abs(s[1] - L3) 
    epsilon = max(epsilon_alpha, epsilon_L3)
    # max on 2 scalars is ok

    alpha = s[0] # scalar
    L3 = s[1]    # scalar
    n = n + 1

问题的根源在于

 s = np.array([[alpha], [L3]]) - J/np.array([[g1], [g2]])

如果alphaL3是标量,则np.array([[alpha], [L3]])(2,1). np.array([[g1], [g2]])也是.但是由于J是(2,2),所以s也是(2,2).但是您一直在使用s[0]s[1],显然假设s是`(2,).

If alpha and L3 are scalars, then np.array([[alpha], [L3]]) is (2,1). So is np.array([[g1], [g2]]). But since J is (2,2), s is also (2,2). But you've been using s[0] and s[1], evidently assuming s is `(2,).

 s = s[:,0] 

s设为(2,),以便其余代码工作.由于epsilon无法收敛,因此可能是错误的解决方法.

makes s a (2,) so the rest of the code works. Since epsilon does not converge it probably is the wrong fix.

我想强调一下-开发numpy代码时,请密切注意数组的形状.如果形状不正确,您将得到类似这样的错误.以我的经验,正确调整形状是调试工作的80%.

May I stress - when developing numpy code, pay close attention to the array shapes. If the shapes are wrong you'll get errors like this. In my experience getting the shapes right is 80% of the debugging work.

这篇关于AttributeError:"numpy.ndarray"对象没有属性"cos"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆