2014-07-16 18 views
0

我有通过以下运行下面的代码物体其他:方法来创建表和像样的地块比在Python

从真实分布绘制多个点。 将这些点与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 

我想自动化这个表,所以如果我在我的代码中更改我的参数,我会得到一个包含新数据的新表。

+1

我很好奇为什么这个问题被拒绝。合法的问题,国际海事组织。 – ericmjl

+1

另一方面,你在正确的轨道上思考熊猫。你有没有试过阅读API文档?有几个很好的例子可以说明:(1)将列表列表转换为DataFrame;(2)将DataFrames保存为CSV文件。 – ericmjl

+0

@ericmjl 谢谢! 我以前做过,但我没有能够输出数据表,因为我想要的。你提到的那些例子应该有所帮助。 – alvarezcl

回答

1

我会用CSV module来生成一个可以呈现的表格。

1

如果你还没有使用它,IPython notebook非常适合渲染rich display格式。这在很多其他方面也非常好。

它会将大熊猫数据框对象呈现为html表格,当它们是单元格中的最后一个未返回值时,或者如果明确地调用Ipython.core.display.display函数而不是打印。

如果你还没有使用熊猫,我强烈推荐它。它基本上是2D & 3D numpy阵列的包装;它速度一样快,但它有很好的命名约定,数据分组和过滤功能,以及其他一些很酷的东西。

在这一点上,它取决于你想如何呈现它。您可以使用nbconvert将整个笔记本呈现为静态html或pdf。您可以将html表格复制粘贴到Excel或PowerPoint或电子邮件中。

相关问题