一、基本概念
numpy特點
numpy作為三方庫,能快速操作數組,也就是python中的列表類型數據,包括數組創建查詢修改移除,意味著也能快速處理excel等來源數據,在數據分析中是非常實用的工具。
1、批量化操作數組數據,高效;
2、廣播機制,不同數組之間的交互(意味著數據與數據之間的交互)
一些基本概念
數組維度
一維:[1,2,3,4] 只有行
二維:[ [1,2],[3,4] ] 行和列
三維:[ [ [1,2],[3,4] ],[ [1,2],[3,4] ] ] 可結合x,y,z軸理解
常見數組一般最高就到三維,更高維的需要發揮三體人想象力去理解
二、numpy數組
numpy數組創建
1. numpy.array(可迭代對象)
import numpy as np
np.array(可迭代對象)
#可迭代對象:列表、元組、range(number)
#e.g
np.array([i for i in range(10)])
np.array((0,1,2,3,4,5,6,7,8,9))
np.array(range(10))
ps: numpy高效在這里可以稍微體現一下,以循環生成10000000個數據為例
import numpy as np
import time
#普通for循環生10000000長度數組
t1 = time.time()
lst = []
for i in range(10000000):
lst.append(i)
t2 = time.time()
#numpy生成10000000長度數組
t3 = time.time()
lst = np.array(range(10000000))
t4 = time.time()
#耗時
print(t2-t1)
print(t4-t3)
#1.5315017700195312
#1.3233845233917236
2. numpy.arange()
arange()方法常用于通過參數直接生成一個等差數列,也即一維數組
np.arange([start,] stop[, step,], dtype=None)
#僅列舉常用的參數
#start 數組起始值,可選,默認為0
#stop 數組結束值,不包含尾
#step 數組創建時步長,可選,默認為1
#dtype 指定數據類型,可選,不傳由方法自己決定類型
#e.g
print(np.arange(10)) #輸出: [0 1 2 3 4 5 6 7 8 9]
print(np.arange(1,10,2)) #輸出:[1 3 5 7 9]
print(np.arange(1,10,2,dtype=float)) #輸出: [1. 3. 5. 7. 9.]
3. numpy.reshape()
numpy中的變形函數,能把array和arange產生的數組轉換成目標維度數組,方便后續的使用,可以結合array和arange方法使用
支持:
低維數組轉為高維數組
高維數組轉為低維數組
np.reshape(a, shape, order='C')
也可以寫成
a.reshape(shape)
#a 待變換維度的數組
#shape 目標維度形狀,以元組表示(1,2,3)或(1,2)
#order 參數是用作排序,可crtl跳轉源碼了解,某些場景才需要控制,一般按行存儲'C' 按列存儲‘F’ 按原始數組排序'A',不常用,可選
#e.g
#一維數組
a = np.arange(12)
print(a)
#輸出: [ 0 1 2 3 4 5 6 7 8 9 10 11]
#一維數組轉為二維,6行2列
b = a.reshape((6,2))
print(b)
#輸出:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]
#一維數組轉為三維,可以理解為線轉為x,y,z空間坐標系
c = a.reshape((3,2,2))
print(c)
#輸出:
[[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]
[[ 8 9]
[10 11]]]
#三維轉為二維
d = c.reshape((3,4))
print(d)
#輸出:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
PS:高維數組轉為一維數組還有另一個方法
numpy.flatten()
flatten()是NumPy數組對象的一個方法,常用于將多維數組”展平”成一維數組。
特點:不需要知道數組長度不管數組是幾維,迅速以一維呈現
#三維數組
a = np.arange(12).reshape((3,2,2))
print(a)
#輸出:
[[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]
[[ 8 9]
[10 11]]]
#將三維數組直接變成一維數組
q = a.flatten()
print(q)
#輸出:[ 0 1 2 3 4 5 6 7 8 9 10 11]
轉置換軸
僅用于一,二維數組操作,也屬于一種變形,但本身不移動數據,只是改變元素的索引方式
轉置操作會交換數組的行和列,即原數組的第i行第j列元素會變成轉置后數組的第j行第i列元素
1. 元素對象.T
T屬性直接交換數組行列
a = np.arange(12).reshape((6,2))
print(a.shape) #輸出(6,2)
print(a.T.shape) #輸出(2,6)
2. 元素對象.swapaxes(1,0)
swapaxes方法也是直接交換數組的1,0軸,1和0只是形參,順序可以交換
a = np.arange(12).reshape((6,2))
print(a.shape) #輸出(6,2)
print(a.swapaxes(1,0).shape) #輸出(2,6)
3. 元素對象.transpose
transpose()也有參數
x.transpose((0,1))表示按照原坐標軸改變序列,也就是保持不變
x.transpose((1,0))表示交換'0軸’和'1軸’
a = np.arange(12).reshape((6,2))
print(a.shape) #輸出:(6, 2)
print(a.transpose().shape) #輸出:(2, 6)
print(a.transpose((0,1)).shape) #輸出:(6, 2)
print(a.transpose(1,0).shape) #輸出:(2, 6)
numpy數組對象幾大屬性
數組對象.ndim
ndim屬性用于獲取數組的維數,它告訴你該數組是幾維數組
a = np.arange(12).reshape((3,2,2))
print(a.ndim) #輸出:3
q = a.flatten()
print(q.ndim) #輸出:1
數組對象.shape
shape 屬性返回數組在每個維度上大小的元組。即arange方法中的shape參數。
對于一維數組,它返回一個表示數組長度的元組
對于二維數組,它返回表示行數和列數的元組
a = np.arange(12).reshape((3,2,2))
print(a.shape) #輸出:(3, 2, 2)
q = a.reshape((6,2))
print(q.shape) #輸出:(6, 2)
b = a.flatten()
print(b.shape) #輸出:(12,)
數組對象.dtype
dtype 屬性用于獲取數組元素的數據類型。arange方法里也有該屬性指定
a = np.arange(12,dtype=float)
print(a.dtype) #輸出:float64
a = np.arange(12)
print(a.dtype) #輸出:int32
數組對象.size
size屬性用于獲取數組長度,比較好理解,即告訴你這個數組不管幾維,里面包含了幾個數組元素
a = np.arange(12).reshape((3,2,2))
print(a.size) #輸出: 12
a = np.arange(12)
print(a.size) #輸出: 12
一些特殊數組用法
numpy.ones()
np.ones(shape,dtype,order)
#參數作用和arange方法一致
#shape 指定數組幾維什么形狀
#dtype 指定數據類型,可選,默認自己根據輸入決定
#order 排序 可選
#也能創建指定形狀數組,但默認元素以1填充
a = np.ones((6,2))
print(a)
#輸出:
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
numpy.ones_like()
ones_like() 以某個數組為目標,按照其形狀進行復制,但默認以1填充元素
np.ones_like(a,dtype,subok=True)
#a 待復制形狀的目標數組對象
#dtype 數據類型
#subok 這是一個可選參數,默認為 False。一般用于帶有掩碼的數據
a = np.arange(12).reshape((6,2))
print(a)
#輸出:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]
a = np.ones_like(a)
print(a)
#輸出:
[[1 1]
[1 1]
[1 1]
[1 1]
[1 1]
[1 1]]
numpy.full()
full()和one()一樣,創建指定形狀數組,但one默認以1填充,full默認通過指定的內容填充
np.full(shape,fill_value,dtype=None,order='C')
# shape 指定數組形狀
# fill_value 指定填充的內容
#dtype 指定數據類型,可選
#order 排序,可選
#e.g
a = np.full((6,2),'a')
print(a)
#輸出:
[['a' 'a']
['a' 'a']
['a' 'a']
['a' 'a']
['a' 'a']
['a' 'a']]
numpy.full_like()
和one_like()一樣,以目標數組為藍本,復制形狀,以指定內容填充數組
numpy.full_like(a,fill_value=1,dtype)
#a 待復制的目標數組對象
#fill_value 指定填充的內容
#e.g
a = np.arange(12).reshape((6,2))
print(a)
#輸出:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]
a = np.full_like(a, fill_value=1)
print(a)
#輸出:
[[1 1]
[1 1]
[1 1]
[1 1]
[1 1]
[1 1]]