1

I have written function, which brings me tables with some statistics. here it is:

def demonstrate_best_results(x, features, clusters, indices,
                             cluster_means, differences, rel_differences):
    
    for cluster, result in clusters.items():
        print( )
        print("cluster number  : " + str(cluster+1),
              "Number of el.", len(indices[cluster] ))
        print("grand mean      : ", 
              np.mean(x, axis=0))
        print("cluster mean    : ", cluster_means[cluster])
        print("differences     : ", differences[cluster])
        print("rel. differences: ", rel_differences[cluster])

When I run it:

demonstrate_best_results(x=df, features=df.columns,
                         clusters=best_clusters_1,
                         indices=best_indices_1, 
                         cluster_means=best_cluster_means_1, 
                         differences=best_differences_1, 
                         rel_differences=best_rel_differences_1)

I get this:

cluster number  : 1 Number of el. 553
grand mean      :  [1.14817115e+08 3.85944444e+00 3.68888889e+01 6.59444444e+00]
cluster mean    :  [9.45822772e+07 4.16365280e+00 3.84918626e+01 3.67450271e+00]
differences     :  [-2.02348373e+07  3.04208358e-01  1.60297368e+00 -2.91994173e+00]
rel. differences:  [-17.62353755   7.88218001   4.34541058 -44.27881312]

cluster number  : 2 Number of el. 115
grand mean      :  [1.14817115e+08 3.85944444e+00 3.68888889e+01 6.59444444e+00]
cluster mean    :  [1.09844053e+08 4.03913043e+00 3.72000000e+01 2.56608696e+01]
differences     :  [-4.97306137e+06  1.79685990e-01  3.11111111e-01  1.90664251e+01]
rel. differences:  [ -4.33128928   4.65574755   0.84337349 289.12860335]

cluster number  : 3 Number of el. 83
grand mean      :  [1.14817115e+08 3.85944444e+00 3.68888889e+01 6.59444444e+00]
cluster mean    :  [9.18498693e+07 3.95783133e+00 2.42409639e+01 5.71084337e+00]
differences     :  [-2.29672452e+07  9.83868809e-02 -1.26479250e+01 -8.83601071e-01]
rel. differences:  [-20.00332901   2.54924983 -34.28654377 -13.39917378]

cluster number  : 4 Number of el. 127
grand mean      :  [1.14817115e+08 3.85944444e+00 3.68888889e+01 6.59444444e+00]
cluster mean    :  [7.23130098e+07 2.30708661e+00 3.79685039e+01 2.72440945e+00]
differences     :  [-4.25041047e+07 -1.55235783e+00  1.07961505e+00 -3.87003500e+00]
rel. differences:  [-37.01896261 -40.22231315   2.9266673  -58.68629311]

cluster number  : 5 Number of el. 22
grand mean      :  [1.14817115e+08 3.85944444e+00 3.68888889e+01 6.59444444e+00]
cluster mean    :  [9.81456648e+08 3.86363636e+00 3.64545455e+01 6.00000000e+00]
differences     :  [ 8.66639533e+08  4.19191919e-03 -4.34343434e-01 -5.94444444e-01]
rel. differences:  [ 7.54799959e+02  1.08614575e-01 -1.17743702e+00 -9.01432182e+00]

However I need those tables to be converted into latex. So how to rewrote my function to get these 5 tables in form of pandas dataframe, so I can use to_latex() after?

1

1 Answer 1

0

Do you mean:

def demonstrate_best_results(x, features, clusters, indices,
                             cluster_means, differences, rel_differences):
    dataframes = []
    for cluster, result in clusters.items():
        print( )
        print("cluster number  : " + str(cluster+1),
              "Number of el.", len(indices[cluster] ))
        df = pd.DataFrame({"grand mean      : ": mean(x, axis=0),
                           "cluster mean    : ": cluster_means[cluster],
                           "differences     : ": differences[cluster],
                           "rel. differences: ", rel_differences[cluster]})
        dataframes.append(dataframes)

    return dataframes
Sign up to request clarification or add additional context in comments.

3 Comments

and how to get those dataframes? result looks like this: cluster number : 1 Number of el. 553 cluster number : 2 Number of el. 115 cluster number : 3 Number of el. 83 cluster number : 4 Number of el. 127 cluster number : 5 Number of el. 22 Out[191]: [[...], [...], [...], [...], [...]]
and need to get them to apply to_latex() function after and get a latex code, how to extract them now?
dataframes = demonstrate(...); dataframes[0].to_latex()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.