尝试使用追加,当我尝试打印由追加创建的列表时,它说 "None"
Trying to use append, when I try to print the list created by append it says "None"
我对 Python 和一般的编程还很陌生,只做了大约一个月。
您好,我创建了一些读取数据的代码,并通过 for 循环更改了它读取数据中的哪一列以获取 y 变量,而 x 变量保持不变。然后它提取我应用于数据的拟合参数之一(mu/mean 高斯+线拟合),然后使用此平均值计算所述固定 x 变量和数据列的速度对于 y 变量,然后将此速度存储在名为“velocity_database”的列表中。所有这些都在 for 循环中。在 for 循环之外,我希望它打印列表以便我可以看到所有速度。稍后我需要使用这些速度进行更多分析。
这是我的代码:
import numpy as np #importing necessary packages
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
from scipy.optimize import curve_fit
e=np.exp
spectral_data=np.loadtxt(r'C:/Users/Sidharth/Documents/Computing Labs/Project 1/Halpha_spectral_data.csv', delimiter=',', skiprows=2) #importing data file
v_list = [] # will store the velocities's in this list
for i in range(1, 30): # each iteration i gets a different value, starting with 1 and end with 30
x = spectral_data[:, 0]
y = spectral_data[:, i]
plt.scatter(x,y) #produce scatter plot
plt.title('Observation 1')
plt.ylabel('Intensity (arbitrary units)')
plt.xlabel('Wavelength (m)')
m,c=np.polyfit(x,y2,deg=1) #fits a linear model to the data, with m = slope and c = intercept
plt.plot(x,y,'*')
plt.plot(x,c+m*x,'-') #plots the fit
print('The slope and intercept of the regression is,', m,c)
m_best=m
c_best=c
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
initial_guess=[160,7.1*10**-7,0.2*10**-7,m_best,c_best]
po,po_cov=sp.optimize.curve_fit(fit_gauss,x,y,initial_guess)
#print("The parameters")
#print(po)
#print('The covariance matrix')
#print(po_cov)
print("The signal parameters are")
print(" Gaussian amplitude = %.1f +/- %.1f" %(po[0],sp.sqrt(po_cov[0,0])))
print(" mu = %.1e +/- %.1e"%(po[1],sp.sqrt(po_cov[1,1])))
print(" Gaussian width (sigma) = %.1f +/- %.1f"%(po[2],sp.sqrt(po_cov[2,2])))
print("and the background estimate is")
print(" m = %.2f +/- %.2f"%(po[3],sp.sqrt(po_cov[3,3])))
print(" c = %.0f +/- %.0f"%(po[4],sp.sqrt(po_cov[4,4])))
plt.plot(x,fit_gauss(x,po[0],po[1],po[2],po[3],po[4]),label='Fit results') #plots the fit just to confirm by eye whether the fit is okay
plt.legend()
plt.show()
mu_calc=po[1]
print("mu is",mu_calc)
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))
print("v is",v, "m/s")
velocity_database=v_list.append(v) # store velocity of the galaxy at the end of the growing list of velocities
print("The velocities are",velocity_database)
控制台响应为我提供了 mean/mu 的所有值和每列数据的速度,但是当我尝试在最后打印列表 (velocity_database) 时,它说:
The velocities are None
谢谢
你的列表是 v_list
,我不明白你为什么要打印 velocity_database
,它只在 for 循环中定义,并且必须像 list.append()
那样是 None
不是 return 任何东西。从您的代码设置方式来看,我猜您实际上想打印出 v_list
并向其附加所有值。
append()
方法将输入附加到现有列表 (v_list
),但它不会 return 任何东西,或者它 returns None.
所以你要的数据大概是v_list.
我对 Python 和一般的编程还很陌生,只做了大约一个月。
您好,我创建了一些读取数据的代码,并通过 for 循环更改了它读取数据中的哪一列以获取 y 变量,而 x 变量保持不变。然后它提取我应用于数据的拟合参数之一(mu/mean 高斯+线拟合),然后使用此平均值计算所述固定 x 变量和数据列的速度对于 y 变量,然后将此速度存储在名为“velocity_database”的列表中。所有这些都在 for 循环中。在 for 循环之外,我希望它打印列表以便我可以看到所有速度。稍后我需要使用这些速度进行更多分析。
这是我的代码:
import numpy as np #importing necessary packages
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
from scipy.optimize import curve_fit
e=np.exp
spectral_data=np.loadtxt(r'C:/Users/Sidharth/Documents/Computing Labs/Project 1/Halpha_spectral_data.csv', delimiter=',', skiprows=2) #importing data file
v_list = [] # will store the velocities's in this list
for i in range(1, 30): # each iteration i gets a different value, starting with 1 and end with 30
x = spectral_data[:, 0]
y = spectral_data[:, i]
plt.scatter(x,y) #produce scatter plot
plt.title('Observation 1')
plt.ylabel('Intensity (arbitrary units)')
plt.xlabel('Wavelength (m)')
m,c=np.polyfit(x,y2,deg=1) #fits a linear model to the data, with m = slope and c = intercept
plt.plot(x,y,'*')
plt.plot(x,c+m*x,'-') #plots the fit
print('The slope and intercept of the regression is,', m,c)
m_best=m
c_best=c
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
initial_guess=[160,7.1*10**-7,0.2*10**-7,m_best,c_best]
po,po_cov=sp.optimize.curve_fit(fit_gauss,x,y,initial_guess)
#print("The parameters")
#print(po)
#print('The covariance matrix')
#print(po_cov)
print("The signal parameters are")
print(" Gaussian amplitude = %.1f +/- %.1f" %(po[0],sp.sqrt(po_cov[0,0])))
print(" mu = %.1e +/- %.1e"%(po[1],sp.sqrt(po_cov[1,1])))
print(" Gaussian width (sigma) = %.1f +/- %.1f"%(po[2],sp.sqrt(po_cov[2,2])))
print("and the background estimate is")
print(" m = %.2f +/- %.2f"%(po[3],sp.sqrt(po_cov[3,3])))
print(" c = %.0f +/- %.0f"%(po[4],sp.sqrt(po_cov[4,4])))
plt.plot(x,fit_gauss(x,po[0],po[1],po[2],po[3],po[4]),label='Fit results') #plots the fit just to confirm by eye whether the fit is okay
plt.legend()
plt.show()
mu_calc=po[1]
print("mu is",mu_calc)
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))
print("v is",v, "m/s")
velocity_database=v_list.append(v) # store velocity of the galaxy at the end of the growing list of velocities
print("The velocities are",velocity_database)
控制台响应为我提供了 mean/mu 的所有值和每列数据的速度,但是当我尝试在最后打印列表 (velocity_database) 时,它说:
The velocities are None
谢谢
你的列表是 v_list
,我不明白你为什么要打印 velocity_database
,它只在 for 循环中定义,并且必须像 list.append()
那样是 None
不是 return 任何东西。从您的代码设置方式来看,我猜您实际上想打印出 v_list
并向其附加所有值。
append()
方法将输入附加到现有列表 (v_list
),但它不会 return 任何东西,或者它 returns None.
所以你要的数据大概是v_list.