我有以下代码,通过以下代码运行:
从一个真实的分布中提取一些要点。使用这些点与curve_fit一起提取参数。检查这些参数是否平均接近真值。(您可以通过创建“拉分布”来做到这一点,并查看它是否返回一个标准的正常变量。
# This script calculates the mean and standard deviation for
# the pull distributions on the estimators that curve_fit returns
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import gauss
import format
numTrials = 10000
# Pull given by (a_j - a_true)/a_error)
error_vec_A = []
error_vec_mean = []
error_vec_sigma = []
# Loop to determine pull distribution
for i in xrange(0,numTrials):
# Draw from primary distribution
mean = 0; var = 1; sigma = np.sqrt(var);
N = 20000
A = 1/np.sqrt((2*np.pi*var))
points = gauss.draw_1dGauss(mean,var,N)
# Histogram parameters
bin_size = 0.1; min_edge = mean-6*sigma; max_edge = mean+9*sigma
Nn = (max_edge-min_edge)/bin_size; Nplus1 = Nn + 1
bins = np.linspace(min_edge, max_edge, Nplus1)
# Obtain histogram from primary distributions
hist, bin_edges = np.histogram(points,bins,density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
# Initial guess
p0 = [5, 2, 4]
coeff, var_matrix = curve_fit(gauss.gaussFun, bin_centres, hist, p0=p0)
# Get the fitted curve
hist_fit = gauss.gaussFun(bin_centres, *coeff)
# Error on the estimates
error_parameters = np.sqrt(np.array([var_matrix[0][0],var_matrix[1][1],var_matrix[2][2]]))
# Obtain the error for each value: A,mu,sigma
A_std = (coeff[0]-A)/error_parameters[0]
mean_std = ((coeff[1]-mean)/error_parameters[1])
sigma_std = (np.abs(coeff[2])-sigma)/error_parameters[2]
# Store results in container
error_vec_A.append(A_std)
error_vec_mean.append(mean_std)
error_vec_sigma.append(sigma_std)
# Plot the distribution of each estimator
plt.figure(1); plt.hist(error_vec_A,bins,normed=True); plt.title('Pull of A')
plt.figure(2); plt.hist(error_vec_mean,bins,normed=True); plt.title('Pull of Mu')
plt.figure(3); plt.hist(error_vec_sigma,bins,normed=True); plt.title('Pull of Sigma')
# Store key information regarding distribution
mean_A = np.mean(error_vec_A); sigma_A = np.std(error_vec_A)
mean_mu = np.mean(error_vec_mean); sigma_mu = np.std(error_vec_mean)
mean_sigma = np.mean(error_vec_sigma); sigma_sig = np.std(error_vec_sigma)
info = np.array([[mean_A,sigma_A],[mean_mu,sigma_mu],[mean_sigma,sigma_sig]])我的问题是,我不知道如何使用python将数据格式化为表。我必须手动进入变量,然后转到google来显示信息。我只是想知道我怎么能用熊猫或其他图书馆。
下面是手动插入的一个示例:
Trial 1 Trial 2 Trial 3
Seed [0.2,0,1] [10,2,5] [5,2,4]
Bins for individual runs 20 20 20
Points Thrown 1000 1000 1000
Number of Runs 5000 5000 5000
Bins for pull dist fit 20 20 20
Mean_A -0.11177 -0.12249 -0.10965
sigma_A 1.17442 1.17517 1.17134
Mean_mu 0.00933 -0.02773 -0.01153
sigma_mu 1.38780 1.38203 1.38671
Mean_sig 0.05292 0.06694 0.04670
sigma_sig 1.19411 1.18438 1.19039我想自动化这个表,所以如果我在代码中更改参数,我将得到一个新的表,其中包含新的数据。
发布于 2014-07-16 22:00:08
我将与CSV模块一起生成一个体面的表。
发布于 2014-07-17 02:25:58
如果您还没有使用它,那么IPython笔记本非常适合呈现丰富显示格式。它在很多其他方面也很好。
当熊猫数据call对象是单元格中的最后一个未返回的值时,或者如果您显式调用Ipython.core.display.display函数而不是打印,它将将它们呈现为html表。
如果你还没有使用熊猫,我强烈建议你这样做。它基本上是2D和3D numpy数组的包装器;它同样快,但是它有很好的命名约定、数据分组和过滤功能,以及其他一些很酷的东西。
在这一点上,这取决于你想如何呈现它。您可以使用nbconvert转换将整个笔记本渲染为静态html或pdf。您可以将html表复制粘贴到Excel、PowerPoint或电子邮件中.
https://stackoverflow.com/questions/24791379
复制相似问题