[Python] 01 - Number and Matrix

故事背景


1、大綱

以下,chapter4 是個概覽,以後纔是具體講解。html

 

2、 編譯過程

Ref: http://www.dsf.unica.it/~fiore/LearningPython.pdfpython


 

 



 

 

 

 

 

 

3、 四個概念

145/1594linux

Python programs can be decomposed into modules, statements, expressions, and objects, as follows: express

1. Programs are composed of modules. 數組

2. Modules contain statements. app

3. Statements contain expressions. dom

4. Expressions create and process objects.ide

 

4、變量類型

Python’s Core Data Types函數


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5、枚舉類

From: 使用枚舉類post

使用默認值

from enum import Enum
# 定義 Month
= Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
# 遍歷
for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)

 

自定義值

@unique裝飾器能夠幫助咱們檢查保證沒有重複值,這裏使用了「類」。

from enum import Enum, unique

@unique
class Weekday(Enum):
    Sun = 0 # Sun的value被設定爲0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6

 

 

 

 

讓咱們開始


1、 表達形式

 

   

2、大數

超長顯示 

總之,問題不大。

 

Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux

Type "help", "copyright", "credits" or "license" for more information.

 

>>> len(str(2**1000))

302
length

>>> 2**1000

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
較長

>>> 2**10000 # Are you serious? 

19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376
超長

 

 

3、小數

Question:一個比較麻煩的問題,如何解決?

 

print() 函數

數字顯示問題,版本變高自動改善,或者經過print做爲替代方案。




 

print等價於str,表示:以讀者但願的形式表達出來。

In [92]: decimal.Decimal(str(1/3))
Out[92]: Decimal('0.3333333333333333')

In [93]: decimal.Decimal(1/3)
Out[93]: Decimal('0.333333333333333314829616256247390992939472198486328125')

 

decimal 方法

>>> 1 / 3 # Floating-point (add a .0 in Python 2.X)
0.3333333333333333
>>> (2/3) + (1/2)
1.1666666666666665

>>> import decimal # Decimals: fixed precision >>> d = decimal.Decimal('3.141') >>> d + 1 Decimal('4.141')
>>> decimal.getcontext().prec = 2                    // 精度設置 >>> decimal.Decimal('1.00') / decimal.Decimal('3.00')        // 小數計算 Decimal('0.33')

 

fractions 方法

>>> from fractions import Fraction # Fractions: numerator+denominator
>>> f = Fraction(2, 3)
>>> f + 1
Fraction(5, 3)
>>> f + Fraction(1, 2)
Fraction(7, 6)
In [2]: (2.55).as_integer_ratio()
Out[2]: (2871044762448691, 1125899906842624) # 精算?哈哈~

 

 

4、專業數學計算

四個經常使用專業庫

更是有專業的庫提供方案 純數計算,可能有必要單獨篇章總結。

import math
import random
import statistics
import numpy as np

  

高數課程

The math module gives access to the underlying C library functions for floating point math:

>>> import math >>> math.cos(math.pi / 4) 0.70710678118654757 >>> math.log(1024, 2) 10.0 

 

統計課程

The random module provides tools for making random selections:

>>> import random >>> random.choice(['apple', 'pear', 'banana']) 'apple' >>> random.sample(range(100), 10) # sampling without replacement [30, 83, 16, 4, 8, 81, 41, 50, 18, 33] >>> random.random() # random float 0.17970987693706186 >>> random.randrange(6) # random integer chosen from range(6) 4 

The statistics module calculates basic statistical properties (the mean, median, variance, etc.) of numeric data:

>>> import statistics >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5] >>> statistics.mean(data) 1.6071428571428572 >>> statistics.median(data) 1.25 >>> statistics.variance(data) 1.3720238095238095

 

 

 

numpy - Quickstart tutorial


Ref: https://docs.scipy.org/doc/numpy/user/quickstart.html

 

1、矩陣 (Matrix) 

初始化

NumPy’s array class is called ndarray. 若干例子:

# 初始化

>>> import numpy as np

# (1) 描述型初始化
>>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])

# (2) 直接賦值初始化
a = np.array([2,3,4])            # 一維
b = np.array([(1.5,2,3), (4,5,6)]) # 多維

# (3) 制定初始化
np.zeros( (3,4) )               # 多維
np.ones( (2,3,4), dtype=np.int16 ) # 多維
np.empty( (2,3) ) # 多維
np.arange( 10, 30, 5 ) # 一維:均勻點-間隔法
np.linspace( 0, 2, 9 ) # 一維:均勻點-個數法
See also
numpy.random.randnumpy.random.randn
fromfunctionfromfile 

------------------------------------
# 形狀和維度
>>>a.shape (3, 5) >>>a.ndim 2

>>> a.size 15

------------------------------------
# 內部成員屬性
>>> type(a)
<type 'numpy.ndarray'>

>>> a.dtype.name
'int64'

>>> a.itemsize
8

 

Basic Operations

知足基本的矩陣性質,略。

>>> import numpy as np
>>> a = np.arange(15).reshape(3,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> b = np.arange(15).reshape(3,5)
>>> a*b
array([[  0,   1,   4,   9,  16],
       [ 25,  36,  49,  64,  81],
       [100, 121, 144, 169, 196]])

 

Universal Functions

Available: https://numpy.org/devdocs/user/quickstart.html

allanyapply_along_axis 矩陣對比,元素全同樣 只要有一個同樣 lamdba 處理某維元素
argmaxargminargsortaveragebincount 第幾維的最大值 第幾維的最小值 某一維排序 某一維均值  基數排序
ceilclipconjcorrcoefcov 取右 "天花板" 設置「左右天花板」 求共軛 協方差[-1, 1] 協方差
crosscumprodcumsumdiffdot 向量積 累積乘 累計加 相鄰元素差值 點積
floorinnerinvlexsortmax 取左 "地板」 相似「點積」 逆矩陣 向量排序 向量中的最大值
maximummeanmedianminminimum 向量中元素與一個值的最大值 mean
median 向量中的最小值 向量中元素與一個值的最小值
nonzeroouterprodreround 非零元素的全部座標 向量1的各元素與向量2相乘  元素的乘積 正則  四捨五入
sortstdsumtracetranspose 按照某一個維度排序 計算全局標準差 某維度求和   對角線元素的和  矩陣轉置
varvdotvectorizewhere  方差 點積  將函數向量化: lamdba 符合某一條件的下標函數  

 

Here is a list of some useful NumPy functions and methods names ordered in categories. See Routines for the full list.

Array Creation arangearraycopyemptyempty_likeeyefromfilefromfunctionidentitylinspacelogspacemgridogridonesones_likerzeroszeros_like
Conversions ndarray.astypeatleast_1datleast_2datleast_3dmat
Manipulations array_splitcolumn_stackconcatenatediagonaldsplitdstackhsplithstackndarray.itemnewaxisravelrepeatreshaperesizesqueezeswapaxestaketransposevsplit,vstack
Questions allanynonzerowhere
Ordering argmaxargminargsortmaxminptpsearchsortedsort
Operations choosecompresscumprodcumsuminnerndarray.fillimagprodputputmaskrealsum
Basic Statistics covmeanstdvar
Basic Linear Algebra crossdotouterlinalg.svdvdot


2、矩陣操做

矩陣下標

index 表示範圍

#coding:utf-8
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)      #(2,3)
print(a[:][0])      #這樣寫,不管先後,只遍歷第一行
print(a[0][:])
print(a[:][1])
print(a[1][:])
############################
print(a[:,0])      #這樣寫纔是遍歷第一列,先後有區別
print(a[0,:])
print(a[:,1])
print(a[1,:])
print(a[:,2])
############################
print(a[0:3][0])    #[:]範圍明明是3個,拆開寫就不對,要符合實際狀況
print(a[0][0])
print(a[1][0])

 

下標表示範圍內的「間隔」

>>> a[:6:2] = -1000    # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])

 

矩陣遍歷

傳統遍歷 - 規則數組 

list2d = [[1,2,3],[4,5,6]]
sum = 0
for i in range(len(list2d)):
    for j in range(len(list2d[0])):
        sum += list2d[i][j]

 

句柄遍歷 - 不規則數組

list2d = [[1,2,3],[4,5]]
sum = 0
for i in list2d:
    for j in i:
        sum += j

 

矩陣取整

取左地板值

# np.floor取整
>>> a = np.floor(10*np.random.random((3,4))) >>> a array([[ 2., 8., 0., 6.], [ 4., 5., 1., 1.], [ 8., 9., 3., 6.]]) >>> a.shape (3, 4)

 

僅保留整數位

math.trunc(-2.5) 更人性化,return -2; 而不是floor的-3。

 

四捨五入

round(2.45):四捨五入

 

 

3、矩陣形變

扁平化

徹底扁平

>>> a.ravel()  # returns the array, flattened
array([ 2.,  8.,  0.,  6.,  4.,  5.,  1.,  1.,  8.,  9.,  3.,  6.])

 

自定義扁平

>>> a.reshape(6,2)  # returns the array with a modified shape
array([[ 2.,  8.],
       [ 0.,  6.],
       [ 4.,  5.],
       [ 1.,  1.],
       [ 8.,  9.],
       [ 3.,  6.]])

-----------------------------------------------------------------------

>>> a.resize((2,6))  # this method modifies the array itself!
>>> a
array([[ 2.,  8.,  0.,  6.,  4.,  5.],
       [ 1.,  1.,  8.,  9.,  3.,  6.]])

 

轉置

>>> a.T  # returns the array, transposed
array([[ 2.,  4.,  8.],
       [ 8.,  5.,  9.],
       [ 0.,  1.,  3.],
       [ 6.,  1.,  6.]])
>>> a.T.shape
(4, 3)
>>> a.shape
(3, 4)

    

堆疊

總體對接

Stacking together different arrays.

>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8.,  8.],
       [ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1.,  8.],
       [ 0., 4.]])

>>> np.vstack((a,b))  # 第一維度的粘結,更深 array([[ 8., 8.], [ 0., 0.], [ 1., 8.], [ 0., 4.]])
>>> np.hstack((a,b))  # 第二維度的對接,更寬 array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]])

 

各取出一個配對

column_stack & row_stack.

>>> np.hstack([np.array([1, 2, 3]), np.array([4, 5, 6])])
array([1, 2, 3, 4, 5, 6])

>>> np.column_stack([np.array([1, 2, 3]), np.array([4, 5, 6])])  # 按照列的順序,各自拿出一個,組合在一塊兒
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> np.vstack([np.array([1, 2, 3]), np.array([4, 5, 6])])
array([[1, 2, 3],
       [4, 5, 6]])

>>> np.row_stack([np.array([1, 2, 3]), np.array([4, 5, 6])])    # 按照行的順序,各自拿出一個,組合在一塊兒
array([[1, 2, 3],
       [4, 5, 6]])

  

元素自增長一維度

>>> from numpy import newaxis
>>> a = np.array([4.,2.]) >>> b = np.array([2.,8.])
>>> a[: ,newaxis] # This allows to have a 2D columns vector array([[ 4.], [ 2.]])   # 可見,自增長了維度的效果

>>> np.column_stack((a[:,newaxis],b[:,newaxis]))  // 兩個一維數組的合併和兩個數字合併,本質上是同樣的:由於一個數字其實就是「默認一維」 array([[ 4., 2.], [ 2., 8.]])

>>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different array([[ 4.], [ 2.], [ 2.], [ 8.]])

 

拆分

注意:能夠經過定義 「分割點」 來分割高維矩陣。

Splitting one array into several smaller ones.

>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9.,  5.,  6.,  3.,  6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],
       [ 1.,  4.,  9.,  2.,  2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])

>>> np.hsplit(a,3)   # Split a into 3
[array([[ 9.,  5.,  6.,  3.], [ 1.,  4.,  9.,  2.]]), 
array([[ 6., 8., 0., 7.], [ 2., 1., 0., 6.]]),
array([[ 9., 7., 2., 7.], [ 2., 2., 4., 0.]])]
>>> np.hsplit(a, (3,4)) # Split a after the third and the fourth column 看樣子像是"分割點" [array([[ 9., 5., 6.], [ 1., 4., 9.]]), array([[ 3.], [ 2.]] ), array([[ 6., 8., 0., 7., 9., 7., 2., 7.], [ 2., 1., 0., 6., 2., 2., 4., 0.]])] >>> np.vsplit(a,(3,4)) [array([[ 2., 9., 0., 4., 3., 2., 8., 0., 6., 2., 0., 4.], [ 8., 0., 5., 3., 4., 9., 5., 0., 3., 4., 4., 5.]]), array([], shape=(0, 12), dtype=float64), array([], shape=(0, 12), dtype=float64)]

 

np.split更靈活,任意分割!

>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10]) [array([ 0., 1., 2.]), array([ 3., 4.]), array([ 5.]), array([ 6., 7.]), array([], dtype=float64)]

其餘:numpy.array_split 【彷佛用處不大】

 

 

4、矩陣拷貝

引用,非拷貝

No Copy at All, 引用

>>> b = a            # no new object is created
>>> id(a)            # id is a unique identifier of an object

 

映射關係 view

雖 id 不一樣,但base指向對方。

view矩陣雖然形狀變了,但每一個元素仍是跟原來的「有映射關係」,改變某值,對應的"原位置的值"改變。

>>> c = a.view()
>>> c is a
False
>>> c.base is a                        # c is a view of the data owned by a <---- 理解這句話!
True
>>> c.flags.owndata False
>>> c.shape = 2,6 # a's shape doesn't change >>> a.shape (3, 4) >>> c[0,4] = 1234 # a's data changes >>> a array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])

 

Deep Copy , 深拷貝

>>> d = a.copy()                          # a new array object with new data is created
>>> d is a
False
>>> d.base is a                           # d doesn't share anything with a
False
>>> d[0,0] = 9999
>>> a
array([[   0,   10,   10,    3],
       [1234,   10,   10,    7],
       [   8,   10,   10,   11]])

 

 

5、統計採樣

Ref: Numpy隨機抽樣

 

正態分佈

#均勻分佈
np.random.rand(2,5)  // shape

#正態分佈
np.random.randn(1,10) // shape

#均勻分佈 - 半開半閉區間 - 整數 【nice】
np.random.randint(2, 5, 10)       // interval
np.random.randint(2, 5, (6,6))    // interval

#均勻分佈 - 閉區間 - 整數 【nice】
np.random.random_integers(2, 5, 10)

#均勻分佈 - 半開半閉區間 - 百分比
np.random.random_sample((6,6))

#均勻分佈 - 閉區間 - 整數
np.random.choice(10, 5)

  

其餘分佈

random.beta(a,b,size):從 Beta 分佈中生成隨機數。 random.binomial(n, p, size):從二項分佈中生成隨機數。 random.chisquare(df,size):從卡方分佈中生成隨機數。 random.dirichlet(alpha,size):從 Dirichlet 分佈中生成隨機數。 random.exponential(scale,size):從指數分佈中生成隨機數。 random.f(dfnum,dfden,size):從 F 分佈中生成隨機數。 random.gamma(shape,scale,size):從 Gamma 分佈中生成隨機數。 random.geometric(p,size):從幾何分佈中生成隨機數。 random.gumbel(loc,scale,size):從 Gumbel 分佈中生成隨機數。 random.hypergeometric(ngood, nbad, nsample, size):從超幾何分佈中生成隨機數。 random.laplace(loc,scale,size):從拉普拉斯雙指數分佈中生成隨機數。 random.logistic(loc,scale,size):從邏輯分佈中生成隨機數。 random.lognormal(mean,sigma,size):從對數正態分佈中生成隨機數。 random.logseries(p,size):從對數系列分佈中生成隨機數。 random.multinomial(n,pvals,size):從多項分佈中生成隨機數。 random.multivariate_normal(mean, cov, size):從多變量正態分佈繪製隨機樣本。 random.negative_binomial(n, p, size):從負二項分佈中生成隨機數。 random.noncentral_chisquare(df,nonc,size):從非中心卡方分佈中生成隨機數。 random.noncentral_f(dfnum, dfden, nonc, size):從非中心 F 分佈中抽取樣本。 random.normal(loc,scale,size):從正態分佈繪製隨機樣本。 random.pareto(a,size):從具備指定形狀的 Pareto II 或 Lomax 分佈中生成隨機數。 random.poisson(lam,size):從泊松分佈中生成隨機數。 random.power(a,size):從具備正指數 a-1 的功率分佈中在 0,1 中生成隨機數。 random.rayleigh(scale,size):從瑞利分佈中生成隨機數。 random.standard_cauchy(size):從標準 Cauchy 分佈中生成隨機數。 random.standard_exponential(size):從標準指數分佈中生成隨機數。 random.standard_gamma(shape,size):從標準 Gamma 分佈中生成隨機數。 random.standard_normal(size):從標準正態分佈中生成隨機數。 random.standard_t(df,size):從具備 df 自由度的標準學生 t 分佈中生成隨機數。 random.triangular(left,mode,right,size):從三角分佈中生成隨機數。 random.uniform(low,high,size):從均勻分佈中生成隨機數。 random.vonmises(mu,kappa,size):從 von Mises 分佈中生成隨機數。 random.wald(mean,scale,size):從 Wald 或反高斯分佈中生成隨機數。 random.weibull(a,size):從威布爾分佈中生成隨機數。 random.zipf(a,size):從 Zipf 分佈中生成隨機數。

再結合取整便可:np.floor(<list>) 

   

End.

相關文章
相關標籤/搜索