HistHist & Bins
Anonymous in /c/coding_help
170
report
I have the following code:<br><br>```python<br>import numpy as np<br><br>def histHist(data, nbins):<br> min_val = np.min(data)<br> max_val = np.max(data)<br> bins = np.linspace(min_val, max_val, nbins + 1)<br> f = np.zeros(nbins)<br> for i in range(0, nbins - 1):<br> if i == nbins - 1:<br> m = np.size(np.extract((data >= bins[i]) & (data <= bins[i + 1]), data))<br> f[i] = m / (bins[i + 1] - bins[i])<br> else:<br> m = np.size(np.extract((data >= bins[i]) & (data < bins[i + 1]), data))<br> f[i] = m / (bins[i + 1] - bins[i])<br><br> return f, bins<br>```<br><br>And am trying to run the following code:<br><br>```python<br>from scipy.stats import norm<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import seaborn as sns<br>from histHist import histHist<br><br>numoons = 500<br>seednum = 1928348123<br>lowersamps = 100<br>numpoints = 10000<br>np.random.seed(seednum + 0)<br>print("Generating moons")<br>data = norm.rvs(size=(2, numpoints), scale=4, random_state=seednum)<br><br>sns.set()<br>sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5, 'figure.figsize': (10, 9.5)})<br>sns.set_style("ticks")<br><br>fig = plt.figure(1)<br>plt.figure(figsize=(8, 6))<br><br>sns.kdeplot(data[0], data[1], gridsize=50, shade=True, shade_lowest=True, cmap='viridis', bw=.2)<br>plt.show()<br><br>fig = plt.figure(2)<br>sns.kdeplot(data[0], data[1], gridsize=50, shade=True, shade_lowest=True, cmap='viridis', bw=.2)<br>plt.show()<br><br>fig = plt.figure(3)<br>sns.kdeplot(data[0], data[1], gridsize=50, shade=True, shade_lowest=True, cmap='viridis', bw=.2)<br>plt.show()<br><br>for i in range(lowersamps):<br> print("Setup example %u" % (i + 1))<br> oonsamp = np.random.permutation(numpoints)[:numoons].squeeze()<br> moons2 = np.random.permutation(numpoints)[:numoons].squeeze()<br><br> np.random.seed(seednum + i + 1)<br> print("Generating background")<br> background = norm.rvs(size=(2, numoons), loc=[5, 0], scale=[2, 5], random_state=seednum + i + 1)<br><br> # Pick which points we're going to use<br> data[1, oonsamp] -= 4<br><br> # And create our samples<br> data_sample = data[:, oonsamp]<br> moons = data[:, moons2]<br><br> # And manually move the points<br> background[1, :] -= 4<br><br> # Append background onto our sample<br> data_sample = np.append(data_sample, background, 1)<br><br> # And bins<br> y_min = np.amin(data[1, :])<br> y_max = np.amax(data[1, :])<br><br> dy = (y_max - y_min) / 50<br><br> newymin = y_min + 4<br> newymax = y_max - 4<br><br> print("Computing bins")<br> bins = np.linspace(newymin, newymax, 50)<br><br> print("Computing Hist")<br> p_y, bin = histHist(data[1, :], 100)<br> p_moons, mbin = histHist(data[1, moons2], 100)<br> p_bkg, bbin = histHist(background[1, :], 100)<br><br> print("Computing Guess")<br> p_g = np.zeros(len(bins) - 1)<br> for j in range(2, len(bins) - 3):<br> p_g[j] = np.sum(p_y[(mbin[1:-1] >= bins[j]) & (mbin[1:-1] <= bins[j + 1])]) - \<br> np.sum(p_bkg[(bbin[1:-1] >= bins[j]) & (mbin[1:-1] <= bins[j + 1])])<br><br> # Compute the Poisson error in our histogram<br> print("Computing Errors")<br> err = np.zeros(len(bins) - 1)<br> for k in range(2, len(bins) - 3):<br> before = np.extract((data[1, :] < bins[k - 2]) & (data[1, :] >= bins[k - 3]), data[1, :])<br> after = np.extract((data[1, :] < bins[k + 1]) & (data[1, :] >= bins[k]), data[1, :])<br> upper = np.extract((data[1, :] < bins[k + 3]) & (data[1, :] >= bins[k + 2]), data[1, :])<br> bot = np.extract((data[1, :] < bins[k - 1]) & (data[1, :] >= bins[k]), data[1, :])<br> err[k] = np.std([<br> np.size(before),<br> np.size(after),<br> np.size(upper),<br> np.size(bot),<br> ])<br><br> print("Plotting")<br> fig = plt.figure(i + 1)<br> plt.figure(figsize=(8, 6))<br> for j in range(1, len(bins) - 2):<br> cut = np.extract((data[1, :] < bins[j + 1]) & (data[1, :] >= bins[j]), data[1, :])<br> plt.fill_between([bins[j], bins[j + 1]], np.size(cut) / (bins[j + 1] - bins[j]), color='red', edgecolor='black', label='Data', alpha=0.5)<br> plt.fill_between([bins[j], bins[j + 1]], p_moons[j], color='blue', edgecolor='black', label='Star Light', alpha=0.5)<br> plt.fill_between([bins[j], bins[j + 1]], p_bkg[j], color='green', edgecolor='black', label='Background', alpha=0.5)<br> plt.fill_between([bins[j], bins[j + 1]], p_g[j], color='black', edgecolor='black', label='Guess', alpha=0.5)<br><br> handles, labels = plt.gca().get_legend_handles_labels()<br> plt.legend(handles=handles[1:], labels=labels[1:], loc='lower right')<br> plt.xlabel('y (kpc)')<br> plt.ylabel('Counts/kpc')<br> plt.xlim(-40, 40)<br> plt.title('Counts/ kpc vs. Height')<br> plt.show()<br><br> fig = plt.figure(i + 4)<br> plt.figure(figsize=(8, 6))<br> for j in range(1, len(bins) - 2):<br> cut = np.extract((data[1, :] < bins[j + 1]) & (data[1, :] >= bins[j]), data[1, :])<br> plt.fill_between([bins[j], bins[j + 1]], [np.size(cut) / (bins[j + 1] - bins[j]), np.size(cut) / (bins[j + 1] - bins[j])], color='red', edgecolor='black', label='Data', alpha=0.5)<br> plt.fill_between([bins[j], bins[j + 1]], [p_moons[j] - err[j], p_moons[j] + err[j]], color='blue', edgecolor='black', label='Star Light', alpha=0.5)<br> plt.fill_between([bins[j], bins[j + 1]], [p_g[j] - err[j], p_g[j] + err[j]], color='black', edgecolor='black', label='Guess', alpha=0.5)<br><br> handles, labels = plt.gca().get_legend_handles_labels()<br> plt.legend(handles=handles[1:], labels=labels[1:], loc='lower right')<br> plt.xlabel('y (kpc)')<br> plt.ylabel('Counts/kpc')<br> plt.xlim(-40, 40)<br> plt.title('Counts/ kpc vs. Height')<br> plt.show()<br>```<br><br>I get the following error:<br><br>`line 16, in histHist<br> m = np.size(np.extract((data >= bins[i]) & (data < bins[i + 1]), data))`<br><br>`TypeError: '<' not supported between instances of 'float' and 'str'`<br><br>I believe the error is coming because `np.extract()` is returning strings, but I have no idea why it would return strings and how to get it to return the numbers as usual. I also tried replacing the `&` with `np.logical_and()` but it still doesn't work. What do I do?<br><br>What do I do?<br><br>​
Comments (4) 5928 👁️