Python?NumPy教程之數(shù)組的創(chuàng)建詳解
使用 List 創(chuàng)建數(shù)組
數(shù)組用于在一個變量中存儲多個值。Python 沒有對數(shù)組的內(nèi)置支持,但可以使用 Python 列表代替。
例子 :
arr = [1, 2, 3, 4, 5] arr1 = ["geeks", "for", "geeks"]
# 用于創(chuàng)建數(shù)組的 Python 程序 # 使用列表創(chuàng)建數(shù)組 arr=[1, 2, 3, 4, 5] for i in arr: print(i)
輸出:
1
2
3
4
5
使用數(shù)組函數(shù)創(chuàng)建數(shù)組
array(data type, value list) 函數(shù)用于創(chuàng)建一個數(shù)組,其參數(shù)中指定了數(shù)據(jù)類型和值列表。
例子 :
# 演示 array() 工作的 Python 代碼 # 為數(shù)組操作導入“array” import array # 用數(shù)組值初始化數(shù)組 # 用有符號整數(shù)初始化數(shù)組 arr = array.array('i', [1, 2, 3]) # 打印原始數(shù)組 print ("The new created array is : ",end="") for i in range (0,3): print (arr[i], end=" ") print ("\r")
輸出:
The new created array is : 1 2 3 1 5
使用 numpy 方法創(chuàng)建數(shù)組
NumPy 提供了幾個函數(shù)來創(chuàng)建具有初始占位符內(nèi)容的數(shù)組。這些最大限度地減少了增長陣列的必要性,這是一項昂貴的操作。例如:np.zeros、np.empty等。
numpy.empty(shape, dtype = float, order = 'C'): 返回給定形狀和類型的新數(shù)組,具有隨機值。
# 說明 numpy.empty 方法的 Python 代碼 import numpy as geek b = geek.empty(2, dtype = int) print("Matrix b : \n", b) a = geek.empty([2, 2], dtype = int) print("\nMatrix a : \n", a) c = geek.empty([3, 3]) print("\nMatrix c : \n", c)
輸出 :
Matrix b :
[ 0 1079574528]
Matrix a :
[[0 0]
[0 0]]
Matrix a :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
numpy.zeros(shape, dtype = None, order = 'C'): 返回給定形狀和類型的新數(shù)組,帶零。
# 說明 numpy.zeros 方法的 Python 程序 import numpy as geek b = geek.zeros(2, dtype = int) print("Matrix b : \n", b) a = geek.zeros([2, 2], dtype = int) print("\nMatrix a : \n", a) c = geek.zeros([3, 3]) print("\nMatrix c : \n", c)
輸出 :
Matrix b :
[0 0]
Matrix a :
[[0 0]
[0 0]]
Matrix c :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
重塑數(shù)組
我們可以使用reshape
方法來重塑數(shù)組??紤]一個形狀為 (a1, a2, a3, ..., aN) 的數(shù)組。我們可以重新整形并將其轉換為另一個形狀為 (b1, b2, b3, ..., bM) 的數(shù)組。
唯一需要的條件是: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM 。(即數(shù)組的原始大小保持不變。)
numpy.reshape(array, shape, order = 'C'): 在不更改數(shù)組數(shù)據(jù)的情況下對數(shù)組進行整形。
# 說明 numpy.reshape() 方法的 Python 程序 import numpy as geek array = geek.arange(8) print("Original array : \n", array) # 具有 2 行和 4 列的形狀數(shù)組 array = geek.arange(8).reshape(2, 4) print("\narray reshaped with 2 rows and 4 columns : \n", array) # 具有 2 行和 4 列的形狀數(shù)組 array = geek.arange(8).reshape(4 ,2) print("\narray reshaped with 2 rows and 4 columns : \n", array) # 構造 3D 數(shù)組 array = geek.arange(8).reshape(2, 2, 2) print("\nOriginal array reshaped to 3D : \n", array)
輸出 :
Original array :
[0 1 2 3 4 5 6 7]
array reshaped with 2 rows and 4 columns :
[[0 1 2 3]
[4 5 6 7]]
array reshaped with 2 rows and 4 columns :
[[0 1]
[2 3]
[4 5]
[6 7]]
Original array reshaped to 3D :
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
為了創(chuàng)建數(shù)字序列,NumPy 提供了一個類似于 range 的函數(shù),它返回數(shù)組而不是列表。
arange返回給定間隔內(nèi)均勻分布的值。步長是指定的。
linspace 返回給定間隔內(nèi)均勻分布的值。編號_ 的元素被返回。
arange([start,] stop[, step,][, dtype]): 根據(jù)間隔返回一個具有均勻間隔元素的數(shù)組。提到的間隔是半開的,即[開始,停止)
# 說明 numpy.arange 方法的 Python 編程 import numpy as geek print("A\n", geek.arange(4).reshape(2, 2), "\n") print("A\n", geek.arange(4, 10), "\n") print("A\n", geek.arange(4, 20, 3), "\n")
輸出 :
A
[[0 1]
[2 3]]
A
[4 5 6 7 8 9]
A
[ 4 7 10 13 16 19]
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None): 在間隔中均勻返回數(shù)字空間。類似于 arange 但不是 step 它使用樣本編號。
# 說明 numpy.linspace 方法的 Python 編程 import numpy as geek # 重新設置為 True print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n") # 長期評估 sin() x = geek.linspace(0, 2, 10) print("A\n", geek.sin(x))
輸出 :
B
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
A
[ 0. 0.22039774 0.42995636 0.6183698 0.77637192 0.8961922
0.9719379 0.99988386 0.9786557 0.90929743]
展平數(shù)組
我們可以使用展平方法將數(shù)組的副本折疊成一維。它接受 order 參數(shù)。默認值為“C”(用于行優(yōu)先順序)。使用“F”表示列主要順序。
numpy.ndarray.flatten(order = 'C') :返回折疊成一維的數(shù)組的副本。
# 說明 numpy.flatten() 方法的 Python 程序 import numpy as geek array = geek.array([[1, 2], [3, 4]]) # 使用扁平化方法 array.flatten() print(array) #使用扁平化方法 array.flatten('F') print(array)
輸出 :
[1, 2, 3, 4]
[1, 3, 2, 4]
在 Numpy 中創(chuàng)建數(shù)組的方法
功能 | 描述 |
---|---|
empty() | 返回給定形狀和類型的新數(shù)組,而不初始化條目 |
empty_like() | 返回與給定數(shù)組具有相同形狀和類型的新數(shù)組 |
eye() | 返回一個二維數(shù)組,其中對角線為 1,其他位置為 0。 |
identity() | 返回標識數(shù)組 |
ones() | 返回一個給定形狀和類型的新數(shù)組,用一個填充 |
one_like() | 返回與給定數(shù)組具有相同形狀和類型的數(shù)組 |
zeros() | 返回給定形狀和類型的新數(shù)組,用零填充 |
zeros_like() | 返回與給定數(shù)組具有相同形狀和類型的零數(shù)組 |
full_like() | 返回與給定數(shù)組具有相同形狀和類型的完整數(shù)組。 |
array() | 創(chuàng)建一個數(shù)組 |
asarray() | 將輸入轉換為數(shù)組 |
asanyarray() | 將輸入轉換為 ndarray,但通過 ndarray 子類 |
ascontiguousarray() | 返回內(nèi)存中的連續(xù)數(shù)組(C 順序) |
asmatrix() | 將輸入解釋為矩陣 |
copy() | 返回給定對象的數(shù)組副本 |
frombuffer() | 將緩沖區(qū)解釋為一維數(shù)組 |
fromfile() | 從文本或二進制文件中的數(shù)據(jù)構造數(shù)組 |
fromfunction() | 通過在每個坐標上執(zhí)行函數(shù)來構造數(shù)組 |
fromiter() | 從可迭代對象創(chuàng)建一個新的一維數(shù)組 |
fromstring() | 從字符串中的文本數(shù)據(jù)初始化的新一維數(shù)組 |
loadtxt() | 從文本文件加載數(shù)據(jù) |
arange() | 在給定間隔內(nèi)返回均勻間隔的值 |
linspace() | 在指定的時間間隔內(nèi)返回均勻分布的數(shù)字 |
logspace() | 返回在對數(shù)刻度上均勻分布的數(shù)字 |
geomspace() | 返回在對數(shù)尺度上均勻分布的數(shù)字(幾何級數(shù)) |
meshgrid() | 從坐標向量返回坐標矩陣 |
mgrid() | nd_grid 實例,它返回一個密集的多維“網(wǎng)格” |
ogrid() | nd_grid 實例,它返回一個開放的多維“meshgrid” |
diag() | 提取對角線或構造對角線數(shù)組 |
diagflat() | 創(chuàng)建一個二維數(shù)組,將扁平化輸入作為對角線 |
tri() | 一個數(shù)組,在給定的對角線處和下方都有一個,在其他地方有零 |
tril() | 數(shù)組的下三角形 |
triu() | 數(shù)組的上三角形 |
vander() | 生成范德蒙德矩陣 |
mat() | 將輸入解釋為矩陣 |
bmat() | 從字符串、嵌套序列或數(shù)組構建矩陣對象 |
到此這篇關于Python NumPy教程之數(shù)組的創(chuàng)建詳解的文章就介紹到這了,更多相關Python NumPy數(shù)組創(chuàng)建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python基礎之python循環(huán)控制語句break/continue詳解
Python中提供了兩個關鍵字用來控制循環(huán)語句,分別是break和continue,接下來通過兩個案例來區(qū)分這兩個控制語句的不同,感興趣的朋友一起看看吧2021-09-09Python實現(xiàn)字典排序、按照list中字典的某個key排序的方法示例
這篇文章主要介紹了Python實現(xiàn)字典排序、按照list中字典的某個key排序的方法,涉及Python字典與列表排序相關操作技巧,需要的朋友可以參考下2018-12-12python3+PyQt5 自定義窗口部件--使用窗口部件樣式表的方法
今天小編就為大家分享一篇python3+PyQt5 自定義窗口部件--使用窗口部件樣式表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06淺析python實現(xiàn)布隆過濾器及Redis中的緩存穿透原理
本文帶你了解了位圖的實現(xiàn),布隆過濾器的原理及 Python 中的使用,以及布隆過濾器如何應對 Redis 中的緩存穿透,相信你對布隆過濾器已經(jīng)有了一定的認識2021-09-09使用python為mysql實現(xiàn)restful接口
這篇文章主要介紹了使用python為mysql實現(xiàn)restful接口的相關資料,需要的朋友可以參考下2018-01-01Python實現(xiàn)自動發(fā)消息自定義內(nèi)容的操作代碼
這篇文章主要介紹了Python實現(xiàn)自動發(fā)消息自定義內(nèi)容的操作代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08