数据科学入门—线性代数

# -*- coding: iso-8859-15 -*-


from __future__ import division # want 3 / 2 == 1.5

import re, math, random # regexes, math functions, random numbers

import matplotlib.pyplot as plt # pyplot

from collections import defaultdict, Counter

from functools import partial


# functions for working with vectors

#


def vector_add(v, w):

    """adds two vectors componentwise"""

    return [v_i + w_i for v_i, w_i in zip(v,w)]


def vector_subtract(v, w):

    """subtracts two vectors componentwise"""

    return [v_i - w_i for v_i, w_i in zip(v,w)]


def vector_sum(vectors):

    return reduce(vector_add, vectors)


def scalar_multiply(c, v):

    return [c * v_i for v_i in v]


# this isn't right if you don't from __future__ import division

def vector_mean(vectors):

    """compute the vector whose i-th element is the mean of the

    i-th elements of the input vectors"""

    n = len(vectors)

    return scalar_multiply(1/n, vector_sum(vectors))


def dot(v, w):

    """v_1 * w_1 + ... + v_n * w_n"""

    return sum(v_i * w_i for v_i, w_i in zip(v, w))


def sum_of_squares(v):

    """v_1 * v_1 + ... + v_n * v_n"""

    return dot(v, v)


def magnitude(v):

    return math.sqrt(sum_of_squares(v))


def squared_distance(v, w):

    return sum_of_squares(vector_subtract(v, w))


def distance(v, w):

   return math.sqrt(squared_distance(v, w))


#

# functions for working with matrices

#


def shape(A):

    num_rows = len(A)

    num_cols = len(A[0]) if A else 0

    return num_rows, num_cols


def get_row(A, i):

    return A[i]

    

def get_column(A, j):

    return [A_i[j] for A_i in A]


def make_matrix(num_rows, num_cols, entry_fn):

    """returns a num_rows x num_cols matrix 

    whose (i,j)-th entry is entry_fn(i, j)"""

    return [[entry_fn(i, j) for j in range(num_cols)]

            for i in range(num_rows)]  


def is_diagonal(i, j):

    """1's on the 'diagonal', 0's everywhere else"""

    return 1 if i == j else 0


identity_matrix = make_matrix(5, 5, is_diagonal)


#          user 0  1  2  3  4  5  6  7  8  9

#

friendships = [[0, 1, 1, 0, 0, 0, 0, 0, 0, 0], # user 0

               [1, 0, 1, 1, 0, 0, 0, 0, 0, 0], # user 1

               [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], # user 2

               [0, 1, 1, 0, 1, 0, 0, 0, 0, 0], # user 3

               [0, 0, 0, 1, 0, 1, 0, 0, 0, 0], # user 4

               [0, 0, 0, 0, 1, 0, 1, 1, 0, 0], # user 5

               [0, 0, 0, 0, 0, 1, 0, 0, 1, 0], # user 6

               [0, 0, 0, 0, 0, 1, 0, 0, 1, 0], # user 7

               [0, 0, 0, 0, 0, 0, 1, 1, 0, 1], # user 8

               [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]] # user 9


#####

# DELETE DOWN

#



def matrix_add(A, B):

    if shape(A) != shape(B):

        raise ArithmeticError("cannot add matrices with different shapes")

        

    num_rows, num_cols = shape(A)

    def entry_fn(i, j): return A[i][j] + B[i][j]

        

    return make_matrix(num_rows, num_cols, entry_fn)



def make_graph_dot_product_as_vector_projection(plt):

    v = [2, 1]

    w = [math.sqrt(.25), math.sqrt(.75)]

    c = dot(v, w)

    vonw = scalar_multiply(c, w)

    o = [0,0]


    plt.arrow(0, 0, v[0], v[1],width=0.002, head_width=.1, length_includes_head=True)

    plt.annotate("v", v, xytext=[v[0] + 0.1, v[1]])

    plt.arrow(0 ,0, w[0], w[1], width=0.002, head_width=.1, length_includes_head=True)

    plt.annotate("w", w, xytext=[w[0] - 0.1, w[1]])

    plt.arrow(0, 0, vonw[0], vonw[1], length_includes_head=True)

    plt.annotate(u"(v•w)w", vonw, xytext=[vonw[0] - 0.1, vonw[1] + 0.1])

    plt.arrow(v[0], v[1], vonw[0] - v[0], vonw[1] - v[1], linestyle='dotted', length_includes_head=True)

    plt.scatter(*zip(v,w,o),marker='.')

    plt.axis('equal')

    plt.show()


if __name__ == "__main__":

    print 'start'

    make_graph_dot_product_as_vector_projection(plt)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值