from fastai.vision import * from fastai.widgets import ClassConfusion path = untar_data(URLs.PETS) path_img = path/'images' fnames = get_image_files(path_img) pat = r'/([^/]+)_\d+.jpg$' data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=64).normalize(imagenet_stats) learn = cnn_learner(data, models.resnet34, metrics=error_rate) learn.fit_one_cycle(2) interp = ClassificationInterpretation.from_learner(learn) classlist = ['Ragdoll', 'Birman', 'Maine_Coon'] ClassConfusion(interp, classlist, is_ordered=False, figsize=(8,8)) classlist = [('Ragdoll', 'Birman'), ('British_Shorthair', 'Russian_Blue')] ClassConfusion(interp, classlist, is_ordered=True) from fastai.tabular import * path = untar_data(URLs.ADULT_SAMPLE) df = pd.read_csv(path/'adult.csv') dep_var = 'salary' cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race'] cont_names = ['age', 'fnlwgt', 'education-num'] procs = [FillMissing, Categorify, Normalize] test = TabularList.from_df(df.iloc[800:1000].copy(), path=path, cat_names=cat_names, cont_names=cont_names) data = (TabularList.from_df(df, path=path, cat_names=cat_names, cont_names=cont_names, procs=procs) .split_by_idx(list(range(800,1000))) .label_from_df(cols=dep_var) .add_test(test) .databunch()) learn = tabular_learner(data, layers=[200,100], metrics=accuracy) learn.fit(1, 1e-2) interp = ClassificationInterpretation.from_learner(learn) interp.plot_confusion_matrix() ClassConfusion(interp, ['>=50k', '<50k'], figsize=(12,12)) ClassConfusion(interp, ['>=50k', '<50k'], figsize=(12,12)) ClassConfusion(interp, ['>=50k', '<50k'], varlist=['age', 'education', 'relationship'], figsize=(12,12)) ClassConfusion(interp, [['>=50k', '>=50k'], ['>=50k', '<50k']], varlist=['age', 'education', 'relationship'], is_ordered=True, figsize=(12,12))