話說python 還真是方便。。 用c要寫n長的** python百行左右就解決了
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#資料結構------------處理稀疏陣列
import copy
def subsparse(dict_sparse_a, dict_sparse_b):
'''兩個字典相減'''
dict_sparse_sub = dict()
for i in dict_sparse_a:
if i in dict_sparse_b:
dict_sparse_sub[i] = dict_sparse_a[i] - dict_sparse_b[i]
else:
dict_sparse_sub[i] = dict_sparse_a[i]
for j in dict_sparse_b:
if j not in dict_sparse_sub:
dict_sparse_sub[j] = 0 - dict_sparse_b[j]
dict_sparse_sub2 = copy.deepcopy(dict_sparse_sub)
for i in dict_sparse_sub:
if dict_sparse_sub[i] == 0:
del dict_sparse_sub2[i]
return dict_sparse_sub2
def addsparse(dict_sparse_a, dict_sparse_b):
'''兩個字典相加'''
dict_sparse_add = dict()
dict_sparse_add2 = dict()
for i in dict_sparse_a:
if i in dict_sparse_b:
dict_sparse_add[i] = dict_sparse_a[i] + dict_sparse_b[i]
else:
dict_sparse_add[i] = dict_sparse_a[i]
for j in dict_sparse_b:
if j not in dict_sparse_add:
dict_sparse_add[j] = dict_sparse_b[j]
for i in dict_sparse_add:
if dict_sparse_add[i] != 0:
dict_sparse_add2[i] = dict_sparse_add[i]
return dict_sparse_add2
def createarray(list_sparse, allrow = 3, allcol = 4):
'''列表模擬陣列'''
row = 0
while row < allrow:
list_string = raw_input("enter one array items spaced by comma:/n").strip().split(',')
list_array = list()
for i in list_string:
row += 1
def createsparse(list_sparse):
'''將模擬出來的列表轉為為 字典 字典的鍵為 陣列下標'''
dict_sparse = dict()
for i in range(0, len(list_sparse)):
for j in range(0, len(list_sparse[i])):
if 0 != list_sparse[i][j]:
dict_sparse[(i, j)] = list_sparse[i][j]
return dict_sparse
def main():
print 'ok'
list_sparse_a = list()
list_sparse_b = list()
print 'array a'
createarray(list_sparse_a)
dict_sparse_a = createsparse(list_sparse_a)
print "sparse array a"
for i in sorted(dict_sparse_a):
print i, dict_sparse_a[i]
print 'array b'
createarray(list_sparse_b)
dict_sparse_b = createsparse(list_sparse_b)
print "sparse array b"
for i in sorted(dict_sparse_b):
print i, dict_sparse_b[i]
dict_sparse_add = addsparse(dict_sparse_a, dict_sparse_b)
print "add result"
for i in sorted(dict_sparse_add):
print i, dict_sparse_add[i]
dict_sparse_sub = subsparse(dict_sparse_a, dict_sparse_b)
print "sub result"
for i in sorted(dict_sparse_sub):
print i, dict_sparse_sub[i]
if __name__ == '__main__':
main()
python雜湊與稀疏矩陣的處理
一雜湊的原理 字典 key 對映到 value 雜湊 key 對映到 key的雜湊值 對映到 value 例如查詢 現代漢語字典 拼音 an 對映到頁碼55 對映到 安 二 自定義雜湊函式 在nlp問題中,假設你想把乙個字串轉換為乙個數值型向量,可以使用one hot encoding也就是雜湊函式...
稀疏變數的處理
經常,對於高維的資料集,降維的第一步,就是刪除那些包含相同資訊的變數,就是變數取值很多相同的或者改變了的方差很小。想刪除第一步,就是,先問為什麼?當然是模型不接受你這種的變數,好比如回歸分析,把很多值相同的變數作為自變數,對於模型的擬合效果有影響。那麼,要刪,當然先找到哪些屬於稀疏變數。nearze...
稀疏矩陣的處理
在矩陣中,若數值為0的元素數目遠遠多於非0元素的數目,並且非0元素分布沒有規律時,則稱該矩陣為稀疏矩陣 與之相反,若非0元素數目佔大多數時,則稱該矩陣為稠密矩陣 通常認為矩陣中非零元素的總數比上矩陣所有元素總數的值小於等於0.05時,則稱該矩陣為稀疏矩陣。該比值稱為這個矩陣的稠密度 列印矩陣 壓縮矩...