Created
May 17, 2019 14:16
-
-
Save admond1994/4431df5afc037bf97eeb1955406f1bf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def plot_prob_density(df_lunch, df_dinner, field, x_start, x_end): | |
| plt.figure(figsize = (10, 7)) | |
| unit = 1.5 | |
| x = np.linspace(df_lunch.min() - unit, df_lunch.max() + unit, 1000)[:, np.newaxis] | |
| # Plot the data using a normalized histogram | |
| plt.hist(df_lunch, bins=10, density=True, label='Lunch Time', color='orange', alpha=0.2) | |
| plt.hist(df_dinner, bins=10, density=True, label='Dinner Time', color='navy', alpha=0.2) | |
| # Do kernel density estimation | |
| kd_lunch = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(df_lunch) | |
| kd_dinner = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(df_dinner) | |
| # Plot the estimated densty | |
| kd_vals_lunch = np.exp(kd_lunch.score_samples(x)) | |
| kd_vals_dinner = np.exp(kd_dinner.score_samples(x)) | |
| plt.plot(x, kd_vals_lunch, color='orange') | |
| plt.plot(x, kd_vals_dinner, color='navy') | |
| plt.axvline(x=x_start,color='red',linestyle='dashed') | |
| plt.axvline(x=x_end,color='red',linestyle='dashed') | |
| # Show the plots | |
| plt.xlabel(field, fontsize=15) | |
| plt.ylabel('Probability Density', fontsize=15) | |
| plt.legend(fontsize=15) | |
| plt.show() | |
| gc.collect() | |
| return kd_lunch, kd_dinner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment