コード例 #1
0
def combined_sorted(lo=8, hi=12, output=True):
    """Generate results for different sorting trials."""
    tbl = DataTable([8] * (hi - lo + 1),
                    ['N'] + [comma(2**k) for k in range(lo, hi)],
                    output=output)

    for n in [2**k for k in range(lo, hi)]:
        row = [n]
        for m in [2**k for k in range(lo, hi)]:
            row.append(run_merge_trial(m, n))
        tbl.row(row)

    # Diagonal values are for 2*M*log(M) so divide in HALF for accurate one
    # build model ONLY for first five values
    x = [2**k for k in range(lo, min(lo + 5, hi))]
    y = [
        tbl.entry(r, comma(r))
        for r in [2**k for k in range(lo, min(lo + 5, hi))]
    ]
    if numpy_error:
        a = 0
    else:
        import numpy as np
        from scipy.optimize import curve_fit
        from scipy.stats.stats import pearsonr

        (coeffs, _) = curve_fit(n_log_n_model, np.array(x), np.array(y))
        a = coeffs[0] / 2

        y_fit = [
            n_log_n_model(r, a)
            for r in [2**k for k in range(lo, min(lo + 5, hi))]
        ]

        print()
        print(pearsonr(y, y_fit))
        print()
        print('Prediction')
        model = DataTable([8] * (hi - lo + 1),
                          ['N'] + [comma(2**k) for k in range(lo, hi)],
                          output=output)
        for n in [2**k for k in range(lo, hi)]:
            row = [n]
            for m in [2**k for k in range(lo, hi)]:
                row.append(n_log_n_model(n, a) + n_log_n_model(m, a))
            model.row(row)
    return tbl
コード例 #2
0
ファイル: test.py プロジェクト: heineman/LearningAlgorithms
    def test_allpairs_directed_sp(self):
        from ch07.all_pairs_sp import floyd_warshall, all_pairs_path_to, debug_state
        DG = nx.DiGraph()
        DG.add_edge('a', 'b', weight=4)
        DG.add_edge('b', 'a', weight=2)
        DG.add_edge('a', 'c', weight=3)
        DG.add_edge('b', 'd', weight=5)
        DG.add_edge('c', 'b', weight=6)
        DG.add_edge('d', 'b', weight=1)
        DG.add_edge('d', 'c', weight=7)
        (dist_to, node_from) = floyd_warshall(DG)

        path = all_pairs_path_to(node_from, 'b', 'c')
        self.assertEqual(5, dist_to['b']['c'])
        self.assertEqual(['b', 'a', 'c'], path)

        path = all_pairs_path_to(node_from, 'd', 'c')
        self.assertEqual(6, dist_to['d']['c'])
        self.assertEqual(['d', 'b', 'a', 'c'], path)

        (tbl, tbl_dist_to) = debug_state('test case',
                                         DG,
                                         node_from,
                                         dist_to,
                                         output=False)

        tbl_path = DataTable([6, 12, 12, 12, 12], ['.', 'a', 'b', 'c', 'd'],
                             output=False)
        tbl_path.format('.', 's')
        for f in 'abcd':
            tbl_path.format(f, 's')
        for u in 'abcd':
            path_row = [u]
            for v in 'abcd':
                if u == v:
                    path_row.append(SKIP)
                else:
                    path_row.append('->'.join(
                        all_pairs_path_to(node_from, u, v)))
            tbl_path.row(path_row)

        # edge on shortest path into 'c', when starting from 'd', came from 'a'
        self.assertEqual('d->b->a->c', tbl_path.entry('d', 'c'))
        self.assertEqual(6, tbl_dist_to.entry('d', 'c'))
        self.assertEqual('a', tbl.entry('d', 'c'))
コード例 #3
0
ファイル: test.py プロジェクト: heineman/LearningAlgorithms
    def test_table(self):
        tbl = DataTable([8, 8, 8], ['N', 'Another', 'SquareRoot'],
                        output=False,
                        decimals=4)
        tbl.format('Another', 'd')
        for n in range(2, 10):
            tbl.row([n, n, n**0.5])
        self.assertEqual(tbl.entry(3, 'Another'), 3)

        print('Testing that Table is print to console')
        tbl = DataTable([8, 8, 8], ['N', 'Another', 'SquareRoot'], decimals=4)
        tbl.format('Another', 'd')
        for n in range(2, 10):
            tbl.row([n, n, n**0.5])

        self.assertEqual(list(range(2, 10)), tbl.column('Another'))

        model = tbl.best_model('Another')[0]
        if numpy_error:
            pass
        else:
            self.assertEqual(model[0], Model.LINEAR)
            self.assertAlmostEqual(model[3], 1.0000, places=5)
コード例 #4
0
ファイル: test.py プロジェクト: heineman/LearningAlgorithms
    def test_allpairs_sp(self):
        from ch07.all_pairs_sp import floyd_warshall, all_pairs_path_to
        G = nx.Graph()
        G.add_edge('a', 'b', weight=3)
        G.add_edge('a', 'c', weight=5)
        G.add_edge('b', 'c', weight=9)
        G.add_edge('b', 'd', weight=2)
        G.add_edge('d', 'c', weight=1)
        G.add_edge('e', 'f', weight=1)  # separate and disconnected edge...
        (dist_to, node_from) = floyd_warshall(G)
        path = all_pairs_path_to(node_from, 'b', 'c')
        self.assertEqual(3, dist_to['b']['c'])
        self.assertEqual(['b', 'd', 'c'], path)

        path = all_pairs_path_to(node_from, 'a', 'd')
        self.assertEqual(5, dist_to['a']['d'])
        self.assertEqual(['a', 'b', 'd'], path)

        with self.assertRaises(ValueError):
            all_pairs_path_to(node_from, 'a', 'e')

        tbl = DataTable([6, 6, 6, 6, 6], ['.', 'a', 'b', 'c', 'd'],
                        output=False)
        tbl.format('.', 's')
        for f in 'abcd':
            tbl.format(f, 's')
        for u in 'abcd':
            row = [u]
            for v in 'abcd':
                if node_from[u][v]:
                    row.append(node_from[u][v])
                else:
                    row.append(SKIP)
            tbl.row(row)

        self.assertEqual('d', tbl.entry('b', 'c'))