初始NumPy庫(一)

在學習數據分析時,NumPy做爲最基礎的數據分析庫,咱們可以熟練的掌握它是學習數據分析的必要條件。接下來就讓咱們學習該庫吧。python

學習NumPy庫的環境:                                                                                                                                                     數組

python:3.6.6編輯器

編輯器:pycharm函數

NumPy安裝:在cmd命令下,直接使用pip語句,pip install NumPy便可!工具

NumPy是使用Python進行科學計算的基本軟件包。它主要包含一下內容:學習

  • 有一個強大的N維數組對象ndarray;
  • 擁有複雜的廣播功能函數;
  • 整合C/C++和Fortran代碼的工具;
  • 線性代數、傅里葉變換、隨機數生成等功能。

具體的內容可參考NumPy官網信息:點擊官網優化

 Python基礎數據類型中是沒有數組概念,NumPy庫可以很好的知足了數組缺失,數組對象的優勢有:spa

  1. 數組對象能夠去掉元素間運算的循環,使一維向量更像單個數據;
  2. 設置專門的數組對象,通過優化,能夠提高這類應用的運算速度;
  3. 數組對象採用相同的數據類型,有助於節省運算和存儲空間。

 1、ndarray的介紹code

一、ndarray的構成:對象

ndarray有兩部分構成,一是實際數據;二是描述這些數據的元數據(數據維度、數據類型等)。ndarray數組通常要求全部元素類型相同(同質),數組的下標從0開始。

其中軸(axis):保存數組的維度;秩(rank):軸的數量

二、ndarray對象的屬性:

.ndim:秩,即軸的數量或維度的數量;

.shape:ndarray對象的尺度,對於矩陣,n行m列;

.size:ndarray對象元素的個數,至關於.shape的n*m的值;

.dtype:ndarray對象的元素類型;
.itemsize:ndarray對象中每一個元素的大小,以字節爲單位。

 以下舉例說明:

求數組a的平方和數組b的立方和:

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())
[729 513 347 225 141]

以下是ndarray屬性的練習:

import numpy as np
a = np.array([[0, 1, 2, 3, 4], [9, 8, 7, 6, 5]])
print(a)
print(a.ndim)
print(type(a))
print(a.shape)
print(a.size)
print(a.dtype)
print(a.itemsize)

[[0 1 2 3 4]
 [9 8 7 6 5]]
2
<class 'numpy.ndarray'>
(2, 5)
10
int32
4

 三、ndarray數組的建立方法:

  • 從python中的列表、元組等類型建立ndarray;
  • 使用NumPy中函數建立ndarray數組,如arange,ones,zeros;
  • 從字節流(raw bytes)中建立ndarray數組;
  • 從文件中讀取特定的格式建立ndarray數組。

以下舉例說明:

①、從python中的列表、元組等類型建立ndarray數組:

 

 1 import numpy as np
 2 x = np.array([[1, 2], [9, 8], (0.1, 0.2)])
 3 print(x)
 4 print(x.shape)
 5 print(x.size)
 6 
 7 [[1.  2. ]
 8  [9.  8. ]
 9  [0.1 0.2]]
10 (3, 2)
11 6

 

②、使用NumPy中函數建立ndarray數組:

np.arange(n)            相似range()函數,返回ndarray類型,元素從0到n-1;
np.ones(shape) 根據shape生成一個全1數組,shape是元組類型;
np.zeros(shape) 根據shape生成一個全0數組,shape是元組類型;
np.full(shape, val) 根據shape生成一個數組,每一個元素值都是val;
np.eye(n) 建立一個正方的n*n單位矩陣,對角線爲1,其他爲0;
相似函數
np.ones_like(a) 根據數組a的形狀生成一個全1數組
np.zeros_like(a) 根據數組a的形狀生成一個全0數組
np.full_like(a,val) 根據數組a的形狀生成一個數組,每一個元素值都是val

np.linspace() 根據起止數據等間距地填充數據,造成數組
np.concatenate() 將兩個或多個數組合併成一個新的數組
 
 1 import numpy as np
 2 
 3 print(np.arange(10))
 4 print(np.ones((3, 6)))
 5 print(np.zeros((3, 6), dtype=np.int32))
 6 print(np.eye(5))
 7 
 8 [0 1 2 3 4 5 6 7 8 9]
 9 [[1. 1. 1. 1. 1. 1.]
10  [1. 1. 1. 1. 1. 1.]
11  [1. 1. 1. 1. 1. 1.]]
12 [[0 0 0 0 0 0]
13  [0 0 0 0 0 0]
14  [0 0 0 0 0 0]]
15 [[1. 0. 0. 0. 0.]
16  [0. 1. 0. 0. 0.]
17  [0. 0. 1. 0. 0.]
18  [0. 0. 0. 1. 0.]
19  [0. 0. 0. 0. 1.]]
 
 1 #不限制數據類型的時候生成的是浮點數
 2 a = np.linspace(1, 10, 4)
 3 print(a)
 4 # endpoint 是指最後一個元素是不是生成的四個元素中的一個
 5 b = np.linspace(1, 10, 4, endpoint=False)
 6 print(b)
 7 c = np.concatenate((a, b))
 8 print(c)
 9 
10 
11 [ 1.  4.  7. 10.]
12 [1.   3.25 5.5  7.75]
13 [ 1.    4.    7.   10.    1.    3.25  5.5   7.75]

 四、數組的變換

對於建立後的ndarray數組,能夠對其進行維度變換和元素類型變換:

ndarray數組的維度變換
.reshape(shape) 不改變數組元素,返回一個shape形狀的數組,原數組不變
.resize(shape) 與.reshape()功能一致,但修改原數組
.swapaxes(ax1, ax2) 將數組n個維度中的兩個維度進行調換
.flatten() 對數組進行降維,返回摺疊後的一維數組,原數組不變

 1 a = np.ones((2, 3, 4), dtype=np.int32)
 2 print(a)
 3 print(a.reshape((3, 8)))
 4 print(a.resize((3, 8)))
 5 print(a)
 6 print(a.flatten())
 7 
 8 [[[1 1 1 1]
 9   [1 1 1 1]
10   [1 1 1 1]]
11 
12  [[1 1 1 1]
13   [1 1 1 1]
14   [1 1 1 1]]]
15 [[1 1 1 1 1 1 1 1]
16  [1 1 1 1 1 1 1 1]
17  [1 1 1 1 1 1 1 1]]
18 None
19 [[1 1 1 1 1 1 1 1]
20  [1 1 1 1 1 1 1 1]
21  [1 1 1 1 1 1 1 1]]
22 [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

# astype()方法必定會建立新的數組(原始數組的一個拷貝),即便兩個類型一致

 1 a = np.ones((2, 3, 4),  dtype=np.int)
 2 print(a)
 3 b = a.astype(np.float)
 4 print(b)
 5 
 6 [[[1 1 1 1]
 7   [1 1 1 1]
 8   [1 1 1 1]]
 9 
10  [[1 1 1 1]
11   [1 1 1 1]
12   [1 1 1 1]]]
13 [[[1. 1. 1. 1.]
14   [1. 1. 1. 1.]
15   [1. 1. 1. 1.]]
16 
17  [[1. 1. 1. 1.]
18   [1. 1. 1. 1.]
19   [1. 1. 1. 1.]]]

五、ndarray數組向列表的轉換

 1 import numpy as np
 2 
 3 a = np.full((2, 3, 4), 25, dtype=np.int32)
 4 print(a)
 5 print(a.tolist())
 6 
 7 [[[25 25 25 25]
 8   [25 25 25 25]
 9   [25 25 25 25]]
10 
11  [[25 25 25 25]
12   [25 25 25 25]
13   [25 25 25 25]]]
14 [[[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]], [[25, 25, 25, 25], [25, 25, 25, 25], [25, 25, 25, 25]]]
相關文章
相關標籤/搜索