【1】數(shù)據(jù)分析之表示

單元1:NumPy庫(kù)入門

數(shù)據(jù)的維度

從一個(gè)數(shù)據(jù)到一組數(shù)據(jù)

一個(gè)數(shù)據(jù):表達(dá)一個(gè)含義
一組數(shù)據(jù):表達(dá)一個(gè)或多個(gè)含義
維度:一組數(shù)據(jù)的組織形式
一維數(shù)據(jù):由對(duì)等關(guān)系的有序或無序數(shù)據(jù)構(gòu)成,采用線性方式組織。

  • 列表和數(shù)組:一組數(shù)據(jù)的有序結(jié)構(gòu)
  • 區(qū)別:列表:數(shù)據(jù)類型可以不同;數(shù)組:數(shù)據(jù)類型相同。

二維數(shù)據(jù):由多個(gè)一維數(shù)據(jù)構(gòu)成,是一維數(shù)據(jù)的組合形式。

  • 表格是典型的二維數(shù)據(jù)。
  • 其中,表頭是二維數(shù)據(jù)的一部分。

多維數(shù)據(jù):由一維或二維數(shù)據(jù)在新維度上擴(kuò)展形成。
高維數(shù)據(jù):僅利用最基本的二元關(guān)系展示數(shù)據(jù)間的復(fù)雜結(jié)構(gòu)。

  • 鍵值對(duì)

數(shù)據(jù)維度的Python表示

一維數(shù)據(jù):列表[]和集合{}類型
二維數(shù)據(jù):列表類型
多維數(shù)據(jù):列表類型
高維數(shù)據(jù):字典類型或數(shù)據(jù)表示格式

NumPy的數(shù)組對(duì)象:ndarray

NumPy

開源的Python科學(xué)計(jì)算基礎(chǔ)庫(kù)

  • 強(qiáng)大的N維數(shù)組對(duì)象:ndarray
  • 廣播功能函數(shù)
  • 整合C、C++、Fortran代碼的工具
  • 線性代數(shù)、傅里葉變換、隨機(jī)數(shù)生成等功能

NumPy是SciPy、Pandas等庫(kù)的基礎(chǔ)

NumPy的引用

import numpy as np

盡管別名可以省略或更改,建議使用以上方式

N維數(shù)組對(duì)象:ndarray

例:計(jì)算A^2 + B^3,其中A和B為一維數(shù)組

def pySum():
    a = [0, 1, 2, 3, 4]
    b = [9, 8, 7, 6, 5]
    c = []
    for i in range(len(a)):
        c.append(a[i]**2 + b[i]**3)
    return c
print(pySum())

用NumPy:

import numpy as np
def npSum():
    a = np.array([0, 1, 2, 3, 4])
    b = np.array([9, 8, 7, 6, 5])
    c = a**2 + b**3
    return c
print(npSum())
  • 數(shù)組對(duì)象可以去掉元素間運(yùn)算所需的循環(huán),使一維向量更像單個(gè)數(shù)據(jù)。
  • 設(shè)置專門的數(shù)組對(duì)象,經(jīng)過優(yōu)化,可以提升這類應(yīng)用的運(yùn)算速度。

觀察:科學(xué)計(jì)算中,一個(gè)維度所有數(shù)據(jù)的類型往往相同。

  • 數(shù)組對(duì)象采用相同的數(shù)據(jù)類型,有助于節(jié)省運(yùn)算和存儲(chǔ)空間。

ndarray是一個(gè)多維數(shù)組對(duì)象,由兩部分組成:

  • 實(shí)際的數(shù)據(jù)
  • 描述這些數(shù)據(jù)的元數(shù)據(jù)(數(shù)據(jù)維度、數(shù)據(jù)類型等)

ndarray數(shù)組一般要求所有元素類型相同(同質(zhì)),數(shù)組下標(biāo)從0開始。

np.array()生成一個(gè)ndarray數(shù)組
軸(axis):保存數(shù)據(jù)的維度
秩(rank):軸的數(shù)量

ndarray對(duì)象的屬性

屬性 說明
.ndim 秩,即軸的數(shù)量或維度的數(shù)量
.shape ndarray對(duì)象的尺度,對(duì)于矩陣,n行m列
.size ndarray對(duì)象元素的個(gè)數(shù),相當(dāng)于.shape中n*m的值
.dtype ndarray對(duì)象的元素類型
.itemsize ndarray對(duì)象中每個(gè)元素的大小,以字節(jié)為單位

ndarray的元素類型

數(shù)據(jù)類型 說明
bool 布爾類型,True或False
inte 與C語言中的int類型一致,一般是int32或int64
intp 用于索引的整數(shù),與C語言中ssize_t一致,int32或int64
int8 字節(jié)長(zhǎng)度的整數(shù),取值:[-128,127]
int16 16位長(zhǎng)度的整數(shù),取值:[-32768,32767]
int32 32位長(zhǎng)度的整數(shù),取值:[-2^31, 2^31-1]
int64 64位長(zhǎng)度的整數(shù),取值:[-2^63, 2^63-1]
uint8 8位無符號(hào)整數(shù),取值:[0, 255]
uint16 16位無符號(hào)整數(shù),取值:[0, 65535]
uint32 32位無符號(hào)整數(shù),取值:[0, 2^32-1]
uint64 64位無符號(hào)整數(shù),取值:[0, 2^64-1]
float16 16位半精度浮點(diǎn)數(shù):1位符號(hào)位,5位指數(shù),10位尾數(shù)
float32 32位半精度浮點(diǎn)數(shù):1位符號(hào)位,8位指數(shù),23位尾數(shù)
float64 64位半精度浮點(diǎn)數(shù):1位符號(hào)位,11位指數(shù),52位尾數(shù)
complex64 復(fù)數(shù)類型,實(shí)部和虛部都是32位浮點(diǎn)數(shù)
complex128 復(fù)數(shù)類型,實(shí)部和虛部都是64位浮點(diǎn)數(shù)

對(duì)比:Python語法僅支持整數(shù)、浮點(diǎn)數(shù)和復(fù)數(shù)3中類型

  • 科學(xué)計(jì)算涉及數(shù)據(jù)較多,對(duì)存儲(chǔ)和性能都有較高要求。
  • 對(duì)元素類型精細(xì)定義,有助于Numpy合理使用存儲(chǔ)空間并優(yōu)化性能。
  • 有助于程序員對(duì)程序規(guī)模有合理評(píng)估。

ndarray可以由非同質(zhì)對(duì)象構(gòu)成。但無法有效發(fā)揮NumPy優(yōu)勢(shì),盡量避免使用。

ndarray數(shù)組的創(chuàng)建和變換

ndarray數(shù)組的創(chuàng)建方法

  • 從Python中的列表、元組等類型創(chuàng)建ndarray數(shù)組
  • 使用NumPy中函數(shù)創(chuàng)建ndarray數(shù)組,如:arange,ones, zeros等
  • 從字節(jié)流(raw bytes)中創(chuàng)建ndarray數(shù)組
  • 從文件中讀取特定格式,創(chuàng)建ndarray數(shù)組

(1) 從Python中的列表、元組等類型創(chuàng)建ndarray數(shù)組

x = np.array(list/tuple)
x = np.array(list/tuple, dtype = np.float32)

當(dāng)np.array()不指定dtype時(shí),NumPy將根據(jù)數(shù)據(jù)情況關(guān)聯(lián)一個(gè)dtype類型。

(2) 使用NumPy中函數(shù)創(chuàng)建ndarray數(shù)組,如:arange,ones, zeros等

函數(shù) 說明
np.arange(n) 類似range()函數(shù),返回ndarray類型,元素從0到n-1
np.ones(shape) 根據(jù)shape生成一個(gè)全1數(shù)組,shape是元組類型
np.zeros(shape) 根據(jù)shape生成一個(gè)全0數(shù)組,shape是元組類型
np.full(shape, val) 根據(jù)shape生成一個(gè)全val值數(shù)組,shape是元組類型
np.eye(n) 創(chuàng)建一個(gè)n階單位方陣
np.ones_like(a) 根據(jù)數(shù)組a的形狀生成一個(gè)全1數(shù)組
np.zeros_like(a) 根據(jù)數(shù)組a的形狀生成一個(gè)全0數(shù)組
np.full_lise(a, val) 根據(jù)數(shù)組a的形狀生成一個(gè)全val值數(shù)組
np.linspace() 根據(jù)起止數(shù)據(jù)等間距填充數(shù)據(jù),形成數(shù)組
np.concatenate() 將兩個(gè)或多個(gè)數(shù)組合并成一個(gè)新的數(shù)組
a = np.linspace(1, 10, 4)
a
array([ 1.,  4.,  7., 10.])
b = np.linspace(1, 10, 4, endpoint = False)  ##不將10作為結(jié)束值
b
array([1.  , 3.25, 5.5 , 7.75])
c = np.concatenate((a,b))
c
array([ 1.  ,  4.  ,  7.  , 10.  ,  1.  ,  3.25,  5.5 ,  7.75])

ndarray數(shù)組的變換

對(duì)創(chuàng)建好的ndarray數(shù)組,可以對(duì)其進(jìn)行維度變換的元素類型變換
維度變換:

方法 說明
.reshape(shape) 不改變數(shù)組元素,返回一個(gè)shape形狀的數(shù)組,原數(shù)組不變
.resize(shape) 與.reshape()功能已知,但修改原數(shù)組
.swapaxes(ax1, ax2) 將數(shù)組n個(gè)維度中兩個(gè)維度進(jìn)行調(diào)換
.flatten() 對(duì)數(shù)組進(jìn)行降維,返回折疊后的一維數(shù)組,原數(shù)組不變
a = np.ones((2,3,4), dtype = np.int32)

a.reshape((3,8))
Out[24]: 
array([[1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])

a
Out[25]: 
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

a.resize((3,8))

a
Out[27]: 
array([[1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])

a.flatten()
Out[28]: 
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1])

a
Out[29]: 
array([[1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])

類型變換
new_a=a.astype(new_type)
會(huì)創(chuàng)建一個(gè)新數(shù)組,可以用來對(duì)原數(shù)組進(jìn)行拷貝

a = np.ones((2,3,4), dtype = np.int)

a
Out[31]: 
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

b = a.astype(np.float)

b
Out[34]: 
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])

向列表的轉(zhuǎn)換
ls = a.tolist()

a = np.full((2,3,4), 25, dtype=np.int32)

a
Out[36]: 
array([[[25, 25, 25, 25],
        [25, 25, 25, 25],
        [25, 25, 25, 25]],

       [[25, 25, 25, 25],
        [25, 25, 25, 25],
        [25, 25, 25, 25]]])

a.tolist()
Out[37]: 
[[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]],
 [[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]]]

ndarray數(shù)組的操作

對(duì)數(shù)組的索引和切片

  • 索引:獲取數(shù)組中特定位置的元素
  • 切片:獲取數(shù)組的子集

一維數(shù)組的索引和切片:與列表類似

a = np.arange(1,10,1)

a
Out[39]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

a[2]
Out[40]: 3

a[1:4:2]
Out[41]: array([2, 4])

多維數(shù)組的索引

a = np.arange(24).reshape((2,3,4))

a
Out[43]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

a[1,2,3]
Out[44]: 23

a[-1,-2,-3]
Out[45]: 17

a[1,2,3]
Out[44]: 23

a[-1,-2,-3]
Out[45]: 17

a[:,1,-3]  ##選取一個(gè)維度用:
Out[46]: array([ 5, 17])

a[:, 1:3, :]  ##每個(gè)維度切片方法與一維數(shù)組相同
Out[47]: 
array([[[ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])

a[:, :, ::2]  ##每個(gè)維度可以使用步長(zhǎng)跳躍切片
Out[48]: 
array([[[ 0,  2],
        [ 4,  6],
        [ 8, 10]],

       [[12, 14],
        [16, 18],
        [20, 22]]])

ndarray數(shù)組的運(yùn)算

數(shù)組與標(biāo)量之間的運(yùn)算

作用于數(shù)組的每一個(gè)元素
例如:計(jì)算a與元素平均值的商

a = np.arange(24).reshape((2,3,4))

a
Out[50]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

a.mean()
Out[51]: 11.5

a = a/a.mean()

a
Out[53]: 
array([[[0.        , 0.08695652, 0.17391304, 0.26086957],
        [0.34782609, 0.43478261, 0.52173913, 0.60869565],
        [0.69565217, 0.7826087 , 0.86956522, 0.95652174]],

       [[1.04347826, 1.13043478, 1.2173913 , 1.30434783],
        [1.39130435, 1.47826087, 1.56521739, 1.65217391],
        [1.73913043, 1.82608696, 1.91304348, 2.        ]]])

NumPy一元函數(shù)

對(duì)ndarray中的數(shù)據(jù)執(zhí)行元素級(jí)的運(yùn)算

函數(shù) 說明
np.abs(x) np.fabs(x) 計(jì)算數(shù)組各元素的絕對(duì)值
np.sqrt(x) 計(jì)算數(shù)組各元素的平方根
np.square(x) 計(jì)算數(shù)組各元素的平方
np.log(x) np.log10(x) np.log2(x) 計(jì)算數(shù)組各元素的自然對(duì)數(shù)、10底對(duì)數(shù)和2底對(duì)數(shù)
np.ceil(x) np.floor(x) 計(jì)算數(shù)組各元素的ceiling值或floor值
np.rint(x) 計(jì)算數(shù)組各元素的四舍五入值
np.modf(x) 將數(shù)組各元素的小數(shù)和整數(shù)部分以兩個(gè)獨(dú)立數(shù)組形式返回
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) 計(jì)算數(shù)組各元素的普通型和雙曲型三角函數(shù)
np.exp(x) 計(jì)算數(shù)組各元素的指數(shù)值
np.sign(x) 計(jì)算數(shù)組各元素的符號(hào)值,1為正,0,-1為負(fù)
a = np.arange(24).reshape((2,3,4))

a
Out[55]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

np.square(a)
Out[56]: 
array([[[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]], dtype=int32)

np.sqrt(a)
Out[57]: 
array([[[0.        , 1.        , 1.41421356, 1.73205081],
        [2.        , 2.23606798, 2.44948974, 2.64575131],
        [2.82842712, 3.        , 3.16227766, 3.31662479]],

       [[3.46410162, 3.60555128, 3.74165739, 3.87298335],
        [4.        , 4.12310563, 4.24264069, 4.35889894],
        [4.47213595, 4.58257569, 4.69041576, 4.79583152]]])

a = np.sqrt(a)

np.modf(a)

Out[59]: 
(array([[[0.        , 0.        , 0.41421356, 0.73205081],
         [0.        , 0.23606798, 0.44948974, 0.64575131],
         [0.82842712, 0.        , 0.16227766, 0.31662479]],
 
        [[0.46410162, 0.60555128, 0.74165739, 0.87298335],
         [0.        , 0.12310563, 0.24264069, 0.35889894],
         [0.47213595, 0.58257569, 0.69041576, 0.79583152]]]),
 array([[[0., 1., 1., 1.],
         [2., 2., 2., 2.],
         [2., 3., 3., 3.]],
 
        [[3., 3., 3., 3.],
         [4., 4., 4., 4.],
         [4., 4., 4., 4.]]]))

NumPy二元函數(shù)

函數(shù) 說明
+ - * / ** 兩個(gè)數(shù)組各元素進(jìn)行對(duì)應(yīng)運(yùn)算
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin() 元素級(jí)的最大值/最小值計(jì)算
np.mod(x,y) 元素級(jí)的模運(yùn)算
np.copysign(x,y) 將數(shù)組y中各元素值得符號(hào)賦值給數(shù)組x對(duì)應(yīng)元素
> < >= <= == != 算數(shù)比較,產(chǎn)生布爾型數(shù)組
a = np.arange(24).reshape((2,3,4))

b = np.sqrt(a)

a
Out[62]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

b
Out[63]: 
array([[[0.        , 1.        , 1.41421356, 1.73205081],
        [2.        , 2.23606798, 2.44948974, 2.64575131],
        [2.82842712, 3.        , 3.16227766, 3.31662479]],

       [[3.46410162, 3.60555128, 3.74165739, 3.87298335],
        [4.        , 4.12310563, 4.24264069, 4.35889894],
        [4.47213595, 4.58257569, 4.69041576, 4.79583152]]])

np.maximum(a,b)
Out[64]:   ##運(yùn)算結(jié)果為浮點(diǎn)數(shù)
array([[[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]],

       [[12., 13., 14., 15.],
        [16., 17., 18., 19.],
        [20., 21., 22., 23.]]])

a > b
Out[65]: 
array([[[False, False,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]]])

單元2:NumPy數(shù)據(jù)存取與函數(shù)

數(shù)據(jù)的CSV文件存取

CSV文件

(Comma-Separated Value,逗號(hào)分隔值)
常見的文件格式,用來存儲(chǔ)批量數(shù)據(jù)。

寫入CSV文件:
np.savetxt(frame, array, fmt='%.18e', delimiter = None)

  • fname:文件、字符串或產(chǎn)生器,可以是.gz或.bz2的壓縮文件
  • array:存入文件的數(shù)組
  • fmt:寫入文件的格式,例如:%d %.2f %.18e
  • delimiter:分割字符串,默認(rèn)是任何空格

例如:

a = np.arange(100).reshape(5,20)

np.savetxt('a.csv', a, fmt = '%d', delimiter = ',')

np.savetxt('a.csv', a, fmt = '%.1f', delimiter = ',')  ##保存為一位小數(shù)

文件為:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39
40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59
60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99

讀取CSV文件:
np.loadtxt(frame, dtype = np.float, delimiter = None, unpack = False)

  • fname:文件、字符串或產(chǎn)生器,可以是.gz或.bz2的壓縮文件
  • dtype:數(shù)據(jù)類型,可選
  • delimiter:分割字符串,默認(rèn)是任何空格
  • unpack:如果True,讀入屬性將分別寫入不同變量。
b = np.loadtxt('a.csv', delimiter = ',')

b
Out[70]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
        13., 14., 15., 16., 17., 18., 19.],
       [20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.,
        33., 34., 35., 36., 37., 38., 39.],
       [40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52.,
        53., 54., 55., 56., 57., 58., 59.],
       [60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72.,
        73., 74., 75., 76., 77., 78., 79.],
       [80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92.,
        93., 94., 95., 96., 97., 98., 99.]])

b = np.loadtxt('a.csv', dtype = np.int, delimiter = ',')  ##指定數(shù)據(jù)類型為整數(shù)

b
Out[72]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
        36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
        56, 57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
        76, 77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
        96, 97, 98, 99]])

局限性:
只能有效存儲(chǔ)一維和二維數(shù)組

多維數(shù)據(jù)的存取

a.tofile(frame, sep = '', format = '%s')

  • fname:文件、字符串或產(chǎn)生器,可以是.gz或.bz2的壓縮文件
  • sep:分割字符串,如果是空串,寫入文件為二進(jìn)制
  • format:寫入數(shù)據(jù)的格式
a = np.arange(100).reshape(5, 10, 2)

a.tofile("b.dat", sep = ',', format = '%d')

文件為:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99

a.tofile("b.dat", format = '%d')

不指定分隔符,為二進(jìn)制文件,打開為:
? ? ? ? ? ? ?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
二進(jìn)制文件比文本文件占用空間更少。

讀取文件:
np.fromfile(frame, dtype = float, count = -1, sep = '')

  • fname:文件、字符串或產(chǎn)生器,可以是.gz或.bz2的壓縮文件
  • dtype:讀取的數(shù)據(jù)類型
  • count:讀入元素個(gè)數(shù),-1表示讀入整個(gè)文件
  • sep: 分割字符串,如果是空串,寫入文件為二進(jìn)制


c = np.fromfile("b.dat", dtype = np.int, sep = ",")

c
Out[79]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

c = np.fromfile("b.dat", dtype = np.int, sep = ",").reshape(5,10,2)  ##讀入的是一維數(shù)組,需要重新安排形狀

c
Out[81]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],

       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],

       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])

讀取二進(jìn)制文件:

a.tofile("b.dat", format = '%d')

c = np.fromfile("b.dat", dtype = np.int).reshape(5,20)

c
Out[84]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
        36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
        56, 57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
        76, 77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
        96, 97, 98, 99]])

需要注意:

  • 該方法需要讀取時(shí)知道存入文件時(shí)數(shù)組的維度和元素類型
  • a.tofile()和np.fromfile()需要配合使用
  • 可以通過元數(shù)據(jù)文件來存儲(chǔ)額外信息(數(shù)據(jù)類型,維度等)

NumPy的便捷文件存取

np.save(fname, array)或np.savez(fname, array)

  • fname:文件名,以.npy為擴(kuò)展名,壓縮擴(kuò)展名為.npz
  • array:數(shù)組變量

np.load(fname)

  • fname:文件名,以.npy為擴(kuò)展名,壓縮擴(kuò)展名為.npz
a = np.arange(100).reshape(5,10,2)

np.save("a.npy", a)

b = np.load("a.npy")

b
Out[88]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],

       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],

       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])

npy用二進(jìn)制形式存儲(chǔ)數(shù)據(jù)。在第一行用顯式的形式記錄出數(shù)據(jù)的信息。

NumPy的隨機(jī)數(shù)函數(shù)

NumPy的random子庫(kù)。使用:np.random.為函數(shù)名)

函數(shù) 說明
rand(d0, d1, ..., dn) 根據(jù)d1-dn(形狀)創(chuàng)建隨機(jī)數(shù)組,浮點(diǎn)數(shù),[0,1),均勻分布
randn(d0, d1, ..., dn) 根據(jù)d1-dn創(chuàng)建隨機(jī)數(shù)組,浮點(diǎn)數(shù),[0,1),正態(tài)分布
randint(low[, high, shape]) 根據(jù)shape創(chuàng)建隨機(jī)整數(shù)或整數(shù)組,范圍是[low, high)
seed(s) 隨機(jī)數(shù)種子,s是給定的種子
import numpy as np

a = np.random.rand(3, 4, 5)

a
Out[3]: 
array([[[0.15137256, 0.81596633, 0.67956184, 0.73756265, 0.81162265],
        [0.56326981, 0.61385898, 0.33826593, 0.90415749, 0.4795024 ],
        [0.24026997, 0.45779641, 0.27117777, 0.30485705, 0.12912242],
        [0.88616861, 0.93975078, 0.31494961, 0.5338654 , 0.15102703]],

       [[0.56245002, 0.31127358, 0.73616424, 0.04189588, 0.72795908],
        [0.70961088, 0.76399396, 0.14776895, 0.14973958, 0.53145689],
        [0.22079254, 0.61468065, 0.60782668, 0.26659865, 0.56179524],
        [0.24112438, 0.42520094, 0.3946708 , 0.31106286, 0.65153135]],

       [[0.73161609, 0.28116563, 0.31808233, 0.0756652 , 0.96681776],
        [0.77428823, 0.29668054, 0.79083103, 0.64877892, 0.89235409],
        [0.4047    , 0.95332281, 0.37391636, 0.18951718, 0.91031481],
        [0.0611578 , 0.83720539, 0.25169338, 0.22016327, 0.4919747 ]]])

b = np.random.randn(3, 4, 5)

b
Out[5]: 
array([[[ 0.58002544,  0.44551767,  0.07520678,  0.99676603,
         -0.8524942 ],
        [ 1.47101275, -1.63386489,  0.29305511, -2.23651731,
         -0.44797517],
        [ 2.1001467 ,  1.32506964,  0.03344443, -1.40475648,
         -1.85884513],
        [-0.29698703,  0.50682197,  2.36272313, -0.55494948,
          0.92003044]],

       [[ 0.47022139,  0.17816267,  0.49414841, -0.04924375,
          1.3658374 ],
        [-0.69638444,  1.19541601, -0.05604738,  0.38314451,
          0.04966896],
        [-0.09367271, -0.23102443, -1.31162772,  0.1537276 ,
          0.3986601 ],
        [-0.5247229 ,  0.70859833,  0.41970116, -0.54254133,
          1.59091854]],

       [[-1.52518514, -1.72684043, -0.69879025,  1.67320924,
          0.79593097],
        [-0.48241376,  0.96427609, -0.97584091,  1.13381381,
         -1.21627224],
        [ 0.17393277,  1.16790334,  0.06148158,  0.0296073 ,
          0.4326265 ],
        [-0.57824447,  1.59110481, -0.81189174, -0.58179106,
          0.14702895]]])

c = np.random.randint(100, 200, (3,4))

c
Out[7]: 
array([[197, 139, 150, 156],
       [150, 157, 127, 193],
       [127, 113, 139, 169]])

np.random.seed(10)  ##設(shè)定隨機(jī)數(shù)種子

np.random.randint(100, 200, (3,4))
Out[9]: 
array([[109, 115, 164, 128],
       [189, 193, 129, 108],
       [173, 100, 140, 136]])
函數(shù) 說明
shuffle(a) 根據(jù)數(shù)組a的第1軸進(jìn)行隨機(jī)排列,改變數(shù)組a
permutation(a) 根據(jù)數(shù)組a的第1軸進(jìn)行隨機(jī)排列,不改變數(shù)組a
choice(a[, size, replace, p]) 從一維數(shù)組a中以概率p抽取元素,形成size形狀的新數(shù)組。replace表示是否可以重用元素,默認(rèn)為False,即可重復(fù)使用。
a = np.random.randint(100, 200, (3,4))

a

Out[14]: 
array([[125, 113, 192, 186],
       [130, 130, 189, 112],
       [165, 131, 157, 136]])

np.random.shuffle(a)

a
Out[16]: 
array([[125, 113, 192, 186],
       [130, 130, 189, 112],
       [165, 131, 157, 136]])

np.random.permutation(a)
Out[17]: 
array([[165, 131, 157, 136],
       [125, 113, 192, 186],
       [130, 130, 189, 112]])

a
Out[18]: 
array([[125, 113, 192, 186],
       [130, 130, 189, 112],
       [165, 131, 157, 136]])

b = np.random.randint(100, 200, (8,))

b
Out[20]: array([123, 194, 111, 128, 174, 188, 109, 115])

np.random.choice(b, (3,2))
Out[21]: 
array([[111, 123],
       [109, 115],
       [123, 128]])

np.random.choice(b, (3,2), p= b/np.sum(b))
Out[22]: 
array([[109, 128],
       [194, 188],
       [194, 188]])

np.random.choice(b, (3,2), replace = False)
Out[23]: 
array([[194, 128],
       [115, 188],
       [111, 109]])
函數(shù) 說明
uniform(low, high, size) 產(chǎn)生具有均勻分布的數(shù)組,low為起始值,high為結(jié)束值,size為形狀
normal(loc, scale, size) 產(chǎn)生具有正態(tài)分布的數(shù)組,loc為均值,scale為標(biāo)準(zhǔn)差,size為形狀
poisson(lam, size) 產(chǎn)生具有泊松分布的數(shù)組,lam表示隨機(jī)事件發(fā)生概率,size為形狀
u = np.random.uniform(0, 10, (3,4))

u
Out[27]: 
array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],
       [1.31291053, 8.42817933, 6.59036304, 5.95439605],
       [4.36353698, 3.56250327, 5.87130925, 1.49471337]])

n = np.random.normal(10, 5, (3,4))

n
Out[29]: 
array([[ 8.17771928,  4.17423265,  3.28465058, 17.2669643 ],
       [10.00584724,  9.94039808, 13.57941572,  4.07115727],
       [ 6.81836048,  6.94593078,  3.40304302,  7.19135792]])

NumPy的統(tǒng)計(jì)函數(shù)

能對(duì)數(shù)組中的信息進(jìn)行統(tǒng)計(jì)運(yùn)算的函數(shù)

NumPy直接提供的統(tǒng)計(jì)類函數(shù)

函數(shù) 說明
sum(a, axis = None) 根據(jù)給定軸axis計(jì)算數(shù)組a相關(guān)元素之和,axis為整數(shù)或元祖,默認(rèn)不給定,即計(jì)算所有元素
mean(a, axis = None) 根據(jù)給定軸axis計(jì)算數(shù)組a相關(guān)元素的期望
average(a, axis = None, weights = None) 根據(jù)給定軸axis計(jì)算數(shù)組a相關(guān)元素的加權(quán)平均值
std(a, axis = None) 根據(jù)給定軸axis計(jì)算數(shù)組a相關(guān)元素的標(biāo)準(zhǔn)差
var(a, axis = None) 根據(jù)給定軸axis計(jì)算數(shù)組a相關(guān)元素的方差
import numpy as np

a = np.arange(15).reshape(3,5)

a
Out[3]: 
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

np.sum(a)
Out[5]: 105

np.mean(a, axis = -1)
Out[6]: array([ 2.,  7., 12.])

np.mean(a, axis = 0)
Out[7]: array([5., 6., 7., 8., 9.])

np.average(a, axis = 0, weights = [10, 5, 1])
Out[8]: array([2.1875, 3.1875, 4.1875, 5.1875, 6.1875])

np.std(a)
Out[9]: 4.320493798938574

np.var(a)
Out[10]: 18.666666666666668
函數(shù) 說明
min(a) max(a) 計(jì)算數(shù)組a中元素的最小值、最大值
argmin(a) argmax(a) 計(jì)算數(shù)組a中元素最小值、最大值的降一維后下標(biāo)
unravel_index(index, shape) 根據(jù)shape將一維下標(biāo)index轉(zhuǎn)換成多維下標(biāo)
ptp(a) 計(jì)算數(shù)組a中元素最大值與最小值的差
median(a) 計(jì)算數(shù)組a中元素的中位數(shù)(中值)
b = np.arange(15, 0, -1).reshape(3, 5)

b
Out[12]: 
array([[15, 14, 13, 12, 11],
       [10,  9,  8,  7,  6],
       [ 5,  4,  3,  2,  1]])

np.max(b)
Out[13]: 15

np.argmax(b)  ##扁平化后的下標(biāo)
Out[14]: 0

np.unravel_index(np.argmax(b), b.shape)  ##重塑成多維下標(biāo)
Out[15]: (0, 0)

np.ptp(b)
Out[16]: 14

np.median(b)  ##是一種運(yùn)算,得到的結(jié)果為浮點(diǎn)數(shù)
Out[17]: 8.0

NumPy 的梯度函數(shù)

np.grandient(f)|計(jì)算數(shù)組f中元素的梯度。當(dāng)f為多維時(shí),返回每個(gè)維度梯度
梯度:連續(xù)值之間的變化率,即斜率

a = np.random.randint(0, 20, (5))

a
Out[19]: array([ 0, 19, 17, 18,  0])

np.gradient(a)
Out[21]: array([ 19. ,   8.5,  -0.5,  -8.5, -18. ])  ##第二個(gè)數(shù)據(jù)的8.5,即8.5=(17-0)/2

c = np.random.randint(0, 50, (3,5))

c
Out[23]: 
array([[41, 35, 41, 44, 23],
       [33, 41, 42, 32, 30],
       [40,  1, 42, 37, 48]])

np.gradient(c)
Out[24]:  ##第一組數(shù)據(jù)表示最外層維度的梯度,第二組表示第二層維度的梯度
[array([[ -8. ,   6. ,   1. , -12. ,   7. ],
        [ -0.5, -17. ,   0.5,  -3.5,  12.5],
        [  7. , -40. ,   0. ,   5. ,  18. ]]),
 array([[ -6. ,   0. ,   4.5,  -9. , -21. ],
        [  8. ,   4.5,  -4.5,  -6. ,  -2. ],
        [-39. ,   1. ,  18. ,   3. ,  11. ]])]

單元3:實(shí)例1:圖像的手繪效果

圖像的數(shù)組表示

圖像的RGB色彩模式。即每個(gè)像素點(diǎn)由R、G、B三個(gè)顏色組成。
三個(gè)顏色通道的變化和疊加得到各種顏色,其中每個(gè)通道的取值范圍為0~255。

PIL庫(kù)

PIL,Python Image Library
具有強(qiáng)大的圖像處理能力的第三方庫(kù)。
命令行安裝方法:

pip install pillow

使用時(shí),引入Image類(對(duì)象)

from PIL import Image

圖像的數(shù)組表示

圖像是一個(gè)由數(shù)組組成的二維矩陣,每個(gè)元素是一個(gè)RGB值。
打開圖像:


示例圖像
from PIL import Image

import numpy as np

im = np.array(Image.open("a.jpg"))

print(im.shape, im.dtype)
(1000, 2275, 3) uint8

圖像是一個(gè)三維數(shù)組,維度為高度、寬度和像素的RGB值。

圖像的變換

  • 讀入圖像后,獲得像素RGB值,修改后保存為新的文件
a = np.array(Image.open("a.jpg"))

print(a.shape, a.dtype)
(1000, 2275, 3) uint8

b = [255, 255, 255] - a  ##生成每個(gè)RGB像素的補(bǔ)值,即反色

im = Image.fromarray(b.astype('uint8'))

im.save("b.jpg")

反色變換后:


反色變化后的圖像
a = np.array(Image.open("a.jpg").convert('L')) ##.convert('L')先講圖像變?yōu)榛叶?
b = 255 - a  ##對(duì)灰度圖像反色

im = Image.fromarray(b.astype('uint8'))

im.save("c.jpg")

灰度處理后反色的圖像:


灰度處理后反色的圖像
a = np.array(Image.open("a.jpg").convert('L'))

c = (100/255)*a + 150

im = Image.fromarray(c.astype('uint8'))

im.save("d.jpg")

對(duì)圖像進(jìn)行灰度處理+區(qū)間灰度變化后:


處理后
a = np.array(Image.open("a.jpg").convert('L'))

d = 255 * (a/255)**2 ##像素平方

im = Image.fromarray(d.astype('uint8'))

im.save("e.jpg")

像素灰度處理+像素平方后


處理后

“圖像手繪效果”實(shí)例分析

特征:

  • 黑白灰色
  • 邊界線條較重
  • 相同或相近色彩趨于白色
  • 略有光源效果

梯度的重構(gòu)

利用像素之間的梯度值和虛擬深度值對(duì)圖像進(jìn)行重構(gòu)。
根據(jù)灰度變化來模擬人類視覺的明暗程度。

depth = 10.  ##預(yù)設(shè)深度值為10,取值范圍0~100
grad = np.gradient(a)
grad_x, grad_y = grad  ##提取x和y方向的梯度值
grad_x = grad_x * depth/100.
grad_y = grad_y * depth/100.  ##根據(jù)深度調(diào)整x和y方向的梯度值

光源效果

示意圖

根據(jù)灰度變化來模擬人類視覺的遠(yuǎn)近程度

  • 設(shè)計(jì)一個(gè)位于圖像斜上方的虛擬光源
  • 建立柱坐標(biāo)系,光源相對(duì)于圖像的俯視角為Elevation,方位角為Azimuth
  • 建立光源對(duì)各點(diǎn)梯度值的影響函數(shù)
  • 運(yùn)算出各點(diǎn)的新的像素值
vec_el = np.pi/2.2
vec_az = np.pi/4.
dx = np.cos(vec_el) * np.cos(vec_az)  ##np.cos(vec_el)為單位光線在地平面上的投影長(zhǎng)度
dy = np.cos(vec_el) * np.sin(vec_az)
dz = np.sin(vec_el)  ##dx,dy,dz是光源對(duì)x、y、z三方向的影響程度

梯度歸一化

A = np.sqrt(grad_x**2 + grad_y**2 + 1.)  ##構(gòu)造x和y軸梯度的三維歸一化單位坐標(biāo)系
uni_x = grad_x/A  ##圖像平面內(nèi)的單位法向量
uni_y = grad_y/A
uni_z = 1./A
b = 255 * (dx*uni_x + dy*uni_y + dz*uni_z)  ##梯度與光源相互作用,將梯度轉(zhuǎn)化為灰度

圖像生成

b = b.clip(0, 255)  #限制灰度值為0~255
im = Image.fromarray(b.astype('uint8'))
im.save('output.jpg')  ##輸出圖像

代碼匯總:

##圖像手繪效果
from PIL import Image
import numpy as np

a = np.asarray(Image.open("a.jpg").convert('L')).astype('float')

depth = 10.  ##預(yù)設(shè)深度值為10,取值范圍0~100
grad = np.gradient(a)
grad_x, grad_y = grad  ##提取x和y方向的梯度值
grad_x = grad_x * depth/100.
grad_y = grad_y * depth/100.  ##根據(jù)深度調(diào)整x和y方向的梯度值

vec_el = np.pi/2.2
vec_az = np.pi/4.
dx = np.cos(vec_el) * np.cos(vec_az)  ##np.cos(vec_el)為單位光線在地平面上的投影長(zhǎng)度
dy = np.cos(vec_el) * np.sin(vec_az)
dz = np.sin(vec_el)  ##dx,dy,dz是光源對(duì)x、y、z三方向的影響程度

A = np.sqrt(grad_x**2 + grad_y**2 + 1.)  ##構(gòu)造x和y軸梯度的三維歸一化單位坐標(biāo)系
uni_x = grad_x/A  ##圖像平面內(nèi)的單位法向量
uni_y = grad_y/A
uni_z = 1./A
b = 255 * (dx*uni_x + dy*uni_y + dz*uni_z)  ##梯度與光源相互作用,將梯度轉(zhuǎn)化為灰度

b = b.clip(0, 255)  #限制灰度值為0~255
im = Image.fromarray(b.astype('uint8'))
im.save('output.jpg')  ##輸出圖像

輸出效果:


output.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)篇NumPy的主要對(duì)象是同種元素的多維數(shù)組。這是一個(gè)所有的元素都是一種類型、通過一個(gè)正整數(shù)元組索引的元素表格(...
    oyan99閱讀 5,288評(píng)論 0 18
  • 一、numpy概述 numpy(Numerical Python)提供了python對(duì)多維數(shù)組對(duì)象的支持:ndar...
    L_steven的貓閱讀 3,609評(píng)論 1 24
  • 1.ndarray數(shù)據(jù)結(jié)構(gòu) 2.創(chuàng)建ndarray 3.ndarray的數(shù)據(jù)類型 4.數(shù)組和標(biāo)量之間的運(yùn)算 5.基...
    一ke大白菜閱讀 1,881評(píng)論 0 6
  • 介紹 NumPy 是一個(gè) Python 包。 它代表 “Numeric Python”。 它是一個(gè)由多維數(shù)組對(duì)象和...
    喔蕾喔蕾喔蕾蕾蕾閱讀 1,865評(píng)論 0 5
  • 本章涉及的主題如下: 數(shù)據(jù)類型 數(shù)組類型 類型轉(zhuǎn)換 創(chuàng)建數(shù)組 索引 花式索引 切片 處理數(shù)組的形狀 2.1NumP...
    帶刺的小花_ea97閱讀 1,071評(píng)論 1 5

友情鏈接更多精彩內(nèi)容