Examples

  1. Compute a distance between two graphs.

  2. Stochastic Gradient Descent applied to linear regression

Compute a distance between two graphs.

See Distance between two graphs.

<<<

import copy
from mlstatpy.graph import GraphDistance

# We define two graphs as list of edges.
graph1 = [("a", "b"), ("b", "c"), ("b", "X"), ("X", "c"),
          ("c", "d"), ("d", "e"), ("0", "b")]
graph2 = [("a", "b"), ("b", "c"), ("b", "X"), ("X", "c"),
          ("c", "t"), ("t", "d"), ("d", "e"), ("d", "g")]

# We convert them into objects GraphDistance.
graph1 = GraphDistance(graph1)
graph2 = GraphDistance(graph2)

distance, graph = graph1.distance_matching_graphs_paths(graph2, use_min=False)

print("distance", distance)
print("common paths:", graph)

>>>

    distance 0.3318250377073907
    common paths: 0
    X
    a
    b
    c
    d
    e
    00
    11
    g
    t
    a -> b []
    b -> c []
    b -> X []
    X -> c []
    c -> d []
    d -> e []
    0 -> b []
    00 -> a []
    00 -> 0 []
    e -> 11 []
    c -> 2a.t []
    2a.t -> d []
    d -> 2a.g []
    2a.g -> 11 []

(entrée originale : graph_distance.py:docstring of mlstatpy.graph.graph_distance.GraphDistance, line 3)

Stochastic Gradient Descent applied to linear regression

The following example how to optimize a simple linear regression.

<<<

import numpy
from mlstatpy.optim import SGDOptimizer


def fct_loss(c, X, y):
    return numpy.linalg.norm(X @ c - y) ** 2


def fct_grad(c, x, y, i=0):
    return x * (x @ c - y) * 0.1


coef = numpy.array([0.5, 0.6, -0.7])
X = numpy.random.randn(10, 3)
y = X @ coef

sgd = SGDOptimizer(numpy.random.randn(3))
sgd.train(X, y, fct_loss, fct_grad, max_iter=15, verbose=True)
print('optimized coefficients:', sgd.coef)

>>>

    0/15: loss: 32.43 lr=0.1 max(coef): 0.88 l1=0/1.8 l2=0/1.4
    1/15: loss: 14.24 lr=0.0302 max(coef): 0.41 l1=0.13/0.9 l2=0.0065/0.33
    2/15: loss: 5.635 lr=0.0218 max(coef): 0.12 l1=0.058/0.16 l2=0.0011/0.015
    3/15: loss: 3.931 lr=0.018 max(coef): 0.23 l1=0.078/0.47 l2=0.0029/0.084
    4/15: loss: 3.261 lr=0.0156 max(coef): 0.32 l1=0.036/0.59 l2=0.00062/0.16
    5/15: loss: 2.816 lr=0.014 max(coef): 0.33 l1=0.34/0.63 l2=0.045/0.19
    6/15: loss: 2.304 lr=0.0128 max(coef): 0.33 l1=0.14/0.7 l2=0.01/0.2
    7/15: loss: 1.93 lr=0.0119 max(coef): 0.36 l1=0.034/0.77 l2=0.00078/0.23
    8/15: loss: 1.647 lr=0.0111 max(coef): 0.38 l1=0.18/0.83 l2=0.014/0.25
    9/15: loss: 1.514 lr=0.0105 max(coef): 0.4 l1=0.015/0.87 l2=8.9e-05/0.27
    10/15: loss: 1.4 lr=0.00995 max(coef): 0.41 l1=0.13/0.91 l2=0.0075/0.3
    11/15: loss: 1.286 lr=0.00949 max(coef): 0.41 l1=0.005/0.95 l2=1.3e-05/0.32
    12/15: loss: 1.183 lr=0.00909 max(coef): 0.42 l1=0.15/0.98 l2=0.0098/0.34
    13/15: loss: 1.068 lr=0.00874 max(coef): 0.42 l1=0.15/1 l2=0.0088/0.35
    14/15: loss: 0.9454 lr=0.00842 max(coef): 0.42 l1=0.016/1 l2=0.00013/0.37
    15/15: loss: 0.8454 lr=0.00814 max(coef): 0.42 l1=0.11/1.1 l2=0.0059/0.39
    optimized coefficients: [ 0.325  0.417 -0.329]

(entrée originale : sgd.py:docstring of mlstatpy.optim.sgd.SGDOptimizer, line 34)