감동, 마음이 움직이는 것

[python] binning numpy (histogram) 본문

Tips (Utility, Computer Language, and etc.)

[python] binning numpy (histogram)

Struggler J. 2017. 12. 13. 07:21

import numpy as np

x = np.array(X) #X is data list, we have to convert list to numpy array

y = np.array(Y) 

#2d heatmap

heatmap, xedges, yedges = np.histogram2d(x, y, bins=100)

#1d histogram

hist, xedges = cp.histogram(x, bins=100)


[Script for read and write]

import numpy as np

import re

#read data

fp = open("data/scatter_a%g_Nen660000.d" % a,'r')

lines = fp.readlines()

Nlines = len(lines)

fp.close()


X = []

Y = []

B = []

C = []

D = []

for l in range(Nlines):

elem = re.split(" |\t|\n", lines[l]);

X.append(float(elem[0]))

Y.append(float(elem[1]))

B.append(float(elem[2]))

C.append(float(elem[3]))

D.append(float(elem[4]))


x = np.array(X)

y = np.array(Y)

b = np.array(B)

c = np.array(C)

d = np.array(D)


#2d heatmap

heatmap, xedges, yedges = np.histogram2d(x, y, bins=100)

#write

ofp = open("data/density_a%g_Nen%d.d" % (a, Nen),'w')

for idx in range(len(xedges)-1):

for jdx in range(len(yedges)-1):

ofp.write("%g %g %g %g\n" %( xedges[idx], yedges[jdx], heatmap[idx][jdx], heatmap[idx][jdx]/Nen) )



#1d projection heatmap

histx, xedges = np.histogram(x, bins=100)

histy, yedges = np.histogram(y, bins=100)

histb, bedges = np.histogram(b, bins=100)

histc, cedges = np.histogram(c, bins=100)

histd, dedges = np.histogram(d, bins=100)

#write

write("x", histx, xedges)

write("y", histy, yedges)

write("b", histb, bedges)

write("c", histc, cedges)

write("d", histd, dedges)