示例#1
0
    def test_rank2d_spearman(self):
        """
        Test Rank2D using spearman metric
        """
        X, _ = load_energy(return_dataset=True).to_numpy()
        oz = Rank2D(algorithm="spearman")
        npt.assert_array_equal(oz.fit_transform(X), X)

        # Check Ranking
        expected = np.array(
            [
                [1.0, -1.0, -0.25580533, -0.8708862, 0.86904819, 0.0, 0.0, 0.0],
                [-1.0, 1.0, 0.25580533, 0.8708862, -0.86904819, 0.0, 0.0, 0.0],
                [-0.25580533, 0.25580533, 1.0, -0.19345677, 0.22076336, 0.0, 0.0, 0.0],
                [-0.8708862, 0.8708862, -0.19345677, 1.0, -0.93704257, 0.0, 0.0, 0.0],
                [0.86904819, -0.86904819, 0.22076336, -0.93704257, 1.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.18759162],
                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18759162, 1.0],
            ]
        )

        assert hasattr(oz, "ranks_")
        assert oz.ranks_.shape == (X.shape[1], X.shape[1])
        npt.assert_array_almost_equal(oz.ranks_, expected)

        # Image similarity comparision
        oz.finalize()
        self.assert_images_similar(oz, tol=0.1)
示例#2
0
    def test_rank2d_kendalltau(self):
        """
        Test Rank2D using kendalltau metric
        """
        X, _ = load_energy(return_dataset=True).to_numpy()
        oz = Rank2D(algorithm="kendalltau")
        npt.assert_array_equal(oz.fit_transform(X), X)

        # Check Ranking
        expected = np.array(
            [
                [1.0, -1.0, -0.2724275, -0.73614431, 0.73854895, 0.0, 0.0, 0.0],
                [-1.0, 1.0, 0.2724275, 0.73614431, -0.73854895, 0.0, 0.0, 0.0],
                [-0.2724275, 0.2724275, 1.0, -0.15192004, 0.19528337, 0.0, 0.0, 0.0],
                [-0.73614431, 0.73614431, -0.15192004, 1.0, -0.87518995, 0.0, 0.0, 0.0],
                [0.73854895, -0.73854895, 0.19528337, -0.87518995, 1.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.15430335],
                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15430335, 1.0],
            ]
        )

        assert hasattr(oz, "ranks_")
        assert oz.ranks_.shape == (X.shape[1], X.shape[1])
        npt.assert_array_almost_equal(oz.ranks_, expected)

        # Image similarity comparision
        oz.finalize()
        self.assert_images_similar(oz, tol=0.1)
示例#3
0
 def test_rank2d_unknown_algorithm(self):
     """
     Test that an error is raised for Rank2D with an unknown algorithm
     """
     X, _ = load_energy()
     msg = "'oscar' is unrecognized ranking method"
     with pytest.raises(YellowbrickValueError, match=msg):
         Rank2D(algorithm="Oscar").transform(X)
示例#4
0
def rank2d(X, y, outpath, **kwargs):
    # Create a new figure and axes
    _, ax = plt.subplots()

    # Create the visualizer
    visualizer = Rank2D(ax=ax, **kwargs)
    visualizer.fit(X, y)
    visualizer.transform(X)

    # Save to disk
    visualizer.poof(outpath=outpath)
示例#5
0
def rank2d(X, y, outpath, **kwargs):
    # Create a new figure and axes
    fig = plt.figure()
    ax = fig.add_subplot(111)

    # Create the visualizer
    visualizer = Rank2D(**kwargs)
    visualizer.fit(X, y)
    visualizer.transform(X)

    # Save to disk
    visualizer.poof(outpath=outpath)
示例#6
0
    def test_rank2d_integrated_numpy(self):
        """
        Test Rank2D on occupancy dataset with numpy ndarray
        """
        data = load_occupancy(return_dataset=True)
        X, y = data.to_numpy()
        features = data.meta["features"]

        assert isinstance(X, np.ndarray)
        assert isinstance(y, np.ndarray)

        # Test the visualizer
        oz = Rank2D(features=features, show_feature_names=True)
        assert oz.fit(X, y) is oz
        assert oz.transform(X) is X
        oz.finalize()

        # Image similarity testing
        self.assert_images_similar(oz, tol=0.1)
示例#7
0
    def test_rank2d_integrated_pandas(self):
        """
        Test Rank2D on occupancy dataset with pandas DataFrame and Series
        """
        data = load_occupancy(return_dataset=True)
        X, y = data.to_pandas()
        features = data.meta["features"]

        assert isinstance(X, pd.DataFrame)
        assert isinstance(y, pd.Series)

        # Test the visualizer
        oz = Rank2D(features=features, show_feature_names=True)
        assert oz.fit(X, y) is oz
        assert oz.transform(X) is X
        oz.finalize()

        # Image similarity testing
        self.assert_images_similar(oz, tol=0.1)
示例#8
0
def showRank2D():
    # Load the dataset
    data = load_data('credit')

    # Specify the features of interest
    features = [
        'limit',
        'sex',
        'edu',
        'married',
        'age',
        'apr_delay',
        'may_delay',
        'jun_delay',
        'jul_delay',
        'aug_delay',
        'sep_delay',
        'apr_bill',
        'may_bill',
        'jun_bill',
        'jul_bill',
        'aug_bill',
        'sep_bill',
        'apr_pay',
        'may_pay',
        'jun_pay',
        'jul_pay',
        'aug_pay',
        'sep_pay',
    ]

    # Extract the numpy arrays from the data frame
    X = data[features].as_matrix()
    y = data.default.as_matrix()

    # Instantiate the visualizer with the Covariance ranking algorithm
    visualizer = Rank2D(features=features, algorithm='pearson')

    visualizer.fit(X, y)  # Fit the data to the visualizer
    visualizer.transform(X)  # Transform the data
    visualizer.poof()  # Draw/show/poof the data
with open('forest-riders.pkl', 'wb') as f:
    pickle.dump(model, f)

with open('forest-riders.pkl', 'rb') as f:
    model = pickle.load(f)

from pandas.tools.plotting import radviz

plt.figure(figsize=(12, 12))
radviz(dataset, 'Yield')
plt.show()

from yellowbrick.features.rankd import Rank2D
# Instantiate the visualizer with the Covariance ranking algorithm
visualizer = Rank2D(features=features, algorithm='covariance')

visualizer.fit(X, y)  # Fit the data to the visualizer
visualizer.transform(X)  # Transform the data
visualizer.poof()  # Draw/show/poof the data

################################################3
#######RANDOM FOREST REGRESSOR TREE######

# features = ['Year','Harvested','Value','Grow_total_p','Grow_avg_t','Price']
# target = 'Yield'
#
# from sklearn.datasets import make_regression
# from sklearn.ensemble import RandomForestRegressor
#
# X = (dataset[features])
示例#10
0
    def test_rank2d_covariance(self):
        """
        Test Rank2D using covariance metric
        """
        X, _ = load_energy(return_dataset=True).to_numpy()
        oz = Rank2D(algorithm="covariance")
        npt.assert_array_equal(oz.fit_transform(X), X)

        # Check Ranking
        expected = np.array(
            [
                [
                    1.11888744e-02,
                    -9.24206867e00,
                    -9.40391134e-01,
                    -4.15083877e00,
                    1.53324641e-01,
                    0.00000000e00,
                    1.57414282e-18,
                    -1.85278419e-17,
                ],
                [
                    -9.24206867e00,
                    7.75916384e03,
                    7.51290743e02,
                    3.50393655e03,
                    -1.32370274e02,
                    0.00000000e00,
                    -2.65874531e-15,
                    -4.86170571e-14,
                ],
                [
                    -9.40391134e-01,
                    7.51290743e02,
                    1.90326988e03,
                    -5.75989570e02,
                    2.14654498e01,
                    0.00000000e00,
                    4.57406096e-17,
                    0.00000000e00,
                ],
                [
                    -4.15083877e00,
                    3.50393655e03,
                    -5.75989570e02,
                    2.03996306e03,
                    -7.69178618e01,
                    0.00000000e00,
                    -1.97089918e-15,
                    1.54151644e-14,
                ],
                [
                    1.53324641e-01,
                    -1.32370274e02,
                    2.14654498e01,
                    -7.69178618e01,
                    3.06649283e00,
                    0.00000000e00,
                    -2.89497529e-19,
                    0.00000000e00,
                ],
                [
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    1.25162973e00,
                    -3.61871912e-20,
                    0.00000000e00,
                ],
                [
                    1.57414282e-18,
                    -2.65874531e-15,
                    4.57406096e-17,
                    -1.97089918e-15,
                    -2.89497529e-19,
                    -3.61871912e-20,
                    1.77477184e-02,
                    4.40026076e-02,
                ],
                [
                    -1.85278419e-17,
                    -4.86170571e-14,
                    0.00000000e00,
                    1.54151644e-14,
                    0.00000000e00,
                    0.00000000e00,
                    4.40026076e-02,
                    2.40547588e00,
                ],
            ]
        )

        assert hasattr(oz, "ranks_")
        assert oz.ranks_.shape == (X.shape[1], X.shape[1])
        npt.assert_array_almost_equal(oz.ranks_, expected, decimal=5)

        # Image similarity comparision
        oz.finalize()
        self.assert_images_similar(oz, tol=0.1)
示例#11
0
    def test_rank2d_pearson(self):
        """
        Test Rank2D using pearson metric
        """
        X, _ = load_energy(return_dataset=True).to_numpy()
        oz = Rank2D(algorithm="pearson")
        npt.assert_array_equal(oz.fit_transform(X), X)

        # Check Ranking
        expected = np.array(
            [
                [
                    1.00000000e00,
                    -9.91901462e-01,
                    -2.03781680e-01,
                    -8.68823408e-01,
                    8.27747317e-01,
                    0.00000000e00,
                    1.11706815e-16,
                    -1.12935670e-16,
                ],
                [
                    -9.91901462e-01,
                    1.00000000e00,
                    1.95501633e-01,
                    8.80719517e-01,
                    -8.58147673e-01,
                    0.00000000e00,
                    -2.26567708e-16,
                    -3.55861251e-16,
                ],
                [
                    -2.03781680e-01,
                    1.95501633e-01,
                    1.00000000e00,
                    -2.92316466e-01,
                    2.80975743e-01,
                    0.00000000e00,
                    7.87010445e-18,
                    0.00000000e00,
                ],
                [
                    -8.68823408e-01,
                    8.80719517e-01,
                    -2.92316466e-01,
                    1.00000000e00,
                    -9.72512237e-01,
                    0.00000000e00,
                    -3.27553310e-16,
                    2.20057668e-16,
                ],
                [
                    8.27747317e-01,
                    -8.58147673e-01,
                    2.80975743e-01,
                    -9.72512237e-01,
                    1.00000000e00,
                    0.00000000e00,
                    -1.24094525e-18,
                    0.00000000e00,
                ],
                [
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    0.00000000e00,
                    1.00000000e00,
                    -2.42798319e-19,
                    0.00000000e00,
                ],
                [
                    1.11706815e-16,
                    -2.26567708e-16,
                    7.87010445e-18,
                    -3.27553310e-16,
                    -1.24094525e-18,
                    -2.42798319e-19,
                    1.00000000e00,
                    2.12964221e-01,
                ],
                [
                    -1.12935670e-16,
                    -3.55861251e-16,
                    0.00000000e00,
                    2.20057668e-16,
                    0.00000000e00,
                    0.00000000e00,
                    2.12964221e-01,
                    1.00000000e00,
                ],
            ]
        )

        assert hasattr(oz, "ranks_")
        assert oz.ranks_.shape == (X.shape[1], X.shape[1])
        npt.assert_array_almost_equal(oz.ranks_, expected)

        # Image similarity comparision
        oz.finalize()
        # Travis Python 3.6 images not close (RMS 0.112)
        self.assert_images_similar(oz, tol=0.5)
示例#12
0
 def draw_rank_2d(self, algo='pearson'):
     visualizer = Rank2D(features=self.get_feature_labels(), algorithm=algo)
     visualizer.fit(self.training_data, self.training_labels)
     visualizer.transform(self.training_data)
     visualizer.poof()