99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python中的線性代數(shù)運算
Python中的線性代數(shù)運算
2017-05-09
收藏

Python中的線性代數(shù)運算

這里,為了熟悉Python語言的特性,我們采用一種最原始的方式去定義線性代數(shù)運算的相關(guān)函數(shù)。如果是真實應(yīng)用場景,則直接使用NumPy的函數(shù)即可。

1.向量
創(chuàng)建一個向量
我們可以把Python中的向量理解為有限維空間中的點。
height_weight_age = [70,170,40]
grades = [95,80,75,62]
向量運算
#### 加法定義——兩個向量
def vector_add(v,w):
    """add coresponding elements"""
    return [v_i + w_i
                for v_i,w_i in zip(v,w)]
#### 減法定義
def vector_substract(v,w):
    """substracts coresponding elements"""
    return [v_i - w_i
                   for v_i,w_i in zip(v,w)]
#### 向量加法——多個向量(list of vectors)
####### method 1:
def vector_sum(vectors):
    """sums of all coresponding elements"""
    result = vectors[0]
    for vector in vectors[1:]:
        result = vector_add(result,vector)
    return result
######## mothod 2:
def vector_sum(vecotrs):
    return reduce(vector_add,vectors)
######## mothod 3:
from functools import partial
vector_sum = partial(reduce,vector_add)
### 向量的數(shù)乘運算
def scalar_multiply(c,v):
    """c is a number,v is a vector"""
    return [c * v_i for v_i in v]
### 向量的均值運算
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(vecotrs)
    return scalar_multiply(1/n,vector_sum())
### 向量的點乘
def dot(v,w):
    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_2*v_2+...+v_n*v_n"""
    return dot(v,v)
### 向量的模
import math
def magnitude(v):
    return math.sqrt(sum_of_squares(v))
### 向量的距離
##### method 1:
def squared_distance(v,w):
    """"""
    return sum_of_squares(vector_substract(v,w))
##### method 2:
def distance(v,w):
    return magnitude(vector_substract(v,w))
##### method 3:
def distance(v,w):
    return math.sqrt(squared_distance(v,w))
2.矩陣
矩陣是一個二維的數(shù)字集合。我們可以通過列表的列表來表達一個矩陣,這樣,內(nèi)層列表是等長的,并且每個內(nèi)層列表表達矩陣的一行。
### 定義一個向量
A = [[1,2,3],
     [4,5,6]]

B = [[1,2],
     [3,4],
    [7,8]]

### 獲得矩陣的行數(shù)和列數(shù)
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]  # j-th element of row A_i
                   for A_i in A]  # for each row in A

### 定制特殊矩陣生成函數(shù):如單位矩陣
def make_matrix(num_rows,num_cols,entry_fn):
    """return a 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):
    return 1 if i==j else 0
make_matrix(5,5,is_diagonal)
[[1, 0, 0, 0, 0],
 [0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 0, 1, 0],
 [0, 0, 0, 0, 1]]

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }