天天看點

python資料分析——pandas,numpy,matplotpandasnumpymatplot

  • pandas
    • pandas.categorical
    • map函數
  • numpy
    • argsort()
    • numpy的ravel()
    • meshgrid函數
    • np.split
  • matplot

pandas

pandas.categorical

>>> pd.Categorical([, , , , , ])
[, , , , , ]
Categories (, int64): [, , ]
>>> pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
[a, b, c, a, b, c]
Categories (, object): [a, b, c]
#### 對category進行排序
>>> c = pd.Categorical(['a','b','c','a','b','c'], ordered=True,
...                    categories=['c', 'b', 'a'])
>>> c
[a, b, c, a, b, c]
Categories (, object): [c < b < a]
>>> c.min()
'c'
           
  • categorical.codes
  • categorical官方文檔的解釋

map函數

主要作用是将函數作用于一個Series的每一個元素,用法如下所示

In []: frame['e'].map(format)
Out[]: 
Utah       
Ohio      -
Texas      
Oregon    -
Name: e, dtype: object
           

總的來說就是apply()是一種讓函數作用于列或者行操作,applymap()是一種讓函數作用于DataFrame每一個元素的操作,而map是一種讓函數作用于Series每一個元素的操作

numpy

argsort()

函數将數組的值從小到大排序後,并按照其相對應的索引值輸出

##一維數組
>>> a = array([,,])  
>>> argsort(a)  
array([, , ])  
##二維數組
>>> b = array([[1,2],[2,3]])  
>>> argsort(b,axis=) #按行排序  
array([[0, 1],  
       [0, 1]])  
>>> argsort(b,axis=) #按列排序  
array([[0, 0],  
       [1, 1]])  
>>>  
           

numpy的ravel()

将多元資料變成一維資料,一般是對label做此操作。資料類型是np.ravel的

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print(np.ravel(x))
[     ]
>>> print(x.reshape(-))
[     ]
           
  • numpy 辨異 (五)—— numpy.ravel() vs numpy.flatten()

python中小數預設是按照科學計數法顯示的,在代碼裡面加上一行

np.set_printoptions(suppress=True)
##這樣就會以小數的形式顯示了
           

meshgrid函數

通常在資料的矢量化上使用,meshgrid的作用适用于生成網格型資料,可以接受兩個一維數組生成兩個二維矩陣,對應兩個數組中所有的(x,y)對。接下來通過簡單的shell互動來示範一下這個功能的使用,并做一下小結。

import numpy as np
N ,M= ,
x1_min, x1_max = ,  # 第列的範圍
x2_min, x2_max = ,   # 第列的範圍
t1 = np.linspace(x1_min, x1_max, N)
t2 = np.linspace(x2_min, x2_max, M)
x1, x2 = np.meshgrid(t1, t2)  # 生成網格采樣點
print(x1)
print(x2)
x_show = np.stack((x1.flat, x2.flat), axis=)  # 測試點
print(x_show)##shape-(, )

[[  1.     3.25   5.5    7.75  10.  ]
 [  1.     3.25   5.5    7.75  10.  ]
 [  1.     3.25   5.5    7.75  10.  ]
 [  1.     3.25   5.5    7.75  10.  ]
 [  1.     3.25   5.5    7.75  10.  ]]
[[  3.   3.   3.   3.   3.]
 [  6.   6.   6.   6.   6.]
 [  9.   9.   9.   9.   9.]
 [ 12.  12.  12.  12.  12.]
 [ 15.  15.  15.  15.  15.]]
[[  1.     3.  ]
 [  3.25   3.  ]
 [  5.5    3.  ]
 [  7.75   3.  ]
 [ 10.     3.  ]
 [  1.     6.  ]
 [  3.25   6.  ]
 [  5.5    6.  ]
 [  7.75   6.  ]
 [ 10.     6.  ]
 [  1.     9.  ]
 [  3.25   9.  ]
 [  5.5    9.  ]
 [  7.75   9.  ]
 [ 10.     9.  ]
 [  1.    12.  ]
 [  3.25  12.  ]
 [  5.5   12.  ]
 [  7.75  12.  ]
 [ 10.    12.  ]
 [  1.    15.  ]
 [  3.25  15.  ]
 [  5.5   15.  ]
 [  7.75  15.  ]
 [ 10.    15.  ]]
           

np.split

資料可以更好的分割開來。對訓練資料可以采用這種方法把特征和label分開

import numpy as np
data = [[1,2,3,4],[5,6,7,8]]
data = np.array(data)
x,y = np.split(data,(,),axis=)
##axis = 按照行的方向進行分割,
print(x)
print(y)
##結果:
[[1 2]
 [5 6]]
[[3 4]
 [7 8]]
 x,y = np.split(data,(,),axis=)
 ##結果
 [[1 2 3]
 [5 6 7]]
[[4]
 [8]]
           

matplot

plt.legend

(loc=’upper right’)#顯示圖例,就是圖裡面的線代表什麼,loc指定圖例的位置

pcolormesh函數将x1,x2兩個網格矩陣和對應的預測結果y_show_hat繪制在圖檔上

plt.pcolormesh(x1, x2, y_show_hat, cmap=cm_light) # 預測值的顯示

plt.scatte-畫散點圖

參數c表示的是顔色,可以是色彩或顔色序列。

plt.xlim()

- 調用時不帶參數,則傳回目前的參數值。例如,plt.xlim()傳回目前的X軸繪圖範圍。

- 調用時帶參數,則設定參數值。是以,plt.xlim([0,10])會将X軸的範圍設定為0到10

繼續閱讀