示例#1
0
    def test_count_plots(self):
        df = self.iris

        ax = df.sns.countplot()
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), '.target')
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        return      # ToDo: only fails on Travis

        ax = df.sns.countplot(df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), df.columns[1])
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        ax = df.sns.countplot(x=df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), df.columns[1])
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        ax = df.sns.countplot(y=df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), 'count')
        self.assertEqual(ax.get_ylabel(), df.columns[1])
        tm.close()

        with tm.assertRaises(TypeError):
            # can't pass both x and y
            df.sns.countplot(x=df.columns[1], y=df.columns[2])
示例#2
0
    def test_frame_data_proparty_series(self):
        df = pdml.ModelFrame({
            'A': [1, 2, 3],
            'B': [4, 5, 6]
        },
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        df.data = df['A']
        exp = pdml.ModelFrame({'A': [1, 2, 3]},
                              target=[7, 8, 9],
                              index=['a', 'b', 'c'])
        self.assert_frame_equal(df, exp)

        df = pdml.ModelFrame({
            'A': [1, 2, 3],
            'B': [4, 5, 6]
        },
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        df.data = pd.Series([1, 2, 3], name='x', index=['a', 'b', 'c'])
        exp = pdml.ModelFrame({'x': [1, 2, 3]},
                              target=[7, 8, 9],
                              index=['a', 'b', 'c'])
        self.assert_frame_equal(df, exp)

        df = pdml.ModelFrame({
            'A': [1, 2, 3],
            'B': [4, 5, 6]
        },
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        with tm.assertRaises(TypeError):
            df.data = [1, 2, 3]
def test_plot():

    try:
        import matplotlib.pyplot  # noqa
    except ImportError:
        import nose
        raise nose.SkipTest()

    y_true = [
        'rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit',
        'rabbit', 'cat', 'dog', 'rabbit'
    ]
    y_pred = [
        'cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit',
        'cat', 'rabbit', 'rabbit'
    ]

    cm = ConfusionMatrix(y_true, y_pred)

    # check plot works
    cm.plot()
    cm.plot(backend='seaborn')

    with tm.assertRaises(ValueError):
        cm.plot(backend='xxx')
示例#4
0
    def test_count_plots(self):
        df = self.iris

        ax = df.sns.countplot()
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), '.target')
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        return      # ToDo: only fails on Travis

        ax = df.sns.countplot(df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), df.columns[1])
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        ax = df.sns.countplot(x=df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), df.columns[1])
        self.assertEqual(ax.get_ylabel(), 'count')
        tm.close()

        ax = df.sns.countplot(y=df.columns[1])
        self.assertIsInstance(ax, matplotlib.axes.Axes)
        self.assertEqual(ax.get_xlabel(), 'count')
        self.assertEqual(ax.get_ylabel(), df.columns[1])
        tm.close()

        with tm.assertRaises(TypeError):
            # can't pass both x and y
            df.sns.countplot(x=df.columns[1], y=df.columns[2])
示例#5
0
    def test_frame_data_proparty_series(self):
        df = pdml.ModelFrame({'A': [1, 2, 3],
                              'B': [4, 5, 6]},
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        df.data = df['A']
        exp = pdml.ModelFrame({'A': [1, 2, 3]},
                              target=[7, 8, 9],
                              index=['a', 'b', 'c'])
        self.assert_frame_equal(df, exp)

        df = pdml.ModelFrame({'A': [1, 2, 3],
                              'B': [4, 5, 6]},
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        df.data = pd.Series([1, 2, 3], name='x', index=['a', 'b', 'c'])
        exp = pdml.ModelFrame({'x': [1, 2, 3]},
                              target=[7, 8, 9],
                              index=['a', 'b', 'c'])
        self.assert_frame_equal(df, exp)

        df = pdml.ModelFrame({'A': [1, 2, 3],
                              'B': [4, 5, 6]},
                             target=[7, 8, 9],
                             index=['a', 'b', 'c'])
        with tm.assertRaises(TypeError):
            df.data = [1, 2, 3]
示例#6
0
    def test_plotting(self):

        iris = datasets.load_iris()
        df = pdml.ModelFrame(iris)

        df.fit(df.svm.SVC())

        # raises if df.estimator is not XGBModel
        with tm.assertRaises(ValueError):
            df.xgb.plot_importance()

        with tm.assertRaises(ValueError):
            df.xgb.to_graphviz()

        with tm.assertRaises(ValueError):
            df.xgb.plot_tree()

        df.fit(df.xgb.XGBClassifier())

        from matplotlib.axes import Axes
        from graphviz import Digraph

        try:
            ax = df.xgb.plot_importance()
        except ImportError:
            import nose
            # matplotlib.use doesn't work on Travis
            # PYTHON=3.4 PANDAS=0.17.1 SKLEARN=0.16.1
            raise nose.SkipTest()

        self.assertIsInstance(ax, Axes)
        assert ax.get_title() == 'Feature importance'
        assert ax.get_xlabel() == 'F score'
        assert ax.get_ylabel() == 'Features'
        assert len(ax.patches) == 4

        g = df.xgb.to_graphviz(num_trees=0)
        self.assertIsInstance(g, Digraph)

        ax = df.xgb.plot_tree(num_trees=0)
        self.assertIsInstance(ax, Axes)
示例#7
0
    def test_plotting(self):

        import matplotlib
        matplotlib.use('Agg')

        iris = datasets.load_iris()
        df = pdml.ModelFrame(iris)

        df.fit(df.svm.SVC())

        # raises if df.estimator is not XGBModel
        with tm.assertRaises(ValueError):
            df.xgb.plot_importance()

        with tm.assertRaises(ValueError):
            df.xgb.to_graphviz()

        with tm.assertRaises(ValueError):
            df.xgb.plot_tree()

        df.fit(df.xgb.XGBClassifier())

        from matplotlib.axes import Axes
        from graphviz import Digraph

        ax = df.xgb.plot_importance()
        assert isinstance(ax, Axes)
        assert ax.get_title() == 'Feature importance'
        assert ax.get_xlabel() == 'F score'
        assert ax.get_ylabel() == 'Features'
        assert len(ax.patches) == 4

        g = df.xgb.to_graphviz(num_trees=0)
        assert isinstance(g, Digraph)

        ax = df.xgb.plot_tree(num_trees=0)
        assert isinstance(ax, Axes)
示例#8
0
    def test_plotting(self):

        import matplotlib
        matplotlib.use('Agg')

        iris = datasets.load_iris()
        df = pdml.ModelFrame(iris)

        df.fit(df.svm.SVC())

        # raises if df.estimator is not XGBModel
        with tm.assertRaises(ValueError):
            df.xgb.plot_importance()

        with tm.assertRaises(ValueError):
            df.xgb.to_graphviz()

        with tm.assertRaises(ValueError):
            df.xgb.plot_tree()

        df.fit(df.xgb.XGBClassifier())

        from matplotlib.axes import Axes
        from graphviz import Digraph

        ax = df.xgb.plot_importance()
        assert isinstance(ax, Axes)
        assert ax.get_title() == 'Feature importance'
        assert ax.get_xlabel() == 'F score'
        assert ax.get_ylabel() == 'Features'
        assert len(ax.patches) == 4

        g = df.xgb.to_graphviz(num_trees=0)
        assert isinstance(g, Digraph)

        ax = df.xgb.plot_tree(num_trees=0)
        assert isinstance(ax, Axes)
示例#9
0
    def test_not_supported_estimator(self):
        df = pdml.ModelFrame(datasets.load_iris())
        df.fit(df.cluster.KMeans(n_clusters=3))

        with tm.assertRaises(NotImplementedError):
            df.plot_estimator()
示例#10
0
 def test_no_estimator(self):
     df = pdml.ModelFrame(datasets.load_iris())
     with tm.assertRaises(ValueError):
         df.plot_estimator()
			wpercent = (basewidth/float(im_pred.size[0]))
			hsize = int((float(im_pred.size[1])*float(wpercent)))
			im_pred = im_pred.resize((basewidth,hsize), Image.NEAREST)
			im_pred = im_pred
			wpercent = (basewidth/float(im_true.size[0]))
			hsize = int((float(im_true.size[1])*float(wpercent)))
			im_true = im_true.resize((basewidth,hsize), Image.NEAREST)
			im_true = im_true
			'''
            pixels_pred_all += list(im_pred.getdata())
            # pix_val_flat_pred = [x for sets in pix_val_pred for x in sets]

            pixels_true_all += list(im_true.getdata())
            # pix_val_flat_true = [x for sets in pix_val_true for x in sets]

# Print number of pixel count vs the colors
# print im_pred.getcolors()

# pixels_true_all = ['rabbit', 'cat', 'rabbit', 'rabbit', 'cat', 'dog', 'dog', 'rabbit', 'mouse', 'cat', 'dog', 'rabbit']
# pixels_pred_all = ['cat', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'dog', 'cat', 'rabbit', 'cat', 'rabbit', 'rabbit']

# Print the confusion matrix

confusion_matrix = ConfusionMatrix(pixels_true_all, pixels_pred_all)
print("\nConfusion matrix:\n%s" % confusion_matrix)
confusion_matrix.plot()
confusion_matrix.plot(backend='seaborn')

with tm.assertRaises(ValueError):
    confusion_matrix.plot(backend='xxx')
示例#12
0
    def test_not_supported_estimator(self):
        df = pdml.ModelFrame(datasets.load_iris())
        df.fit(df.cluster.KMeans(n_clusters=3))

        with tm.assertRaises(NotImplementedError):
            df.plot_estimator()
示例#13
0
 def test_no_estimator(self):
     df = pdml.ModelFrame(datasets.load_iris())
     with tm.assertRaises(ValueError):
         df.plot_estimator()