Arrays 如何将用python计算的数据以迭代方式添加到2d numpy数组中

Arrays 如何将用python计算的数据以迭代方式添加到2d numpy数组中,arrays,numpy,multidimensional-array,scipy,append,Arrays,Numpy,Multidimensional Array,Scipy,Append,我定义了一个2d numpy数组,如下所示: spectral_array=np.empty((30,2),int) #Importing the Spectral Data def fit_gauss(x,a,mu,sig,m,c): gaus = a*sp.exp(-(x-mu)**2/(2*sig**2)) line = m*x+c return gaus + line #plotting the fit using the ga

我定义了一个2d numpy数组,如下所示:

spectral_array=np.empty((30,2),int) #Importing the Spectral Data
def fit_gauss(x,a,mu,sig,m,c):
        gaus = a*sp.exp(-(x-mu)**2/(2*sig**2))
        line = m*x+c
        return gaus + line
    
#plotting the fit using the gaussian model
for i in range(1, 31): # each iteration i gets a different value, starting with 1 and end with 30
    x = spectral_data[:, 0]
    y = spectral_data[:, i]
#A gaussian fit is applied here, which finds a mean, and that mean is used to calculate the velocity, v below. This is irrelevant to the question but I've just kept it here#
    light_speed=3*10**8
    expect_lambda_square=(656.28*10**-9)**2
    v=-1*((expect_lambda_square*light_speed)-((mu_calc**2)*(light_speed)))/(expect_lambda_square+(mu_calc**2)) #calculating velocity given the mean from the gaussian fits for each observation
    v_kms=v/1000
    print("v is",v_kms, "km/s")
    velocity_database=v_list.append(v_kms) # store velocity of the galaxy at the end of the growing list of velocities
print("The velocities are",v_list)
with open('C:/Users/Sidharth/Documents/Computing Labs/Project 1/Halpha_spectral_data.csv','r') as file:
        line1=file.readline()
        line2=file.readline()
        line1_split=line1.split(',')
        line2_split=line2.split(',')
        str1=('White Space')
        str2=str1.strip()
        
    print(line2_split)
    observation=line2_split
    observation=observation[1:]
    print(observation)
    observation_int = [int(item.split(' ')[1]) for item in observation]
    print(observation_int)
这将创建一个包含30行和2列的空数组。第一列应该对应于“观测数字”,第二列应该对应于“速度”(这些标题不相关,不需要打印,它正好是我希望最终数据在数组中结束的位置)

通过一个函数计算速度,该函数通过一个for循环对某些给定数据进行计算,for循环对给定数据中的每列进行计算,如下所示:

spectral_array=np.empty((30,2),int) #Importing the Spectral Data
def fit_gauss(x,a,mu,sig,m,c):
        gaus = a*sp.exp(-(x-mu)**2/(2*sig**2))
        line = m*x+c
        return gaus + line
    
#plotting the fit using the gaussian model
for i in range(1, 31): # each iteration i gets a different value, starting with 1 and end with 30
    x = spectral_data[:, 0]
    y = spectral_data[:, i]
#A gaussian fit is applied here, which finds a mean, and that mean is used to calculate the velocity, v below. This is irrelevant to the question but I've just kept it here#
    light_speed=3*10**8
    expect_lambda_square=(656.28*10**-9)**2
    v=-1*((expect_lambda_square*light_speed)-((mu_calc**2)*(light_speed)))/(expect_lambda_square+(mu_calc**2)) #calculating velocity given the mean from the gaussian fits for each observation
    v_kms=v/1000
    print("v is",v_kms, "km/s")
    velocity_database=v_list.append(v_kms) # store velocity of the galaxy at the end of the growing list of velocities
print("The velocities are",v_list)
with open('C:/Users/Sidharth/Documents/Computing Labs/Project 1/Halpha_spectral_data.csv','r') as file:
        line1=file.readline()
        line2=file.readline()
        line1_split=line1.split(',')
        line2_split=line2.split(',')
        str1=('White Space')
        str2=str1.strip()
        
    print(line2_split)
    observation=line2_split
    observation=observation[1:]
    print(observation)
    observation_int = [int(item.split(' ')[1]) for item in observation]
    print(observation_int)
我希望在计算完这些速度后,立即将其插入2d numpy数组的第二列。

给定数据中包含一个“观测编号:(此处编号)”是用于计算v的每列的列标题我必须确保观测值与使用给定数据(该特定观测值)列进行计算的速度一致。这应该打印在2d numpy阵列的第一列中,在第二列中对应的速度旁边。我已经从列标题导入了观测值的整数值,如下所示:

spectral_array=np.empty((30,2),int) #Importing the Spectral Data
def fit_gauss(x,a,mu,sig,m,c):
        gaus = a*sp.exp(-(x-mu)**2/(2*sig**2))
        line = m*x+c
        return gaus + line
    
#plotting the fit using the gaussian model
for i in range(1, 31): # each iteration i gets a different value, starting with 1 and end with 30
    x = spectral_data[:, 0]
    y = spectral_data[:, i]
#A gaussian fit is applied here, which finds a mean, and that mean is used to calculate the velocity, v below. This is irrelevant to the question but I've just kept it here#
    light_speed=3*10**8
    expect_lambda_square=(656.28*10**-9)**2
    v=-1*((expect_lambda_square*light_speed)-((mu_calc**2)*(light_speed)))/(expect_lambda_square+(mu_calc**2)) #calculating velocity given the mean from the gaussian fits for each observation
    v_kms=v/1000
    print("v is",v_kms, "km/s")
    velocity_database=v_list.append(v_kms) # store velocity of the galaxy at the end of the growing list of velocities
print("The velocities are",v_list)
with open('C:/Users/Sidharth/Documents/Computing Labs/Project 1/Halpha_spectral_data.csv','r') as file:
        line1=file.readline()
        line2=file.readline()
        line1_split=line1.split(',')
        line2_split=line2.split(',')
        str1=('White Space')
        str2=str1.strip()
        
    print(line2_split)
    observation=line2_split
    observation=observation[1:]
    print(observation)
    observation_int = [int(item.split(' ')[1]) for item in observation]
    print(observation_int)
我相信上面的内容应该放在运行速度计算的for循环中,这样就可以在计算速度的同时将观测值插入到2d numpy数组中


最后,我应该能够按观察数升序排列2d numpy阵列。

alist=[]
alist。append(…)
是一种创建列表的好方法。不应该用数组来模仿它。为预定义数组的元素(或者更好的是切片)赋值是有效的(但不要称之为追加)。如果要分配所有元素,则可以使用
np.empty((n,m))
创建数组;我喜欢
np.zero
。谢谢。我已相应地调整了标题。