##numpy函數庫中一些常用函數的記錄
最近才開始接觸Python,python中為我們提供了大量的庫,不太熟悉,因此在《機器學習實戰》的學習中,對遇到的一些函數的用法進行記錄。
(1)mat( )
numpy函數庫中存在兩種不同的數據類型(矩陣matrix和數組array),都可以用於處理行列表示的數字元素。雖然他們看起來很相似,但是在這兩個數據類型上執行相同的數學運算可以得到不同的結果,其中numpy函數庫中matrix與MATLAB中matrices等價。
調用mat( )函數可以將數組轉化為矩陣。例如
random.rand(3,3)#構造一個3*3的隨機數組
- 1
- 1

mat(random.rand(3,3))#將3*3的隨機數組轉化為一個3*3的矩陣
- 1
- 1

(2)shape( )
shape函數是numpy.core.fromnumeric中的函數,它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度。它的輸入參數可以使一個整數表示維度,也可以是一個矩陣。例子如下:
matDemo=mat(random.rand(3,5)) matDemo.shape[0]#獲取矩陣第一維的長度,輸入參數是一個整數表示維度 matDemo.shape[1]#獲取矩陣第二維的長度, shape(matDemo) #獲取矩陣的各個維度的大小,輸入參數是一個矩陣 shape(matDemo)[i]#這樣寫也可以獲取矩陣的第i個維度的大小
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5


(3)random.uniform( )函數
功能:uniform(x,y) 方法將隨機生成下一個實數,它在[x,y]范圍內。
參數說明:
x – 隨機數的最小值界。
y – 隨機數的最大值界。
返回值:返回一個浮點數。
用法如下:
from numpy import * #或者直接 import random也可以 random.uniform(x,y)
- 1
- 2
- 1
- 2

注意:uniform()是不能直接訪問的,需要導入 random 模塊,然后通過 random 靜態對象調用該方法。
(4)means( )方法
means( )方法為求平均值的方法
numpy.mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False)
axis=None時計算數組中的所有值的平均值
axis=0時以列為單位計算數組的每列的所有值的平均值
axis=1時計算數組的每行為單位的所有事的平均值
dtype為指定數組中元素的類型,默認為float64
numpy.mean
numpy.mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False)
Compute the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.
Parameters :
a : array_like
Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.
axis : int, optional
Axis along which the means are computed. The default is to compute the mean of the flattened array.
dtype : data-type, optional
Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.
out : ndarray, optional
Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See doc.ufuncs for details.
skipna : bool, optional
If this is set to True, skips any NA values during calculation instead of propagating them.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
Returns :
m : ndarray, see dtype parameter above
If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.
See also
average
Weighted average
Notes
The arithmetic mean is the sum of the elements along the axis divided by the number of elements.
Note that for floating-point input, the mean is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.
例子如下: 
(5)、tile( )方法
tile函數位於python模塊 numpy.lib.shape_base中,他的功能是重復某個數組。比如tile(A,reps),功能是將數組A重復n次,構成一個新的數組,
在tile(A,reps)中
A的類型眾多,幾乎所有類型都可以:array, list, tuple, dict, matrix以及基本數據類型int, string, float以及bool類型。
reps的類型也很多,可以是tuple,list, dict, array, int,bool.但不可以是float, string, matrix類型。
先來看一些例子,然后我們就可以清楚的感受到這個函數到底是干什么的了。
第一類情況:reps為一個整數,A為一個int、tuple、dict等

第二類情況:reps為一個簡單的list,A為一個int、tuple、dict等

總結
從上面可以看出,假定A的維度為d,reps的長度為len
A=[[1,2],[2,3]]的維度為2
reps=[2,3]長度為2
reps=2 長度為1
(1)當d小於len時,tile(A,reps)就是將A中所有元素作為單元,變成一個reps.n1*reps.n2 *…. *reps.nd的新數組,其中reps.n2為reps中的第2個數
為便於說明,舉一個例子
假設A為一個二維數組
a=array([[1,2],[2,3]]),
reps為一個tuple:reps=[2,3];
tile(A,reps)的含義就是將A中所有元素作為單元,變成一個2*3的新數組。
(2)當d>=len時,將reps長度補足為d,即在reps前面加上d-len個1。tile(A,reps)就是將A中所有元素作為單元,變成一個1*1…reps.n1 reps.n2 …reps.nd
例如
a=array([[1,2],[2,3]])
tile(a,2)與tile(a,(1,2))是一樣的。
(6)、argsort( )方法
python中的排序問題
numpy包中的argsort函數是對一個數組進行升序排列。
a=[0,1,3,2]
s=argsort(a)
截圖如下: 
結果返回的就是a中所有元素排序后各個元素在a中之前的下標,簡單來說就是返回的是下標,而不是值,我們需要通過索引才能獲取到相應的值. 
上面是將數組或者是tuple等按升序排列,那么你肯定會問,降序排序應該用哪個函數來實現呢,或者說怎么實現呢?
- 第一種方法:b=argsort(a) #a為你源數組,這里a也可以是tuple
c=b[::-1] #c就是你想要的降序的索引了,然后你就可以通過c來獲取a中的值了- 第二種方法:b=argsort(-a)#a為源數組,不能是tuple
(7)、transpose( )方法
這個方法用於矩陣的轉置.
完成矩陣的轉置還可以這樣做:A.T來完成
假設矩陣為A,則y=transpose(A)或者是y=A.T就可以完成轉置,但是有一種情況我們用transpose( )函數進行轉置需要注意。
如下: x=linspace(0,4,5) #參數的意義按順序為:開始值、終值和元素個數 
y=transpose(x) #轉置
- 1
- 1
從上面兩個結果對比可以看出,沒有轉置成功,那么,這種情況下,怎么能夠成功??
原因是因為:x.shape 為(5,),而不是(5,1)導致的 
x=linspace(0,4,5) x.shape=(5,1) y=transpose(x)
- 1
- 2
- 3
- 1
- 2
- 3
從結果可以看出,這樣我們就轉置成功了。
