1. Nmupy處理一維數(shù)據(jù)
-
Numpy數(shù)組
-
Numpy array 與Python list的區(qū)別

-
創(chuàng)建Numpy數(shù)組
- 首先創(chuàng)建一個Python列表
- 然后調(diào)用np.array(), 將該列表作為參數(shù), 如下面的代碼示例.
示例數(shù)據(jù)
import numpy as np
# First 20 countries with employment data
countries = np.array([
'Afghanistan', 'Albania', 'Algeria', 'Angola', 'Argentina',
'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas',
'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium',
'Belize', 'Benin', 'Bhutan', 'Bolivia',
'Bosnia and Herzegovina'
])
# Employment data in 2007 for those 20 countries
employment = np.array([
55.70000076, 51.40000153, 50.5 , 75.69999695,
58.40000153, 40.09999847, 61.5 , 57.09999847,
60.90000153, 66.59999847, 60.40000153, 68.09999847,
66.90000153, 53.40000153, 48.59999847, 56.79999924,
71.59999847, 58.40000153, 70.40000153, 41.20000076
])
-
獲取元素
print(countries[0])
print(countries[3])
Afghanistan
Angola
-
切片
print(countries[0:3])
print(countries[:3])
print(countries[17:])
print(countries[:])
['Afghanistan' 'Albania' 'Algeria']
['Afghanistan' 'Albania' 'Algeria']
['Bhutan' 'Bolivia' 'Bosnia and Herzegovina']
['Afghanistan' 'Albania' 'Algeria' 'Angola' 'Argentina' 'Armenia'
'Australia' 'Austria' 'Azerbaijan' 'Bahamas' 'Bahrain' 'Bangladesh'
'Barbados' 'Belarus' 'Belgium' 'Belize' 'Benin' 'Bhutan' 'Bolivia'
'Bosnia and Herzegovina']
-
元素類型
print(countries.dtype)
print(employment.dtype)
print(np.array([0, 1, 2, 3]).dtype)
print(np.array([1.0, 1.5, 2.0, 2.5]).dtype)
print(np.array([True, False, True]).dtype)
print(np.array(['AL', 'AK', 'AZ', 'AR', 'CA']).dtype)
|S22
float64
int64
float64
bool
|S2
備注: 其中S代表字符串, 22表示數(shù)組中最長的字符有22個字母
-
Looping
for country in countries:
print('Examining country {}'.format(country))
for i in range(len(countries)):
country = countries[i]
country_employment = employment[i]
print('Country {} has employment {}'.format(country,
country_employment))
-
Numpy 常用函數(shù)
print(employment.mean())
print(employment.std())
print(employment.max())
print(employment.sum())
58.68500003850001
9.338269113687888
75.69999695
1173.70000077
備注: argmax()函數(shù) - 返回最大值的位置
?
-
向量化運算
這個概念來自于線性代數(shù), 因此規(guī)則和線性代數(shù)中相同. 它和列表之間的相加不同, 如下圖.
-
向量加法

-
向量與標量相乘
規(guī)則和線性代數(shù)中相同. 它和列表與一個數(shù)字相乘不同, 列表和一個數(shù)字相乘,則會重復(fù)這個列表中的值相應(yīng)的次數(shù), 形成一個新的列表, 如下圖.

-
更多的向量運算

import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([1, 2, 1, 2])
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** b)
[2 4 4 6]
[0 0 2 2]
[1 4 3 8]
[1 1 3 2]
[ 1 4 3 16]
a = np.array([1, 2, 3, 4])
b = 2
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** b)
[3 4 5 6]
[-1 0 1 2]
[2 4 6 8]
[0 1 1 2]
[ 1 4 9 16]
a = np.array([True, True, False, False])
b = np.array([True, False, True, False])
print(a & b)
print(a | b)
print(~a)
print(a & True)
print(a & False)
print(a | True)
print(a | False)
[ True False False False]
[ True True True False]
[False False True True]
[ True True False False]
[False False False False]
[ True True True True]
[ True True False False]
a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 4, 3, 2, 1])
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
[False False False True True]
[False False True True True]
[ True True False False False]
[ True True True False False]
[False False True False False]
[ True True False True True]
a = np.array([1, 2, 3, 4])
b = 2
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
[False False True True]
[False True True True]
[ True False False False]
[ True True False False]
[False True False False]
[ True False True True]
?
-
標準化數(shù)據(jù)
為什么要對數(shù)據(jù)進行標準化?
在數(shù)據(jù)分析中, 一個常常需要回答的問題是, 某一個數(shù)據(jù)點與其他數(shù)據(jù)點相比有何取別?
例如 美國就業(yè)率與其他國家就業(yè)率的差異, 他比平均值更高還是更低? 兩者相差多少?
如果要回答上述問題, 通常是將各數(shù)據(jù)點轉(zhuǎn)換為相對于平均值的標準偏差值, 這叫做數(shù)據(jù)標準化.
比如2007年的就業(yè)數(shù)據(jù), 平均就業(yè)率是58.6%, 標準偏差是10.5%, 美國就業(yè)率是62.3%, 那么美國就業(yè)率和平均就業(yè)率之間差距就是3.7%, 這約等于0.35個標準偏差, 而墨西哥2007年的平均就業(yè)率約為57.9%, 那么墨西哥就業(yè)率和平均就業(yè)率之間差距就是-0.7%, 注意, 負值表示這個值低于平均值, 正值表示高于平均值, 因此墨西哥可以轉(zhuǎn)換為低于平均值0.067個標準偏差.
如何用numpy數(shù)組的向量化運算, 對一個數(shù)組中的所有值進行標準化?
代碼如下:
def standardize_data(values):
'''傳入一個數(shù)組. 然后用向量運算, 對該數(shù)組進行標準化'''
standardized_values = (values - values.mean()) / values.std()
return standardized_values
?
-
Numpy索引數(shù)組(index array)

何為索引數(shù)組
對于上圖中的b即為索引數(shù)組, 它將返回數(shù)組a中, 在數(shù)組b中對應(yīng)為
True的元素, 因此a[b]所返回的就是3,4,5
索引數(shù)組的三種表現(xiàn)形式
- 構(gòu)建如上圖所示的布爾數(shù)組
- 數(shù)組b也可以用另外一種形式表示
b = a > 2, 然后執(zhí)行a[b]- 為了實現(xiàn)上圖中結(jié)果, 還可以直接直接使用
a > 2作為索引數(shù)組, 而不用賦值給b, 即a[a>2]
代碼示例:
使用索引數(shù)組
import numpy as np
# Using index arrays
a = np.array([1, 2, 3, 4])
b = np.array([True, True, False, False])
print(a[b])
print(a[np.array([True, False, True, False])])
[1 2]
[1 3]
使用向量化操作創(chuàng)建索引數(shù)組
a = np.array([1, 2, 3, 2, 1])
b = (a >= 2)
print(a[b])
print(a[a >= 2])
[2 3 2]
[2 3 2]
在另一個數(shù)組上使用向量化操作創(chuàng)建索引數(shù)組
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 2, 3, 2, 1])
print(b == 2)
print(a[b == 2])
[False True False True False]
[2 4]
?
-
原位與非原位
+=: 原位運算, 會將所有的新值儲存在原值所在的位置, 而不是創(chuàng)建一個新的數(shù)組
+: 非原位運算
二者區(qū)別見下圖:

?
?
2. Nmupy處理二維數(shù)據(jù)
-
Numpy二維數(shù)組
-
Numpy 2D-array 與Python List of lists的區(qū)別

示例數(shù)據(jù)
import numpy as np
# Subway ridership for 5 stations on 10 different days
ridership = np.array([
[ 0, 0, 2, 5, 0],
[1478, 3877, 3674, 2328, 2539],
[1613, 4088, 3991, 6461, 2691],
[1560, 3392, 3826, 4787, 2613],
[1608, 4802, 3932, 4477, 2705],
[1576, 3933, 3909, 4979, 2685],
[ 95, 229, 255, 496, 201],
[ 2, 0, 1, 27, 0],
[1438, 3785, 3589, 4174, 2215],
[1342, 4043, 4009, 4665, 3033]
])
-
獲取元素
print(ridership[1, 3])
print(ridership[1:3, 3:5])
print(ridership[1, :])
2328
[[2328 2539]
[6461 2691]]
[1478 3877 3674 2328 2539]
-
行向量化運算與列向量化運算
print(ridership[0, :] + ridership[1, :])
print(ridership[:, 0] + ridership[:, 1])
[1478 3877 3676 2333 2539]
[ 0 5355 5701 4952 6410 5509 324 2 5223 5385]
-
2D-數(shù)組間的向量化運算
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
print(a + b)
[[ 2 3 4]
[ 6 7 8]
[10 11 12]]
?
-
Numpy數(shù)軸
在許多情況下, 需要對列或行進行操作, 那么就需要用到數(shù)軸的知識. 比如圖中所示, 每一行代表一個日期點, 每一列代表一個車站, 我們可以對每個車站求平均客流量, 當然我也可以求每天的平均客流量.
Numpy中的大多數(shù)函數(shù)都將數(shù)軸作為參數(shù)之一, 其值通常為0或1.

代碼示例
import numpy as np
# NumPy axis argument
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(a.sum())
print(a.sum(axis=0))
print(a.sum(axis=1))
45
[12 15 18]
[ 6 15 24]
?
-
Numpy數(shù)據(jù)類型
-
dtype
適用于二維數(shù)組
import numpy as np
np.array([1,2,3,4,5]).dtype
dtype('int64')
,不過需要注意的數(shù)組中的每一個元素都屬于同一類型, 如果為多種類型, 最終形成數(shù)組后, 都將被轉(zhuǎn)換為字符串.
