def plot_density_map(x, y, xbins, ybins, Nlevels=4, cbar=True, weights=None):
Z = np.histogram2d(x, y, bins=(xbins, ybins), weights=weights)[0].astype(float).T
# central values
lt = get_centers_from_bins(xbins)
lm = get_centers_from_bins(ybins)
cX, cY = np.meshgrid(lt, lm)
X, Y = np.meshgrid(xbins, ybins)
im = plt.pcolor(X, Y, Z, cmap=plt.cm.Blues)
plt.contour(cX, cY, Z, levels=nice_levels(Z, Nlevels), cmap=plt.cm.Greys_r)
if cbar:
cb = plt.colorbar(im)
else:
cb = None
plt.xlim(xbins[0], xbins[-1])
plt.ylim(ybins[0], ybins[-1])
try:
plt.tight_layout()
except Exception as e:
print(e)
return plt.gca(), cb
python类histogram2d()的实例源码
def shot_heatmap(df,sigma = 1,log=False,player_pic=True,ax=None,cmap='jet'):
'''
This function plots a heatmap based on the shot chart.
input - dataframe with x and y coordinates.
optional - log (default false) plots heatmap in log scale.
player (default true) adds player's picture and name if true
sigma - the sigma of the Gaussian kernel. In feet (default=1)
'''
n,_,_ = np.histogram2d( 0.1*df['LOC_X'].values, 0.1*df['LOC_Y'].values,bins = [500, 500],range = [[-25,25],[-5.25,44.75]])
KDE = ndimage.filters.gaussian_filter(n,10.0*sigma)
N = 1.0*KDE/np.sum(KDE)
if ax is None:
ax = plt.gca(xlim = [30,-30],ylim = [-7,43],xticks=[],yticks=[],aspect=1.0)
court(ax,outer_lines=True,color='black',lw=2.0,direction='down')
ax.axis('off')
if log:
ax.imshow(np.rot90(np.log10(N+1)),cmap=cmap,extent=[25.0, -25.0, -5.25, 44.75])
else:
ax.imshow(np.rot90(N),cmap=cmap,extent=[25.0, -25.0, -5.25, 44.75])
if player_pic:
player_id = df.PLAYER_ID.values[0]
pic = players_picture(player_id)
ax.imshow(pic,extent=[15,25,30,37.8261])
ax.text(0,-7,'By: Doingthedishes',color='white',horizontalalignment='center',fontsize=20,fontweight='bold')
def photonsToFrame(photonposframe,imagesize,background):
pixels = imagesize
edges = range(0, pixels+1)
# HANDLE CASE FOR NO PHOTONS DETECTED AT ALL IN FRAME
if photonposframe.size == 0:
simframe = _np.zeros((pixels, pixels))
else:
xx = photonposframe[:, 0]
yy = photonposframe[:, 1]
simframe, xedges, yedges = _np.histogram2d(yy, xx, bins=(edges, edges))
simframe = _np.flipud(simframe) # to be consistent with render
#simframenoise = noisy(simframe,background,noise)
simframenoise = noisy_p(simframe, background)
simframenoise[simframenoise > 2**16-1] = 2**16-1
simframeout = _np.round(simframenoise).astype('<u2')
return simframeout
def mask_locs(self):
locs = self.locs[0]
steps_x = len(self.xedges)
steps_y = len(self.yedges)
x_ind = np.floor((locs['x']-self.x_min)/(self.x_max-self.x_min)*steps_x)-1
y_ind = np.floor((locs['y']-self.y_min)/(self.y_max-self.y_min)*steps_y)-1
x_ind = x_ind.astype(int)
y_ind = y_ind.astype(int)
index = self.mask[y_ind,x_ind].astype(bool)
self.index_locs = locs[index]
self.index_locs_out = locs[~index]
H_new, xedges, yedges = np.histogram2d(self.index_locs['x'], self.index_locs['y'], bins=(self.xedges, self.yedges))
self.H_new = H_new.T # Let each row list bins with common y range.
ax4 = self.figure.add_subplot(144, title='Masked image')
ax4.imshow(self.H_new, interpolation='nearest', origin='low',extent=[self.xedges[0], self.xedges[-1], self.yedges[0], self.yedges[-1]])
ax4.grid(False)
self.mask_exists = 1
self.saveButton.setEnabled(True)
self.canvas.draw()
def calc_mutual_information(x, y, bins):
try:
if bins == -1:
bins = doane_bin(x)
if bins == np.inf:
bins = sturges_bin(x)
except ValueError:
bins = 10.0
# print "bins", bins
try:
c_xy = np.histogram2d(x, y, bins)[0]
mi = metrics.mutual_info_score(None, None, contingency=c_xy)
# print "success"
except Exception,e:
print "error with mi calc", str(e)
mi = 0
return mi
def signal_gaussian(
signal_location=np.array([61, 21])*u.deg,
fov_center=np.array([60, 20])*u.deg,
width=0.05*u.deg,
signal_events=80,
bins=[80, 80],
fov=4*u.deg,
):
# reshape so if signal_events = 1 the array can be indexed in the same way.
signal = multivariate_normal.rvs(
mean=signal_location.value,
cov=width.value,
size=signal_events
).reshape(signal_events, 2)
r = np.array([fov_center - fov/2, fov_center + fov/2]).T
signal_hist, _, _ = np.histogram2d(signal[:, 0], signal[:, 1], bins=bins, range=r)
return signal_hist
def plot_heatmap(ax, xpoints, ypoints, nbins, title=None, maxcount=None):
''' Plot a heatmap of the given data on on the given axes. '''
# imshow expects y,x for the image, but x,y for the extents,
# so we have to manage that here...
bins = np.concatenate( (np.arange(0,1.0,1.0/nbins), [1.0]) )
heatmap, yedges, xedges = np.histogram2d(ypoints, xpoints, bins=bins)
extent = [xedges[0],xedges[-1], yedges[0], yedges[-1]]
# make sure we always show the full extent of the tank and the full extent of the data,
# whichever is wider.
ax.set_xlim(min(0, xedges[0]), max(1, xedges[-1]))
ax.set_ylim(min(0, yedges[0]), max(1, yedges[-1]))
if title:
ax.set_title(title)
if maxcount is not None:
norm = Normalize(0, maxcount)
else:
norm = None
return ax.imshow(heatmap, extent=extent, cmap=plt.get_cmap('hot'), origin='lower', interpolation='nearest', norm=norm)
def drawSmoothCatalog(self, catalog, label=None, **kwargs):
ax = plt.gca()
ra,dec = catalog.ra_dec
x, y = sphere2image(self.ra,self.dec,ra,dec)
delta_x = self.radius/100.
smoothing = 2*delta_x
bins = numpy.arange(-self.radius, self.radius + 1.e-10, delta_x)
h, xbins, ybins = numpy.histogram2d(x, y, bins=[bins, bins])
blur = nd.filters.gaussian_filter(h.T, smoothing / delta_x)
defaults = dict(cmap='gray_r',rasterized=True)
kwargs = dict(defaults.items()+kwargs.items())
xx,yy = np.meshgrid(xbins,ybins)
im = drawProjImage(xx,yy,blur,coord='C',**kwargs)
if label:
plt.text(0.05, 0.95, label, fontsize=10, ha='left', va='top',
color='k', transform=pylab.gca().transAxes,
bbox=dict(facecolor='white', alpha=1., edgecolor='none'))
def visualize2D(fig, ax, xs, ys, bins=200,
xlabel='x', ylabel='y',
xlim=None, ylim=None):
H, xedges, yedges = numpy.histogram2d(xs, ys, bins)
H = numpy.rot90(H)
H = numpy.flipud(H)
Hmasked = numpy.ma.masked_where(H == 0, H)
ax.pcolormesh(xedges, yedges, Hmasked)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
if xlim is None:
xlim = (min(xs), max(xs))
if ylim is None:
ylim = (min(ys), max(ys))
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
fig.colorbar(pyplot.contourf(Hmasked))
def test_zoom():
edges = pixel_edges(jet_size=1, pixel_size=(0.1, 0.1), border_size=0.25)
assert_equal(edges[0].shape, (26,))
assert_equal(edges[1].shape, (26,))
image, _, _ = np.histogram2d(
np.random.normal(0, 1, 1000), np.random.normal(0, 1, 1000),
bins=(edges[0], edges[1]))
assert_true(image.sum() > 0)
assert_equal(image.shape, (25, 25))
# zooming with factor 1 should not change anything
image_zoomed = zoom_image(image, 1, out_width=25)
assert_array_almost_equal(image, image_zoomed)
assert_raises(ValueError, zoom_image, image, 0.5)
# test out_width
assert_equal(zoom_image(image, 1, out_width=11).shape, (11, 11))
image_zoomed = zoom_image(image, 2, out_width=25)
assert_true(image.sum() < image_zoomed.sum())
def heatmap (d, bins=(100, 100), smoothing=1.3, cmap='jet'):
def getx (pt):
return pt.coords[0][0]
def gety (pt):
return pt.coords[0][1]
x = list(d.geometry.apply(getx))
y = list(d.geometry.apply(gety))
heatmap, xedges, yedges = np.histogram2d(y, x, bins=bins)
extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] # bin edges along the x and y dimensions, ordered
# why are we taking log?
logheatmap = np.log(heatmap)
logheatmap[np.isneginf(logheatmap)] = 0
logheatmap = ndimage.filters.gaussian_filter(logheatmap, smoothing, mode='nearest')
return (logheatmap, extent)
def DensityHistogram(xypoints, numbins):
xpoints = map(lambda (x, y): x, xypoints)
ypoints = map(lambda (x, y): y, xypoints)
minx, maxx, miny, maxy = min(xpoints), max(xpoints), \
min(ypoints), max(ypoints)
xedges = np.arange(minx, maxx, (maxx - minx) / float(numbins))
yedges = np.arange(miny, maxy, (maxy - miny) / float(numbins))
H, xedges, yedges = np.histogram2d(ypoints, xpoints, bins = (xedges, yedges))
fig = plt.figure(figsize=(7, 3))
ax = fig.add_subplot(132)
ax.set_title('pcolormesh: exact bin edges')
X, Y = np.meshgrid(xedges, yedges)
ax.pcolormesh(X, Y, H)
ax.set_aspect('equal')
#plt.savefig('./output/foo.png', bbox_inches='tight')
#plt.show()
g = histogram([xpoints, ypoints])
g.save('./output/foo2.png')
## def
def addDistributions(self,Tuple):
import numpy
selidxs=[]
ytuple=Tuple[self.nameY]
xtuple=Tuple[self.nameX]
useonlyoneclass=len(self.classes)==1 and len(self.classes[0])==0
if not useonlyoneclass:
labeltuple=Tuple[self.classes]
for c in self.classes:
selidxs.append(labeltuple[c]>0)
else:
selidxs=[numpy.zeros(len(xtuple),dtype='int')<1]
for i in range(len(self.classes)):
tmphist,xe,ye=numpy.histogram2d(xtuple[selidxs[i]],ytuple[selidxs[i]],[self.axisX,self.axisY],normed=True)
self.xedges=xe
self.yedges=ye
if len(self.distributions)==len(self.classes):
self.distributions[i]=self.distributions[i]+tmphist
else:
self.distributions.append(tmphist)
def heatmap(self, get_ball_var):
heat_values = get_ball_var('ball_position_history')
# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
# x = np.random.randn(100000)
y = np.random.randn(100000)
# print(y)
# plt.hist2d(HeatValues[0],HeatValues[1],bins=100);
# plt.clf()
# plt.imshow(heatmap, extent=extent)
# plt.show()
def _init_read_free(self):
# subverted... interpret self.filename as a numpy array
try:
FEC = self.filename # FreeEnergyContainer?
F = FEC.F
except AttributeError:
super(DerivedFreeEnergy,self)._init_read_free()
return
self._midpoints = (FEC.X, FEC.Y)
self._X = FEC.X
self._Y = FEC.Y
self._free_energy = numpy.ma.array(F, mask=numpy.logical_not(numpy.isfinite(F)),
fill_value=1000);
# reconstruct what input histogram2d would need
self._edges = (self._mid2edges(self._X), # NMP bin edges
self._mid2edges(self._Y)) # LID bin edges
def plotAgainstGFP_hist2d(self):
fig1 = pylab.figure(figsize = (20, 15))
print len(self.GFP)
for i in xrange(min(len(data.cat), 4)):
print len(self.GFP[self.categories == i])
vect = []
pylab.subplot(2,2,i+1)
pop = self.GFP[self.categories == i]
print "cat", i, "n pop", len(self.GFP[(self.categories == i) & (self.GFP > -np.log(12.5))])
H, xedges, yedges = np.histogram2d(self.angles[self.categories == i], self.GFP[self.categories == i], bins = 10)
hist = pylab.hist2d(self.GFP[self.categories == i], self.angles[self.categories == i], bins = 10, cmap = pylab.cm.Reds, normed = True)
pylab.clim(0.,0.035)
pylab.colorbar()
pylab.title(data.cat[i])
pylab.xlabel('GFP score')
pylab.ylabel('Angle (degree)')
pylab.xlim([-4.2, -1])
pylab.show()
def calculate_mutualinformation(x, y, bins):
pxy, _, _ = np.histogram2d(x, y, bins)
px, _, = np.histogram(x, bins)
py, _, = np.histogram(y, bins)
pxy = pxy/np.sum(pxy)
px = px/np.sum(px)
py = py/np.sum(py)
pxy = pxy[np.nonzero(pxy)]
px = px[np.nonzero(px)]
py = py[np.nonzero(py)]
hxy = -np.sum(pxy*np.log2(pxy))
hx = -np.sum(px*np.log2(px))
hy = -np.sum(py*np.log2(py))
MI = hx+hy-hxy
return MI
def test_f32_rounding(self):
# gh-4799, check that the rounding of the edges works with float32
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
assert_equal(counts_hist.sum(), 3.)
def edgeplot(self, TT, ps):
for ei,X in enumerate(self.edges):
(i, j) = X[:2]
Ta = TT[i]
Tb = TT[j]
plt.clf()
if len(Ta) > 1000:
nbins = 101
ra = np.hstack((Ta.ra, Tb.ra))
dec = np.hstack((Ta.dec, Tb.dec))
H,xe,ye = np.histogram2d(ra, dec, bins=nbins)
(matchRA, matchDec, dr,dd) = self.edge_matches(ei, goodonly=True)
G,xe,ye = np.histogram2d(matchRA, matchDec, bins=(xe,ye))
assert(G.shape == H.shape)
img = antigray(H / H.max())
img[G>0,:] = matplotlib.cm.hot(G[G>0] / H[G>0])
ax = setRadecAxes(xe[0], xe[-1], ye[0], ye[-1])
plt.imshow(img, extent=(min(xe), max(xe), min(ye), max(ye)),
aspect='auto', origin='lower', interpolation='nearest')
plt.axis(ax)
else:
self.plotallstars([Ta,Tb])
self.plotmatchedstars(ei)
plt.xlabel('RA (deg)')
plt.ylabel('Dec (deg)')
ps.savefig()
# one plot per edge
def gen_hist(self, event=None):
try:
xnum = int(self.x_axis_num.text())
ynum = int(self.y_axis_num.text())
except ValueError:
sys.stderr.write('Need axes numbers to be integers\n')
return
self.hist2d, self.binx, self.biny = np.histogram2d(self.embed[:,xnum], self.embed[:,ynum], bins=100)
delx = self.binx[1] - self.binx[0]
dely = self.biny[1] - self.biny[0]
self.binx = np.insert(self.binx, 0, [self.binx[0]-6*delx, self.binx[0]-delx])
self.binx = np.insert(self.binx, len(self.binx), [self.binx[-1]+delx, self.binx[-1]+6*delx])
self.biny = np.insert(self.biny, 0, [self.biny[0]-6*dely, self.biny[0]-dely])
self.biny = np.insert(self.biny, len(self.biny), [self.biny[-1]+dely, self.biny[-1]+6*dely])
def _compute_occupancy(self):
x, y = self.trans_func(self._extern, at=self._bst.bin_centers)
xmin = self.xbins[0]
xmax = self.xbins[-1]
ymin = self.ybins[0]
ymax = self.ybins[-1]
occupancy, _, _ = np.histogram2d(x, y, bins=[self.xbins, self.ybins], range=([[xmin, xmax], [ymin, ymax]]))
return occupancy
def test_f32_rounding(self):
# gh-4799, check that the rounding of the edges works with float32
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
assert_equal(counts_hist.sum(), 3.)
def from_values(cls, x, y, weights=None, nbins=200, x_name=None, y_name=None):
"""
This function ...
:param x:
:param y:
:param weights:
:param nbins:
:param x_name:
:param y_name:
:return:
"""
#rBins_F, FBins_r = getRadBins(x, y, 1, weights)
#rBins_F[rBins_F > 25] = np.nan
rBins_F = None
FBins_r = None
#print("rBins_F", rBins_F)
#print("FBins_r", FBins_r)
# Estimate the 2D histogram
H, xedges, yedges = np.histogram2d(x, y, bins=nbins, normed=True, weights=weights)
# H needs to be rotated and flipped
H = np.rot90(H)
H = np.flipud(H)
# Mask zeros
Hmasked = np.ma.masked_where(H == 0, H) # Mask pixels with a value of zero
return cls(Hmasked, xedges, yedges, rBins_F, FBins_r, x_name, y_name)
# -----------------------------------------------------------------
def from_values(cls, x, y, weights=None, nbins=200, x_name=None, y_name=None):
"""
This function ...
:param x:
:param y:
:param weights:
:param nbins:
:param x_name:
:param y_name:
:return:
"""
#rBins_F, FBins_r = getRadBins(x, y, 1, weights)
#rBins_F[rBins_F > 25] = np.nan
rBins_F = None
FBins_r = None
#print("rBins_F", rBins_F)
#print("FBins_r", FBins_r)
# Estimate the 2D histogram
H, xedges, yedges = np.histogram2d(x, y, bins=nbins, normed=True, weights=weights)
# H needs to be rotated and flipped
H = np.rot90(H)
H = np.flipud(H)
# Mask zeros
Hmasked = np.ma.masked_where(H == 0, H) # Mask pixels with a value of zero
return cls(Hmasked, xedges, yedges, rBins_F, FBins_r, x_name, y_name)
# -----------------------------------------------------------------
def get_hist_node2vec(emb,d,my_min,my_max,definition):
# d should be an even integer
img_dim = int(np.arange(my_min, my_max+0.05,(my_max+0.05-my_min)/float(definition*(my_max+0.05-my_min))).shape[0]-1)
my_bins = np.linspace(my_min,my_max,img_dim) # to have middle bin centered on zero
Hs = []
for i in range(0,d,2):
H, xedges, yedges = np.histogram2d(x=emb[:,i],y=emb[:,i+1],bins=my_bins, normed=False)
Hs.append(H)
Hs = np.array(Hs)
return Hs
def get_hist_node2vec(emb,d,my_min,my_max,definition):
# d should be an even integer
img_dim = int(np.arange(my_min, my_max+0.05,(my_max+0.05-my_min)/float(definition*(my_max+0.05-my_min))).shape[0]-1)
my_bins = np.linspace(my_min,my_max,img_dim) # to have middle bin centered on zero
Hs = []
for i in range(0,d,2):
H, xedges, yedges = np.histogram2d(x=emb[:,i],y=emb[:,i+1],bins=my_bins, normed=False)
Hs.append(H)
Hs = np.array(Hs)
return Hs
def histogram2dstd(data, std = 6, bins = 50):
"""Create histogram resolving distribution according to std"""
## calculate standard deviations in each direction
stds = np.std(data[:,0:-2], axis = 0);
means = np.mean(data[:,0:-2], axis = 0);
rngs = [[m- std * s, m + std * s] for m,s in itertools.izip(means,stds)];
hist = np.histogram2d(data[:,0], data[:,1], bins = bins, range = rngs);
return hist;
demo_mi.py 文件源码
项目:Building-Machine-Learning-Systems-With-Python-Second-Edition
作者: PacktPublishing
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def mutual_info(x, y, bins=10):
counts_xy, bins_x, bins_y = np.histogram2d(x, y, bins=(bins, bins))
counts_x, bins = np.histogram(x, bins=bins)
counts_y, bins = np.histogram(y, bins=bins)
counts_xy += 1
counts_x += 1
counts_y += 1
P_xy = counts_xy / np.sum(counts_xy, dtype=float)
P_x = counts_x / np.sum(counts_x, dtype=float)
P_y = counts_y / np.sum(counts_y, dtype=float)
I_xy = np.sum(P_xy * np.log2(P_xy / (P_x.reshape(-1, 1) * P_y)))
return I_xy / (entropy(counts_x) + entropy(counts_y))
def generate_image(self):
locs = self.locs[0]
self.stepsize = 1/self.oversampling
self.xedges = np.arange(self.x_min,self.x_max,self.stepsize)
self.yedges = np.arange(self.y_min,self.y_max,self.stepsize)
H, xedges, yedges = np.histogram2d(locs['x'], locs['y'], bins=(self.xedges, self.yedges))
H = H.T # Let each row list bins with common y range.
self.H = H
def getHSHistograms_2D(HSVimage):
(Width, Height) = HSVimage.shape[1], HSVimage.shape[0]
H, xedges, yedges = numpy.histogram2d(numpy.reshape(HSVimage[:,:,0], Width*Height), numpy.reshape(HSVimage[:,:,1], Width*Height), bins=(range(-1,180, 30), range(-1, 256, 64)))
H = H / numpy.sum(H);
return (H, xedges, yedges)