【问题标题】:np.append not saving values in arraynp.append 不在数组中保存值
【发布时间】:2021-11-02 14:47:31
【问题描述】:

我正在编写一个程序,该程序可以绘制一个投掷物体的抛射运动图形。在嵌套循环中,我插入了一行以将计算值保存在一个 numpy 数组中,但每当我打印该数组时,只会显示初始值。

import numpy as np


v_0 = np.empty(0, dtype = float) #in m/s
theta_0 = np.empty(0, dtype = float)
t = np.arange(0, 50, .1) #time
G = -9.81 #gravity
r = 0
X = 0
Y = 0

x_val = np.array([0.0])
y_val = np.array([0.0])

for i in range(1): #will use this later for generating random values
    theta_0 = np.array([15, 20, 22, 25]) * (np.pi/180)
    v_0 = np.array([20, 24, 28, 32])
    

print("These are the angles in degrees:", theta_0 / (np.pi/180))
print("These are the initial velocities in m/s:", v_0)


for i in range(0, len(t)):
    for r in range(len(theta_0)):
        #if any(Y > 0):
        X = v_0[r] * np.cos(theta_0[r]) * t[i]
        X = np.round_(X,4)
        Y = v_0[r] * np.sin(theta_0[r]) * t[i] + 0.5 * G * (t[i])**2
        Y = np.round_(Y,4)
        np.append(x_val, X)
        np.append(y_val, Y)
            
print(x_val)
print(y_val)

【问题讨论】:

    标签: arrays numpy append


    【解决方案1】:

    在您的代码中使用:

    x_val.append(X)
    y_val.append(Y)
    

    numpy 有时不是答案,最好使用内置函数。

    此外,我查看了您的代码并对其进行了一些更改。

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    v_0 = np.empty(0, dtype = float) #in m/s
    theta_0 = np.empty(0, dtype = float)
    t = np.arange(0, 50, .1) #time
    G = -9.81 #gravity
    r = 0
    X = 0
    Y = 0
    
    
    
    for i in range(1): #will use this later for generating random values
        theta_0 = np.array([15, 20, 22, 25]) * (np.pi/180)
        v_0 = np.array([20, 24, 28, 32])
        
    
    print("These are the angles in degrees:", theta_0 / (np.pi/180))
    print("These are the initial velocities in m/s:", v_0)
    
    x_val = np.zeros((1,len(t)*len(theta_0))) #Create an array full of zeros
    y_val = np.zeros((1,len(t)*len(theta_0)))
    
    j = 0
    for i in range(0, len(t)):
        for r in range(len(theta_0)):
            #if any(Y > 0):
            X = v_0[r] * np.cos(theta_0[r]) * t[i]
            X = np.round_(X,4)
            Y = v_0[r] * np.sin(theta_0[r]) * t[i] + 0.5 * G * (t[i])**2
            Y = np.round_(Y,4)
            x_val[0,j] = X #np.append(x_val, X) - same result but better 
            y_val[0,j] = Y #np.append(y_val, Y)
            j  = j + 1
            
                
    
    
    plt.plot(x_val[0,:], y_val[0,:])
    plt.show()
    

    所以我拿走了你的np.append。我建议你避免使用它,因为打印和写入数据比使用np.zeros 在 CPU 上预留空间要慢。在您的情况下,时间差异尚未看到,但当您进行 Monte Carlo 集成或不同类型的大数据项目时,您将需要开始优化您的代码。

    这里还解释了一些可以优化代码的东西:LINK

    然后我得到了这个情节:

    【讨论】:

    • x_val.append(X)numpy 中不受支持。 x_val = np.append(x_val, X) 会工作。 y_val 相同
    • 我意识到另一个问题。我还制作了一个类似的图表,但应该为 theta_0 的每个值绘制一个单独的图表。最后制作 4 个不同的数组是解决这个问题的唯一方法,还是我仍然可以从同一个数组制作 4 个单独的图表
    • 在 plt.plot(x_val[0,:], y_val[0,:]) 之前使用表格,这样它就在 theta_0 循环内部。
    【解决方案2】:

    np.append不是 列表追加克隆。请阅读文档!如果你必须像这样迭代,坚持使用你理解的列表。

    但是你不需要迭代;使用整个数组方法:

    In [11]: theta_0 = np.array([15, 20, 22, 25]) * (np.pi/180)
        ...: v_0 = np.array([20, 24, 28, 32])
    In [12]: t = np.arange(0, 50, .1) #time
        ...: G = -9.81 #gravity
    In [13]: t = t[:,None]
    In [14]: X = v_0 * np.cos(theta_0) * t
    In [15]: X.shape
    Out[15]: (500, 4)
    In [16]: Y = v_0 * np.sin(theta_0) * t + 0.5 * G * (t)**2
    In [17]: Y.shape
    Out[17]: (500, 4)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2012-11-25
      • 1970-01-01
      相关资源
      最近更新 更多