1. 程式人生 > >『Python』常用函數實踐筆記

『Python』常用函數實踐筆記

sta lca extend article next() idea array cep 數組元素

庫安裝:

1).pip & conda

2).在win10下手動安裝python庫的方法:

『python』計算機視覺_OpenCV3庫安裝

原生:

list.append():添加元素到list末尾

list.extend():使用一個list擴展另一個list

字典列表化:字典是有順序的,而且list字典等於list字典的key

dict = {‘c‘:1,‘b‘:2,‘a‘:3}

list(dict)
# Out[13]: 
# [‘c‘, ‘b‘, ‘a‘]

list(dict.keys())
# Out[12]: 
# [‘c‘, ‘b‘, ‘a‘]

random庫:

random.randrange ([start,] stop [,step])

# 輸出 100 <= number < 1000 間的偶數
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)

# 輸出 100 <= number < 1000 間的3的倍數加100的結果
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)

os庫:

os.path.exists(sub_dir_path):判斷是否存在文件夾

os.makedirs(sub_dir_path):創建文件夾

os.walk(’路徑‘):遞歸遍歷,返回所有子文件

[x[0] for x in os.walk(‘./‘)]
# Out[1]:
# [‘./‘, ‘./幫師兄修圖‘, ‘./.idea‘, ‘./.idea/inspectionProfiles‘, ‘./Machine_Learning‘, ‘./Machinening # /1400OS_05_Codes‘, ‘./Machine_Learning/1400OS_01_Codes‘, # ...... # ‘./TensorFlow/遷移學習/bottleneck‘, ‘./TensorFlow/遷移學習/inception_dec_2015‘]

os.path.join():合並各個字符串,添加’/‘為路徑

os.path.join(‘dir1‘,‘dir2‘)
# Out[1]: 
# ‘dir1/dir2‘

os.path.basename(file_name):剔除路徑,保存文件名

os.path.basename(‘/123/123‘)
Out[1]: 
‘123‘

glob庫:

glob.glob(file):返回匹配的文件

glob.glob(./flower_photos/tulips/*.jpg)
# Out[1]:
# [‘./flower_photos/tulips/19425920580_cdc8f49aed_n.jpg‘, ‘./flower_photos/tulips # /17094167287_865840060d_n.jpg‘, ‘./flower_photos/tulips/8712282563_3819afb7bc.jpg‘, # ‘./flower_photos/tulips/7166606598_5d2cd307c3.jpg‘‘]

matplotlib.pyplot

中文詳細教程

numpy庫:

np.bincount()

numpy.bincount詳解

np.maximum(X, Y, out=None):

    • X 與 Y 逐位比較取其大者;
    • 最少接收兩個參數

np.squeeze():剔除長度為一的軸

np.squeeze(np.array([[1,2,3]]))
# Out[17]: 
# array([1, 2, 3])
np.squeeze(np.array([[1],[2],[3]]))
# Out[18]: 
# array([1, 2, 3])

np.nditer():numpy的強大叠代器

默認情況下,nditer將視待叠代遍歷的數組為只讀對象(read-only),為了在遍歷數組的同時,實現對數組元素值得修改,必須指定op_flags=[‘readwrite‘]模式:

np.nditer(a, op_flags=[‘readwrite‘])

基本叠代參數flag=[‘f_index‘/‘mulit_index‘],可輸出自身坐標it.index/it.multi_index:

a = np.arange(6).reshape(2,3)

‘‘‘叠代方式‘‘‘

# 單維叠代
it = np.nditer(a, flags=[‘f_index‘])
while not it.finished:
    print("%d <%s>" % (it[0], it.index))
    it.iternext()
    
#0 <0>
#1 <2>
#2 <4>
#3 <1>
#4 <3>
#5 <5>


# 多維叠代
it = np.nditer(a, flags=[‘multi_index‘])
while not it.finished:
    print("%d <%s>" % (it[0], it.multi_index))
    it.iternext()
    
#0 <(0, 0)>
#1 <(0, 1)>
#2 <(0, 2)>
#3 <(1, 0)>
#4 <(1, 1)>
#5 <(1, 2)>

改變叠代的順序:

# 列順序叠代
it = np.nditer(a, flags=[‘f_index‘], order=‘F‘)
while not it.finished:
    print("%d <%s>" % (it[0], it.index), end=‘ | ‘)
    it.iternext()
    
# 0 <0> | 3 <1> | 1 <2> | 4 <3> | 2 <4> | 5 <5> | 


# 行順序叠代
it = np.nditer(a, flags=[‘f_index‘], order=‘C‘)
while not it.finished:
    print("%d <%s>" % (it[0], it.index), end=‘ | ‘)
    it.iternext()
    
# 0 <0> | 1 <2> | 2 <4> | 3 <1> | 4 <3> | 5 <5> | 

深入理解numpy廣播機制:

numpy計算函數返回默認是一維行向量:

import numpy as np

a = [[1,1,1],
    [2,2,2],
    [3,3,3]]
b = (np.sum(a,axis=1))
c = (np.sum(a,axis=0))
print(b,‘\n‘,c)

# [3 6 9] 
# [6 6 6]

所以廣播之實際是高維對一維行向量的廣播:

除法廣播:

b = a/(np.sum(a,axis=1))
c = a/(np.sum(a,axis=0))
print(b,‘\n‘,c)

# [[ 0.33333333  0.16666667  0.11111111]
#  [ 0.66666667  0.33333333  0.22222222]
#  [ 1.          0.5         0.33333333]] 
# [[ 0.16666667  0.16666667  0.16666667]
#  [ 0.33333333  0.33333333  0.33333333]
#  [ 0.5         0.5         0.5       ]]

向量乘法,加法可以類比:

np.array([1,2,3])*np.array([1,1,1])
# [1 2 3]

np.array([1,2,3])*np.array([1])
# [1 2 3]

np.array([1,2,3])*np.array([1,1])
# 報錯

np.array([[1],[1],[1]])*np.array([1,2,3])
# [[1 2 3]
#  [1 2 3]
#  [1 2 3]]

『Python』常用函數實踐筆記