Skr-Eric的數據分析課堂(一)--Numpy的介紹和Numpy基礎講解

數據分析

Matlab

Python

Numpy+Scipy+Pandas+Matplotlib

數值計算

科學應用

數據清洗

數據可視化

 

一、Numpy概述

1.Numpy是什麼?

1)基於C語言和Python接口的數值算法庫;

2)開源免費;

3)彌補了Python語言在數值計算方面的短板;

4)作爲常用科學計算工具的底層支撐;

 

2.Numpy的歷史

1)1995年,Numeric作爲Python語言數值計算擴展;

2)2001年,Scipy項目,Numarray矢量化操作;

3)2005年,Numeric+Numarray->Numpy;

4)2006年,Numpy脫離Scipy項目,獨立發佈;

 

3.Numpy的性能

1)簡化代碼編寫,提高開發效率;

2)通過優化底層實現,提高運行速度;

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime as dt
import numpy as np
n = 100000
start = dt.datetime.now()
A, B = [], []
for i in range(n):
    A.append(i ** 2)
    B.append(i ** 3)
C = []
for a, b in zip(A, B):
    C.append(a + b)
print((dt.datetime.now() - start).microseconds)
start = dt.datetime.now()
A, B = np.arange(n) ** 2, np.arange(n) ** 3
C = A + B
print((dt.datetime.now() - start).microseconds)

 

二、Numpy基礎

1.數組

1)Numpy中的數組是ndarray類實例化的對象,其中包括:

實際數據:數組的內容

元數據:對數組的描述

大部分對數組的操作僅僅是對元數據的操作,以此提高執行性能

2)Numpy中的數組必須是同質的,即所有元素的數據類型必須完全相同

3)通過基0的下標訪問數組中的元素

10: [0, 9]

4)dtype和shape屬性分別表示元素類型和維度

5)實例化

np.arange(起始值,終止值,步長)->[起始值,終止值)

默認起始值:0

默認步長:1

np.array(任何可被解釋爲數組的序列)->數組

6)類型轉換

數組.astype(目標類型)->轉換後的新數組

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = [[1, 2, 3],
     [4, 5, 6]]
print(type(a))
print(a)
b = np.array(a)
print(type(b))
print(b)
print(b.dtype, type(b[0][0]))
print(b.shape)
c = np.arange(1, 10, 2)
print(c, c.shape, c.dtype)
d = np.array([np.arange(1, 4), np.arange(4, 7)])
print(d, d.shape, d.dtype, sep='\n')
e = d.astype(float)
print(e, e.shape, e.dtype, sep='\n')
f = e.astype(str)
print(f, f.shape, f.dtype, sep='\n')
# <U32表示字符串,其中每個字符都
# 是小端字節序的32位Unicode字符

 

2.多維數組和元素索引

1)數組的維度表示爲一個元組:其中的元素按照從高維度數到低維度數的順序排列。

一維數組,6個元素:(6,)

二維數組,2行3列:(2, 3)

三維數組,2頁3行4列:(2, 3, 4)

2)通過下標運算符訪問數組中的元素

數組[頁標][行標][列標]

數組[頁標,行標,列標]

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.array([
    [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12]],
    [[13, 14, 15, 16],
     [17, 18, 19, 20],
     [21, 22, 23, 24]]])
print(a)
print(a.shape)
print(a[1][1][1])
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        for k in range(a.shape[2]):
            print(a[i, j, k])

 

3.數據類型

存儲形式,處理方式

1)內置類型

布爾型

bool_:True/False

整型

有符號:int8/int16/int32/int64

無符號:uint8/uint16/uint32/uint64

浮點型

float16/float32/float64

複數型

complex64/complex128

字符串

str_

2)複合類型

有多個相同或不同類型的字段組合而成的類型

np.array(..., dtype=複合類型)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.array([('ABC', [1, 2, 3]
               )],
             dtype='U3, 3i4')
print(a[0]['f0'],
      a[0]['f1'][0], a[0]['f1'][1], a[0]['f1'][2])
b = np.array([('ABC', [1, 2, 3])],
             dtype=[('fa', np.str_, 3),
                    ('fb', np.int32, 3)])
print(b[0]['fa'],
      b[0]['fb'][0], b[0]['fb'][1], b[0]['fb'][2])
c = np.array([('ABC', [1, 2, 3])],
             dtype={'names': ['fa', 'fb'],
                    'formats': ['U3', '3i4']})
print(c[0]['fa'],
      c[0]['fb'][0], c[0]['fb'][1], c[0]['fb'][2])
d = np.array([('ABC', [1, 2, 3])],
             dtype={'fa': ('U3', 0),
                    'fb': ('3i4', 16)})
print(d[0]['fa'],
      d[0]['fb'][0], d[0]['fb'][1], d[0]['fb'][2])
# 數組元素的字節數
print(c.itemsize, d.itemsize)
e = np.array([0x1234],
             dtype=('>u2', {'lo': ('u1', 0),
                            'hi': ('u1', 1)}))
print('{:x} {:x} {:x}'.format(
    e[0], e['lo'][0], e['hi'][0]))

3)類型字符碼

bool_: ?

有符號整型:i1/2/4/8

無符號整型:u1/2/4/8

浮點型:f2/4/8

複數型:c8/16

字符串:U<字符數>

日期時間:M

字節序(針對多字節整型):</>/[=]表示小端/大端/硬件

 

4.切片

數組[起始:終止:步長, 起始:終止:步長, ...]

默認起始:首(正步長)/尾(負步長)

默認終止:尾後(正步長)/首前(負步長)

默認步長:1

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.arange(1, 10)
print(a)  # 1 2 3 4 5 6 7 8 9
print(a[:3])  # 1 2 3
print(a[3:6])  # 4 5 6
print(a[6:])  # 7 8 9
print(a[::-1])  # 9 8 7 6 5 4 3 2 1
print(a[:-4:-1])  # 9 8 7
print(a[-4:-7:-1])  # 6 5 4
print(a[-7::-1])  # 3 2 1
print(a[::])  # 1 2 3 4 5 6 7 8 9
print(a[:])  # 1 2 3 4 5 6 7 8 9
# print(a[])  # 錯誤
print(a[::3])  # 1 4 7
print(a[1::3])  # 2 5 8
print(a[2::3])  # 3 6 9
b = np.arange(1, 25).reshape(2, 3, 4)
print(b)
print(b[:, 0, 0])  # 1 13
print(b[0])
print(b[0, :, :])
print(b[0, ...])
print(b[0, 1])  # 5 6 7 8
print(b[0, 1, ::2])  # 5 7
print(b[..., 1])
print(b[:, 1])
print(b[0, 1, 1::2])  # 6 8
print(b[0, :, -1])  # 4 8 12
print(b[0, ::-1, -1])  # 12 8 4
print(b[0, ::2, -1])  # 4 12
print(b[::-1, ::-1])
print(b[..., ::-1])
print(b[-1, 1:, 2:])

 

5.變維

1)視圖變維:元數據獨立,實際數據共享

數組.reshape(新維度)->新數組

   \_______________________/

                      |

              元數據獨立

            實際數據共享

元素數和維度數必須匹配

(8,) -> (2,4) -> (4,2) ->(2,2,2) -X-> (3,3)

數組.ravel()->一維數組

2)複製變維:元數據和實際數據都是獨立的

數組.flatten()->一維數組

   \_________________/

                 |

         元數據獨立

       實際數據獨立

3)就地變維:修改元數據的維度信息,不產生新的數組

數組.shape = 新維度

數組.resize(新維度)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.arange(1, 9)
print(a)
b = a.reshape(2, 4)
print(b)
c = b.reshape(2, 2, 2)
print(c)
d = c.ravel()
print(d)
e = c.flatten()
print(e)
a += 10
print(a, b, c, d, e, sep='\n')
a.shape = (2, 2, 2)
print(a)
a.resize(4, 2)
print(a)

 

6.組合拆分

1)垂直組合:沿着垂直方向組合兩個小的同維數組爲一個大數組

np.vstack((上, 下))

np.concatenate((上, 下), axis=0)

axis - 軸向,用維度的下標表示

                0  1

shape->(2, 4)

2)水平組合:沿着水平方向組合兩個小的同維數組爲一個大數組

np.hstack((左, 右))

np.concatenate((左, 右), axis=1)

3)深度組合:沿着縱深方向組合兩個小的同維數組爲一個大數組

np.dstack((前, 後))

4)行組合:以兩個一維數組按照行的方式組合成一個二維數組

np.row_stack((上, 下))

5)列組合:以兩個一維數組按照列的方式組合成一個二維數組

np.column_stack((左, 右))

np.c_[左, 右]

6)垂直拆分:將一個大的數組沿着垂直方向拆分成若個小的同維數組

np.vsplit(被拆分數組, 拆分份數)

np.split(被拆分數組, 拆分份數, axis=0)

7)水平拆分:將一個大的數組沿着水平方向拆分成若個小的同維數組

np.hsplit(被拆分數組, 拆分份數)

np.split(被拆分數組, 拆分份數, axis=1)

8)深度拆分:將一個大的數組沿着縱深方向拆分成若個小的同維數組

np.dsplit(被拆分數組, 拆分份數)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.arange(11, 20).reshape(3, 3)
print(a)
b = a + 10
print(b)
#c = np.vstack((a, b))
c = np.concatenate((a, b), axis=0)
print(c)
#d = np.hstack((a, b))
d = np.concatenate((a, b), axis=1)
print(d)
e = np.dstack((a, b))
print(e)
f, g = a[0], b[0]
print(f, g)
h = np.row_stack((f, g))
print(h)
i = np.column_stack((f, g))
print(i)
#j, k, l = np.vsplit(c, 3)
j, k, l = np.split(c, 3, axis=0)
print(j, k, l, sep='\n')
#m, n, o = np.hsplit(d, 3)
m, n, o = np.split(d, 3, axis=1)
print(m, n, o, sep='\n')
p, q = np.dsplit(e, 2)
print(p, q, sep='\n')
print(p.T[0].T, q.T[0].T, sep='\n')

 

7.ndarray的屬性

dtype - 元素的數據類型

shape - 數組的維度

ndim - 數組的維數,len(shape)

size - 數組的元素數,shape中元素相乘

itemsize - 元素字節數,與dtype相關

nbytes - 總字節數,size x itemsize

T - 轉置視圖

1 2 3

4 5 6

變維

1 2

3 4

5 6

轉置

1 4

2 5

3 6

real - 複數數組的實部視圖

imag - 複數數組的虛部視圖

flat - 扁平迭代器

 

8.ndarray<=>list

np.array(列表)->數組

數組.tolist()->列表

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
a = np.array([
    [1 + 1j, 2 + 4j, 3 + 7j],
    [4 + 2j, 5 + 5j, 6 + 8j],
    [7 + 3j, 8 + 6j, 9 + 9j]])
print(a.dtype, a.dtype.str)
print(a.shape)
print(a.ndim, len(a.shape))
print(a.size, len(a))
print(a.itemsize)
print(a.nbytes, a.size * a.itemsize)
print(a.T)
print(a.real)
print(a.imag)
for row in a:
    for col in row:
        print(col)
for v in a.flat:
    print(v)
b = [10, 20, 30, 40, 50]
print(b, type(b))
c = np.array(b)  # list->ndarray
print(c, type(c))
d = c.tolist()  # ndarray->list
print(d, type(d))
e = c  # 淺拷貝
print(e, type(e))
print(id(e), id(c))
e += 1
print(e, c)
f = c.copy()  # 深拷貝
print(f, type(f))
print(id(f), id(c))
f += 1
print(f, c)

 

 

 

 

想要看更多的課程請微信關注SkrEric的編程課堂

發佈了107 篇原創文章 · 獲贊 152 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章