Exemple #1
0
def least_squares_fit(xs: List[Vector],
                      ys: List[float],
                      learning_rate: float = 0.001,
                      num_steps: int = 1000,
                      batch_size: int = 1) -> Vector:
    """
    Find the beta that minimizes the sum of squared errors
    assuming the model y = dot(x, beta)
    """

    # Start with a random guess
    guess = [random.random() for _ in xs[0]]

    for _ in tqdm.trange(num_steps, desc="least squares fit"):
        for start in range(0, len(xs), batch_size):
            batch_xs = xs[start:start + batch_size]
            batch_ys = ys[start:start + batch_size]

            gradient = vector_mean([
                sqerror_gradient(x, y, guess)
                for x, y in zip(batch_xs, batch_ys)
            ])
            guess = gradient_step(guess, gradient, -learning_rate)

    return guess
def scale(data: List[Vector]) -> Tuple[Vector, Vector]:
    """returns the means and standard deviations for each position"""
    dim = len(data[0])

    means = vector_mean(data)
    stdevs = [standard_deviation([vector[i] for vector in data])
              for i in range(dim)]

    return means, stdevs
def cluster_means(k, inputs, assignments):
    clusters = [[] for i in range(k)]
    for input, assignment in zip(inputs, assignments):
        clusters[assignment].append(input)

    return [
        vector_mean(cluster) if cluster else random.choice(inputs)
        for cluster in clusters
    ]
def scale(data: List[Vector]) -> Tuple[Vector, Vector]:
    """returns the means and standard deviations for each position"""
    dim = len(data[0])

    means = vector_mean(data)
    stdevs = [standard_deviation([vector[i] for vector in data])
              for i in range(dim)]

    return means, stdevs
def scale(data):
    dim = len(data[0])

    means = vector_mean(data)
    stdevs = [
        standard_deviation([vector[i] for vector in data]) for i in range(dim)
    ]

    return means, stdevs
def cluster_means(k: int,
                  inputs: List[Vector],
                  assignments: List[int]) -> List[Vector]:
    # clusters[i] contains the inputs whose assignment is i
    clusters = [[] for i in range(k)]
    for input, assignment in zip(inputs, assignments):
        clusters[assignment].append(input)

    # if a cluster is empty, just use a random point
    return [vector_mean(cluster) if cluster else random.choice(inputs)
            for cluster in clusters]
Exemple #7
0
def cluster_means(
        k: int, inputs: List[Vector],
        assignments: List[int]) -> List[Vector]:  # 클러스터 소속을 표시하는 번호 리스트
    clusters = [[] for i in range(k)]
    for input, assignment in zip(inputs, assignments):
        clusters[assignment].append(input)

    # if a cluster is empty, just use a random point
    return [
        vector_mean(cluster) if cluster else random.choice(inputs)
        for cluster in clusters
    ]
def cluster_means(k: int, inputs: List[Vector],
                  assignments: List[int]) -> List[Vector]:
    # clusters[i] zawiera punkty, które są przypisane do i
    clusters = [[] for i in range(k)]
    for input, assignment in zip(inputs, assignments):
        clusters[assignment].append(input)

    # jeżeli klaster jest pusty, użyj losowego punktu
    return [
        vector_mean(cluster) if cluster else random.choice(inputs)
        for cluster in clusters
    ]
def least_squares_fit(xs: List[Vector],
                      ys: List[float],
                      learning_rate: float = 0.001,
                      num_steps: int = 1000,
                      batch_size: int = 1) -> Vector:
    guess = [random.random() for _ in xs[0]]

    for _ in tqdm.trange(num_steps, desc="least squares fit"):
        for start in range(0, len(xs), batch_size):
            batch_xs = xs[start:start + batch_size]
            batch_ys = ys[start:start + batch_size]

            gradient = vector_mean([
                squared_gradient(x, y, guess)
                for x, y in zip(batch_xs, batch_ys)
            ])
            guess = gradient_step(guess, gradient, -learning_rate)

    return guess
Exemple #10
0
def least_squares_fit_ridge(xs: List[Vector],
                            ys: List[float],
                            alpha: float,
                            learning_rate: float,
                            num_steps: int,
                            batch_size: int = 1) -> Vector:
    # Rozpoczynamy od średniej
    guess = [random.random() for _ in xs[0]]

    for i in range(num_steps):
        for start in range(0, len(xs), batch_size):
            batch_xs = xs[start:start + batch_size]
            batch_ys = ys[start:start + batch_size]

            gradient = vector_mean([
                sqerror_ridge_gradient(x, y, guess, alpha)
                for x, y in zip(batch_xs, batch_ys)
            ])
            guess = gradient_step(guess, gradient, -learning_rate)

    return guess
def minibatches(dataset: List[T],
                batch_size: int,
                shuffle: bool = True) -> Iterator[List[T]]:
    #dataset에서 batch_size만큼 데이터 포인트를 샘플링해서 미니배치를 생성
    #각 미니배치의 시작점인 0, batch_size, 2 * batch_size, ...을 나열
    batch_starts = [start for start in range(0, len(dataset), batch_size)]

    if shuffle: random.shuffle(batch_starts)  #미니배치의 순서를 섞는다.

    for start in batch_starts:
        end = start + batch_size
        yield dataset[start:end]

    #미니배치
    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]

    for epoch in range(1000):
        for batch in minibatches(inputs, batch_size=20):
            grad = vector_mean(
                [linear_gradient(x, y, theta) for x, y in batch])
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)

    slope, intercept = theta
    assert 19.9 < slope < 20.1
    assert 4.9 < intercept < 5.1

    #SGD(stochastic gradient descent)
    for epoch in range(500):
        for x, y in inputs:
            grad = linear_gradient(x, y, theta)
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)

    slope, intercept = theta
    assert 19.9 < slope < 20.1
    assert 4.9 < intercept < 5.1
Exemple #12
0
def least_squares_fit(xs: List[Vector],
                      ys: List[float],
                      learning_rate: float = 0.001,
                      num_steps: int = 1000,
                      batch_size: int = 1) -> Vector:
    """
    Znajdź beta, które minimalizuje sumę kwadratów błędów,
    zakładając model y = dot(x, beta).
    """
    # Rozpoczynamy od losowej wartości
    guess = [random.random() for _ in xs[0]]

    for _ in tqdm.trange(num_steps, desc="least squares fit"):
        for start in range(0, len(xs), batch_size):
            batch_xs = xs[start:start + batch_size]
            batch_ys = ys[start:start + batch_size]

            gradient = vector_mean([
                sqerror_gradient(x, y, guess)
                for x, y in zip(batch_xs, batch_ys)
            ])
            guess = gradient_step(guess, gradient, -learning_rate)

    return guess
Exemple #13
0
def main():
    inputs: List[List[float]] = [[-14, -5], [13, 13], [20, 23], [-19, -11],
                                 [-9, -16], [21, 27], [-49, 15], [26, 13],
                                 [-46, 5], [-34, -1], [11, 15], [-49, 0],
                                 [-22, -16], [19, 28], [-12, -8], [-13, -19],
                                 [-41, 8], [-11, -6], [-25, -9], [-18, -3]]

    random.seed(12)  # so you get the same results as me
    clusterer = KMeans(k=3)
    clusterer.train(inputs)
    means = sorted(clusterer.means)  # sort for the unit test // means만 남음
    print("means when k=3" + str(means))

    random.seed(0)
    clusterer = KMeans(k=2)
    clusterer.train(inputs)
    means = sorted(clusterer.means)
    print("means when k=2" + str(means))

    # plot from 1 up to len(inputs) clusters // k 값 선택하기
    # ks = range(1, len(inputs) + 1)
    # errors = [squared_clustering_errors(inputs, k) for k in ks]
    #
    # plt.plot(ks, errors)
    # plt.xticks(ks)
    # plt.xlabel("k")
    # plt.ylabel("total squared error")
    # plt.title("Total Error vs. # of Clusters")
    # plt.show()
    #
    # plt.savefig('im/total_error_vs_num_clusters')
    # plt.gca().clear()

    # --------------------RGB 군집화---------------------
    # image_path = r"girl_with_book.jpg"
    # import matplotlib.image as mpimg
    # img = mpimg.imread(image_path) / 256  # rescale to between 0 and 1
    # # print(img[:1])
    #
    # pixels = [pixel.tolist() for row in img for pixel in row]
    #
    # clusterer = KMeans(5)
    # clusterer.train(pixels)
    #
    # def recolor(pixel: Vector) -> Vector:
    #     cluster = clusterer.classify(pixel)
    #     return clusterer.means[cluster]
    #
    # new_img = [[recolor(pixel) for pixel in row] for row in img]
    #
    # plt.close()
    # plt.imshow(new_img)
    # plt.axis('off')
    # # plt.show()
    #
    # plt.savefig('im/recolored_girl_with_book.jpg')
    # plt.gca().clear()

    # base_cluster = bottom_up_cluster(inputs)
    # # print(base_cluster)
    # three_cluster = [get_values(cluster)
    #                  for cluster in generate_clusters(base_cluster, 3)]
    #
    # for i, cluster, marker, color in zip([1, 2, 3],
    #                                      three_cluster,
    #                                      ['D', 'o', '*'],
    #                                      ['r', 'g', 'b']):
    #     xs, ys = zip(*cluster)
    #     plt.scatter(xs, ys, color=color, marker=marker)
    #
    #     x, y = vector_mean(cluster)
    #     plt.plot(x, y, marker='$' + str(i) + '$', color='black')
    #
    # plt.title("User Locations -- 3 Bottom-Up clusters, Min")
    # plt.xlabel("blocks east of city center")
    # plt.ylabel("blocks north of city center")
    # # plt.show()

    base_cluster_max = bottom_up_cluster(inputs, max)
    three_cluster_max = [
        get_values(cluster)
        for cluster in generate_clusters(base_cluster_max, 3)
    ]

    for i, cluster, marker, color in zip([1, 2, 3], three_cluster_max,
                                         ['D', 'o', '*'], ['r', 'g', 'b']):
        xs, ys = zip(*cluster)
        plt.scatter(xs, ys, color=color, marker=marker)

        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')

    plt.title("User Locations -- 3 Bottom-Up clusters, Min")
    plt.xlabel("blocks east of city center")
    plt.ylabel("blocks north of city center")
    plt.show()
Exemple #14
0
    grad = [2 * error * x, 2 * error]           # gradient를 사용
    return grad


"""평균 제곱 오차( mean squared error )
 : 각 데이터 포인트에서 계산된 gradient의 평균

1. 임의의 theta로 시작
2. 모든 gradient의 평균을 계산
3. theta를 2번에서 계산된 값으로 변경
4. 반복
"""
from scratch.linear_algebra import vector_mean

# 임의의 경사와 절편으로 시작
import random
theta = [random.uniform(-1, 1), random.uniform(-1, 1)]

learning_rate = 0.001

for epoch in range(5000):
    # 모든 gradient의 평균을 계산
    grad = vector_mean([linear_gradient(x, y, theta) for x, y in inputs])
    # gradient만큼 이동
    theta = gradient_step(theta, grad, -learning_rate)
    print(epoch, theta)

slope, intercept = theta
assert 19.9 < slope < 20.1, "slope should be about 20"
assert 4.9 < intercept < 5.1, "intercept should be about 5"
def main():
    
    
    inputs: List[List[float]] = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]
    
    random.seed(12)                   # so you get the same results as me
    clusterer = KMeans(k=3)
    clusterer.train(inputs)
    means = sorted(clusterer.means)   # sort for the unit test
    
    assert len(means) == 3
    
    # Check that the means are close to what we expect.
    assert squared_distance(means[0], [-44, 5]) < 1
    assert squared_distance(means[1], [-16, -10]) < 1
    assert squared_distance(means[2], [18, 20]) < 1
    
    random.seed(0)
    clusterer = KMeans(k=2)
    clusterer.train(inputs)
    means = sorted(clusterer.means)
    
    assert len(means) == 2
    assert squared_distance(means[0], [-26, -5]) < 1
    assert squared_distance(means[1], [18, 20]) < 1
    
    from matplotlib import pyplot as plt
    
    def squared_clustering_errors(inputs: List[Vector], k: int) -> float:
        """finds the total squared error from k-means clustering the inputs"""
        clusterer = KMeans(k)
        clusterer.train(inputs)
        means = clusterer.means
        assignments = [clusterer.classify(input) for input in inputs]
    
        return sum(squared_distance(input, means[cluster])
                   for input, cluster in zip(inputs, assignments))
    
    # now plot from 1 up to len(inputs) clusters
    
    ks = range(1, len(inputs) + 1)
    errors = [squared_clustering_errors(inputs, k) for k in ks]
    
    plt.plot(ks, errors)
    plt.xticks(ks)
    plt.xlabel("k")
    plt.ylabel("total squared error")
    plt.title("Total Error vs. # of Clusters")
    # plt.show()
    
    
    
    plt.savefig('im/total_error_vs_num_clusters')
    plt.gca().clear()
    
    image_path = r"girl_with_book.jpg"    # wherever your image is
    import matplotlib.image as mpimg
    img = mpimg.imread(image_path) / 256  # rescale to between 0 and 1
    
    # .tolist() converts a numpy array to a Python list
    pixels = [pixel.tolist() for row in img for pixel in row]
    
    clusterer = KMeans(5)
    clusterer.train(pixels)   # this might take a while
    
    def recolor(pixel: Vector) -> Vector:
        cluster = clusterer.classify(pixel)        # index of the closest cluster
        return clusterer.means[cluster]            # mean of the closest cluster
    
    new_img = [[recolor(pixel) for pixel in row]   # recolor this row of pixels
               for row in img]                     # for each row in the image
    
    
    plt.close()
    
    plt.imshow(new_img)
    plt.axis('off')
    # plt.show()
    
    
    
    plt.savefig('im/recolored_girl_with_book.jpg')
    plt.gca().clear()
    
    base_cluster = bottom_up_cluster(inputs)
    
    three_clusters = [get_values(cluster)
                      for cluster in generate_clusters(base_cluster, 3)]
    
    
    
    # sort smallest to largest
    tc = sorted(three_clusters, key=len)
    assert len(tc) == 3
    assert [len(c) for c in tc] == [2, 4, 14]
    assert sorted(tc[0]) == [[11, 15], [13, 13]]
    
    
    plt.close()
    
    for i, cluster, marker, color in zip([1, 2, 3],
                                         three_clusters,
                                         ['D','o','*'],
                                         ['r','g','b']):
        xs, ys = zip(*cluster)  # magic unzipping trick
        plt.scatter(xs, ys, color=color, marker=marker)
    
        # put a number at the mean of the cluster
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')
    
    plt.title("User Locations -- 3 Bottom-Up Clusters, Min")
    plt.xlabel("blocks east of city center")
    plt.ylabel("blocks north of city center")
    # plt.show()
    
    
    
    plt.savefig('im/bottom_up_clusters_min.png')
    plt.gca().clear()
    plt.close()
    
    
    
    base_cluster_max = bottom_up_cluster(inputs, max)
    three_clusters_max = [get_values(cluster)
                          for cluster in generate_clusters(base_cluster_max, 3)]
    
    for i, cluster, marker, color in zip([1, 2, 3],
                                         three_clusters_max,
                                         ['D','o','*'],
                                         ['r','g','b']):
        xs, ys = zip(*cluster)  # magic unzipping trick
        plt.scatter(xs, ys, color=color, marker=marker)
    
        # put a number at the mean of the cluster
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')
    
    plt.title("User Locations -- 3 Bottom-Up Clusters, Max")
    plt.xlabel("blocks east of city center")
    plt.ylabel("blocks north of city center")
    plt.savefig('im/bottom_up_clusters_max.png')
    plt.gca().clear()
Exemple #16
0
def main():
    xs = range(-10, 11)
    actuals = [derivative(x) for x in xs]
    estimates = [difference_quotient(square, x, h=0.001) for x in xs]

    # plot to show they're basically the same
    import matplotlib.pyplot as plt
    plt.title("Actual Derivatives vs. Estimates")
    plt.plot(xs, actuals, 'rx', label='Actual')  # red  x
    plt.plot(xs, estimates, 'b+', label='Estimate')  # blue +
    plt.legend(loc=9)
    # plt.show()

    plt.close()

    def partial_difference_quotient(f: Callable[[Vector], float], v: Vector,
                                    i: int, h: float) -> float:
        """Returns the i-th partial difference quotient of f at v"""
        w = [
            v_j + (h if j == i else 0)  # add h to just the ith element of v
            for j, v_j in enumerate(v)
        ]

        return (f(w) - f(v)) / h

    # "Using the Gradient" example

    # pick a random starting point
    v = [random.uniform(-10, 10) for i in range(3)]

    for epoch in range(1000):
        grad = sum_of_squares_gradient(v)  # compute the gradient at v
        v = gradient_step(v, grad, -0.01)  # take a negative gradient step
        print(epoch, v)

    assert distance(v, [0, 0, 0]) < 0.001  # v should be close to 0

    # First "Using Gradient Descent to Fit Models" example

    from scratch.linear_algebra import vector_mean

    # Start with random values for slope and intercept.
    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]

    learning_rate = 0.001

    for epoch in range(5000):
        # Compute the mean of the gradients
        grad = vector_mean([linear_gradient(x, y, theta) for x, y in inputs])
        # Take a step in that direction
        theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)

    slope, intercept = theta
    assert 19.9 < slope < 20.1, "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"

    # Minibatch gradient descent example

    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]

    for epoch in range(1000):
        for batch in minibatches(inputs, batch_size=20):
            grad = vector_mean(
                [linear_gradient(x, y, theta) for x, y in batch])
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)

    slope, intercept = theta
    assert 19.9 < slope < 20.1, "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"

    # Stochastic gradient descent example

    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]

    for epoch in range(100):
        for x, y in inputs:
            grad = linear_gradient(x, y, theta)
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)

    slope, intercept = theta
    assert 19.9 < slope < 20.1, "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"
def de_mean(data: List[Vector]) -> List[Vector]:
    """Recenters the data to have mean 0 in every dimension"""
    mean = vector_mean(data)
    return [subtract(vector, mean) for vector in data]
Exemple #18
0
def main():

    inputs: List[List[float]] = [[-14, -5], [13, 13], [20, 23], [-19, -11],
                                 [-9, -16], [21, 27], [-49, 15], [26, 13],
                                 [-46, 5], [-34, -1], [11, 15], [-49, 0],
                                 [-22, -16], [19, 28], [-12, -8], [-13, -19],
                                 [-41, 8], [-11, -6], [-25, -9], [-18, -3]]

    random.seed(12)  # Dzięki temu uzyskasz taki sam wynik jak ja.
    clusterer = KMeans(k=3)
    clusterer.train(inputs)
    means = sorted(clusterer.means)  # sortowanie dla testów jednostkowych

    assert len(means) == 3

    # Sprawdź, czy średnie są takie, jakich oczekiwaliśmy
    assert squared_distance(means[0], [-44, 5]) < 1
    assert squared_distance(means[1], [-16, -10]) < 1
    assert squared_distance(means[2], [18, 20]) < 1

    random.seed(0)
    clusterer = KMeans(k=2)
    clusterer.train(inputs)
    means = sorted(clusterer.means)

    assert len(means) == 2
    assert squared_distance(means[0], [-26, -5]) < 1
    assert squared_distance(means[1], [18, 20]) < 1

    from matplotlib import pyplot as plt

    def squared_clustering_errors(inputs: List[Vector], k: int) -> float:
        """Określa sumę błędów podniesionych do kwadratu
        uzyskanych w wyniku działania algorytmu k średnich"""
        clusterer = KMeans(k)
        clusterer.train(inputs)
        means = clusterer.means
        assignments = [clusterer.classify(input) for input in inputs]

        return sum(
            squared_distance(input, means[cluster])
            for input, cluster in zip(inputs, assignments))

    # Wykonaj wykres dla podziału od 1 grupy do len(inputs) grup.

    ks = range(1, len(inputs) + 1)
    errors = [squared_clustering_errors(inputs, k) for k in ks]

    plt.plot(ks, errors)
    plt.xticks(ks)
    plt.xlabel("k")
    plt.ylabel("Suma kwadratow bledow")
    plt.title("Blad calkowity a liczba grup")
    plt.show()

    plt.savefig('im/total_error_vs_num_clusters')
    plt.gca().clear()

    image_path = r"girl_with_book.jpg"  # ścieżka pliku obrazu
    import matplotlib.image as mpimg
    img = mpimg.imread(
        image_path
    ) / 256  # przeskalujmy, aby uzyskać wartości z przedziału od 0 do 1

    # .tolist() konwertuje tablicę NumPy na obiekt list
    pixels = [pixel.tolist() for row in img for pixel in row]

    clusterer = KMeans(5)
    clusterer.train(pixels)  # Operacja ta może być czasochłonna.

    def recolor(pixel: Vector) -> Vector:
        cluster = clusterer.classify(pixel)  # indeks najbliższej grupy
        return clusterer.means[cluster]  # średnia najbliższej grupy

    new_img = [
        [recolor(pixel) for pixel in row]  # Zmień kolor tego rzędu pikseli.
        for row in img
    ]  # Wykonaj tę operację dla każdego wiersza obrazu.

    plt.close()

    plt.imshow(new_img)
    plt.axis('off')
    plt.show()

    plt.savefig('im/recolored_girl_with_book.jpg')
    plt.gca().clear()

    base_cluster = bottom_up_cluster(inputs)

    three_clusters = [
        get_values(cluster) for cluster in generate_clusters(base_cluster, 3)
    ]

    # posortuj od najmniejszego do największego
    tc = sorted(three_clusters, key=len)
    assert len(tc) == 3
    assert [len(c) for c in tc] == [2, 4, 14]
    assert sorted(tc[0]) == [[11, 15], [13, 13]]

    plt.close()

    for i, cluster, marker, color in zip([1, 2, 3], three_clusters,
                                         ['D', 'o', '*'], ['r', 'g', 'b']):
        xs, ys = zip(*cluster)  # rozpakowywanie
        plt.scatter(xs, ys, color=color, marker=marker)

        # Wprowadź średnią klastra.
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')

    plt.title("Miejsca zamieszkania (3 grupy, metoda bottom-up, minimum)")
    plt.xlabel("Liczba przecznic na wschod od centrum miasta ")
    plt.ylabel("Liczba przecznic na polnoc od centrum miasta ")
    plt.show()

    plt.savefig('im/bottom_up_clusters_min.png')
    plt.gca().clear()
    plt.close()

    base_cluster_max = bottom_up_cluster(inputs, max)
    three_clusters_max = [
        get_values(cluster)
        for cluster in generate_clusters(base_cluster_max, 3)
    ]

    for i, cluster, marker, color in zip([1, 2, 3], three_clusters_max,
                                         ['D', 'o', '*'], ['r', 'g', 'b']):
        xs, ys = zip(*cluster)  # rozpakowywanie
        plt.scatter(xs, ys, color=color, marker=marker)

        # Wprowadź średnią klastra.
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')

    plt.title("Miejsca zamieszkania (3 grupy, metoda bottom-up, maksimum)")
    plt.xlabel("Liczba przecznic na wschod od centrum miasta ")
    plt.ylabel("Liczba przecznic na polnoc od centrum miasta ")
    plt.savefig('im/bottom_up_clusters_max.png')
    plt.gca().clear()
Exemple #19
0
def de_mean(data: List[Vector]) -> List[Vector]:
    """Recenters the data to have mean 0 in every dimension"""
    mean = vector_mean(data)
    return [subtract(vector, mean) for vector in data]
Exemple #20
0
def main():

    inputs: List[List[float]] = [[-14, -5], [13, 13], [20, 23], [-19, -11],
                                 [-9, -16], [21, 27], [-49, 15], [26, 13],
                                 [-46, 5], [-34, -1], [11, 15], [-49, 0],
                                 [-22, -16], [19, 28], [-12, -8], [-13, -19],
                                 [-41, 8], [-11, -6], [-25, -9], [-18, -3]]

    random.seed(12)  # so you get the same results as me
    clusterer = KMeans(k=3)
    clusterer.train(inputs)
    means = sorted(clusterer.means)  # sort for the unit test

    assert len(means) == 3

    # Check that the means are close to what we expect.
    assert squared_distance(means[0], [-44, 5]) < 1
    assert squared_distance(means[1], [-16, -10]) < 1
    assert squared_distance(means[2], [18, 20]) < 1

    random.seed(0)
    clusterer = KMeans(k=2)
    clusterer.train(inputs)
    means = sorted(clusterer.means)

    assert len(means) == 2
    assert squared_distance(means[0], [-26, -5]) < 1
    assert squared_distance(means[1], [18, 20]) < 1

    from matplotlib import pyplot as plt

    def squared_clustering_errors(inputs: List[Vector], k: int) -> float:
        """finds the total squared error from k-means clustering the inputs"""
        clusterer = KMeans(k)
        clusterer.train(inputs)
        means = clusterer.means
        assignments = [clusterer.classify(input) for input in inputs]

        return sum(
            squared_distance(input, means[cluster])
            for input, cluster in zip(inputs, assignments))

    # now plot from 1 up to len(inputs) clusters

    ks = range(1, len(inputs) + 1)
    errors = [squared_clustering_errors(inputs, k) for k in ks]

    plt.plot(ks, errors)
    plt.xticks(ks)
    plt.xlabel("k")
    plt.ylabel("total squared error")
    plt.title("Total Error vs. # of Clusters")
    # plt.show()

    plt.savefig('im/total_error_vs_num_clusters')
    plt.gca().clear()

    image_path = r"girl_with_book.jpg"  # wherever your image is
    import matplotlib.image as mpimg
    img = mpimg.imread(image_path) / 256  # rescale to between 0 and 1

    # .tolist() converts a numpy array to a Python list
    pixels = [pixel.tolist() for row in img for pixel in row]

    clusterer = KMeans(5)
    clusterer.train(pixels)  # this might take a while

    def recolor(pixel: Vector) -> Vector:
        cluster = clusterer.classify(pixel)  # index of the closest cluster
        return clusterer.means[cluster]  # mean of the closest cluster

    new_img = [
        [recolor(pixel) for pixel in row]  # recolor this row of pixels
        for row in img
    ]  # for each row in the image

    plt.close()

    plt.imshow(new_img)
    plt.axis('off')
    # plt.show()

    plt.savefig('im/recolored_girl_with_book.jpg')
    plt.gca().clear()

    base_cluster = bottom_up_cluster(inputs)

    three_clusters = [
        get_values(cluster) for cluster in generate_clusters(base_cluster, 3)
    ]

    # sort smallest to largest
    tc = sorted(three_clusters, key=len)
    assert len(tc) == 3
    assert [len(c) for c in tc] == [2, 4, 14]
    assert sorted(tc[0]) == [[11, 15], [13, 13]]

    plt.close()

    for i, cluster, marker, color in zip([1, 2, 3], three_clusters,
                                         ['D', 'o', '*'], ['r', 'g', 'b']):
        xs, ys = zip(*cluster)  # magic unzipping trick
        plt.scatter(xs, ys, color=color, marker=marker)

        # put a number at the mean of the cluster
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')

    plt.title("User Locations -- 3 Bottom-Up Clusters, Min")
    plt.xlabel("blocks east of city center")
    plt.ylabel("blocks north of city center")
    # plt.show()

    plt.savefig('im/bottom_up_clusters_min.png')
    plt.gca().clear()
    plt.close()

    base_cluster_max = bottom_up_cluster(inputs, max)
    three_clusters_max = [
        get_values(cluster)
        for cluster in generate_clusters(base_cluster_max, 3)
    ]

    for i, cluster, marker, color in zip([1, 2, 3], three_clusters_max,
                                         ['D', 'o', '*'], ['r', 'g', 'b']):
        xs, ys = zip(*cluster)  # magic unzipping trick
        plt.scatter(xs, ys, color=color, marker=marker)

        # put a number at the mean of the cluster
        x, y = vector_mean(cluster)
        plt.plot(x, y, marker='$' + str(i) + '$', color='black')

    plt.title("User Locations -- 3 Bottom-Up Clusters, Max")
    plt.xlabel("blocks east of city center")
    plt.ylabel("blocks north of city center")
    plt.savefig('im/bottom_up_clusters_max.png')
    plt.gca().clear()
def main():
    xs = range(-10, 11)
    actuals = [derivative(x) for x in xs]
    estimates = [difference_quotient(square, x, h=0.001) for x in xs]
    
    # plot to show they're basically the same
    import matplotlib.pyplot as plt
    plt.title("Actual Derivatives vs. Estimates")
    plt.plot(xs, actuals, 'rx', label='Actual')       # red  x
    plt.plot(xs, estimates, 'b+', label='Estimate')   # blue +
    plt.legend(loc=9)
    # plt.show()
    
    
    plt.close()
    
    def partial_difference_quotient(f: Callable[[Vector], float],
                                    v: Vector,
                                    i: int,
                                    h: float) -> float:
        """Returns the i-th partial difference quotient of f at v"""
        w = [v_j + (h if j == i else 0)    # add h to just the ith element of v
             for j, v_j in enumerate(v)]
    
        return (f(w) - f(v)) / h
    
    
    # "Using the Gradient" example
    
    # pick a random starting point
    v = [random.uniform(-10, 10) for i in range(3)]
    
    for epoch in range(1000):
        grad = sum_of_squares_gradient(v)    # compute the gradient at v
        v = gradient_step(v, grad, -0.01)    # take a negative gradient step
        print(epoch, v)
    
    assert distance(v, [0, 0, 0]) < 0.001    # v should be close to 0
    
    
    # First "Using Gradient Descent to Fit Models" example
    
    from scratch.linear_algebra import vector_mean
    
    # Start with random values for slope and intercept.
    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]
    
    learning_rate = 0.001
    
    for epoch in range(5000):
        # Compute the mean of the gradients
        grad = vector_mean([linear_gradient(x, y, theta) for x, y in inputs])
        # Take a step in that direction
        theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)
    
    slope, intercept = theta
    assert 19.9 < slope < 20.1,   "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"
    
    
    # Minibatch gradient descent example
    
    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]
    
    for epoch in range(1000):
        for batch in minibatches(inputs, batch_size=20):
            grad = vector_mean([linear_gradient(x, y, theta) for x, y in batch])
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)
    
    slope, intercept = theta
    assert 19.9 < slope < 20.1,   "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"
    
    
    # Stochastic gradient descent example
    
    theta = [random.uniform(-1, 1), random.uniform(-1, 1)]
    
    for epoch in range(100):
        for x, y in inputs:
            grad = linear_gradient(x, y, theta)
            theta = gradient_step(theta, grad, -learning_rate)
        print(epoch, theta)
    
    slope, intercept = theta
    assert 19.9 < slope < 20.1,   "slope should be about 20"
    assert 4.9 < intercept < 5.1, "intercept should be about 5"