def predictions_vs_actual_classification(model_results, model_name, n_bins, figsize=(7, 3)):
holdout = model_results.holdout_data
target = model_results.target
bins = np.arange(0, 1.001, 1 / n_bins)
bin_mids = (bins[:-1] + bins[1:]) / 2
binned = pd.cut(holdout['prediction'], bins=bins)
bin_counts = holdout.groupby(binned)[target].count()
bin_means = holdout.groupby(binned)[target].mean()
fig = plt.figure(figsize=figsize)
plt.suptitle('{0}: Predictions vs Actual'.format(model_name), fontsize=14)
ax1 = plt.gca()
ax1.grid(False)
ax1.bar(bin_mids, bin_counts, width=1/n_bins, color=sns.light_palette('green')[1],
label='row count', edgecolor='black')
ax1.set_xlabel('predicted probability')
ax1.set_ylabel('row count')
ax2 = ax1.twinx()
ax2.plot(bin_mids, bin_means, linewidth=3,
marker='.', markersize=16, label='actual rate')
ax2.plot(bins, bins, color=sns.color_palette()[2], label='main diagonal')
ax2.set_ylabel('actual rate')
handles, labels = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
legend = plt.legend(handles + handles2, labels + labels2,
loc='best',
frameon=True,
framealpha=0.7)
frame = legend.get_frame()
frame.set_facecolor('white')
return fig
python类light_palette()的实例源码
def plot_experiment(self, path=""):
color = self.color
data = self.experiment_data
cmap = sns.light_palette(color, as_cmap=True)
fig, ax = plt.subplots()
occupants, readings = (np.array(array) for array in zip(*data))
# ax_left, im_left = plot_linear_fit(
# ax_left, occupants, readings, self.model, self.model_sigma, color,
# cmap)
ax, im = plot_linear_fit(
ax, readings, occupants,
self.predictor, self.predictor_sigma,
color, cmap
)
ax.set_xlabel("{} sensor readout ({})".format(self.name, self.units))
ax.set_ylabel("Number of train car occupants")
# cax, kw = mpl.colorbar.make_axes(
# [ax_left, ax_right], location="bottom"
# )
# norm = mpl.colors.Normalize(vmin=0, vmax=1)
# cbar = mpl.colorbar.ColorbarBase(
# ax, cmap=cmap, norm=norm, alpha=0.5)
cbar = plt.colorbar(im, alpha=0.5, extend='neither', ticks=[
gaussian(3 * self.predictor_sigma, 0, self.predictor_sigma),
gaussian(2 * self.predictor_sigma, 0, self.predictor_sigma),
gaussian(self.predictor_sigma, 0, self.predictor_sigma),
gaussian(0, 0, self.predictor_sigma),
])
# cbar.solids.set_edgecolor("face")
cbar.set_ticklabels(
['$3 \sigma$', '$2 \sigma$', '$\sigma$', '{:.2%}'.format(
gaussian(0, 0, self.predictor_sigma))],
update_ticks=True
)
fig.savefig(os.path.join(path, self.name+".svg"))