www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 電源 > 功率器件
[導(dǎo)讀]NumPy是Python語(yǔ)言的一個(gè)擴(kuò)展包。支持多維數(shù)組與矩陣運(yùn)算,此外也針對(duì)數(shù)組運(yùn)算提供大量的數(shù)學(xué)函數(shù)庫(kù)。NumPy提供了與Matlab相似的功能與操作方式,因?yàn)閮烧呓詾橹弊g語(yǔ)言。Nu

NumPy是Python語(yǔ)言的一個(gè)擴(kuò)展包。支持多維數(shù)組與矩陣運(yùn)算,此外也針對(duì)數(shù)組運(yùn)算提供大量的數(shù)學(xué)函數(shù)庫(kù)。NumPy提供了與Matlab相似的功能與操作方式,因?yàn)閮烧呓詾橹弊g語(yǔ)言。

NumPy通常與SciPy(Scientific Python)和Matplotlib(繪圖庫(kù))一起使用,這種組合廣泛用于替代Matlab,是一個(gè)流行的技術(shù)平臺(tái)。

NumPy中定義的最重要的對(duì)象是稱(chēng)為ndarray的N維數(shù)組類(lèi)型。它描述相同類(lèi)型的元素集合,可以使用基于零的索引訪問(wèn)集合中元素。基本的ndarray是使用NumPy中的數(shù)組函數(shù)創(chuàng)建的: numpy.array。

NumPy支持比Python更多種類(lèi)的數(shù)值類(lèi)型。NumPy數(shù)值是dtype(數(shù)據(jù)類(lèi)型)對(duì)象的實(shí)例,每個(gè)對(duì)象具有唯一的特征。

以下對(duì)ndarray的介紹來(lái)自于 https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html (此鏈接可查看ndarray中包含的各種函數(shù)介紹):

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.

As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array, and via the methods and attributes of the ndarray.

Different ndarrays can share the same data, so that changes made in one ndarray may be visible in another. That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the buffer or array interfaces.

以下是NumPy簡(jiǎn)單使用例子(參考: https://wizardforcel.gitbooks.io/ts-numpy-tut/content/ ):

import numpy as np

from matplotlib import pyplot as plt

# 一維

a = np.array([1, 2, 3]); print(a) # [1 2 3]

# 等間隔數(shù)字的數(shù)組

b = np.arange(10); print(b) # [0 1 2 3 4 5 6 7 8 9]

# 二維

c = np.array([[1, 2], [3, 4]]); print(c) # [[1 2]

# [3 4]]

# ndmin指定返回?cái)?shù)組的最小維數(shù)

d = np.array([1, 2, 3, 4, 5]); print(d) # [1 2 3 4 5]

e = np.array([1, 2, 3, 4, 5], ndmin=2); print(e) # [[1 2 3 4 5]]

# dtype:數(shù)組的所需數(shù)據(jù)類(lèi)型

f = np.array([1, 2, 3], dtype=complex); print(f) # [1.+0.j 2.+0.j 3.+0.j]

# 使用數(shù)組標(biāo)量類(lèi)型

dt = np.dtype(np.int32); print(dt) # int32

# int8,int16,int32,int64可替換為等價(jià)的字符串'i1', 'i2', 'i4', 'i8'

dt = np.dtype('i8'); print(dt) # int64

# 調(diào)整數(shù)組shape

a = np.array([[1, 2, 3], [4, 5, 6]]); print(a); # [[1 2 3]

# [4 5 6]]

a.shape = (3, 2); print(a) # [[1 2]

# [3 4]

# [5 6]]

a = np.array([[1, 2, 3], [4, 5, 6]]); b = a.reshape(3, 2); print(b) # [[1 2]

# [3 4]

# [5 6]]

# ndim:返回?cái)?shù)組的維數(shù)

a = np.arange(24); print(a.ndim) # 1

# numpy.reshape: 在不改變數(shù)據(jù)的條件下修改形狀

b = a.reshape(2, 4, 3); print(b.ndim) # 3

# itemsize:返回?cái)?shù)組中每個(gè)元素的字節(jié)單位長(zhǎng)度

a = np.array([1, 2, 3, 4], dtype=np.int8); print(a.itemsize) # 1

a = np.array([1, 2, 3, 4], dtype=np.float32); print(a.itemsize) # 4

# 空數(shù)組

x = np.empty([3, 2], dtype='i1'); print(x) # 數(shù)組x的元素為隨機(jī)值,因?yàn)樗鼈兾闯跏蓟?/p>

# 含有5個(gè)0的數(shù)組,若不指定類(lèi)型,則默認(rèn)為float

x = np.zeros(5, dtype=np.int); print(x) # [0 0 0 0 0]

# 含有6個(gè)1的二維數(shù)組,若不指定類(lèi)型,則默認(rèn)為float

x = np.ones([2, 3], dtype=int); print(x) # [[1 1 1]

# [1 1 1]]

# 將列表轉(zhuǎn)換為ndarray

x = [1, 2, 3]

a = np.asarray(x, dtype=float); print(a) # [1. 2. 3.]

# 將元組轉(zhuǎn)換為ndarray

x = (1, 2, 3)

a = np.asarray(x, dtype=complex); print(a) # [1.+0.j 2.+0.j 3.+0.j]

# 使用內(nèi)置的range()函數(shù)創(chuàng)建列表對(duì)象

x = range(5); print(x) # range(0, 5)

# 從列表中獲得迭代器

it = iter(x); print(it) #

# 使用迭代器創(chuàng)建ndarray, fromiter函數(shù)從任何可迭代對(duì)象構(gòu)建一個(gè)ndarray對(duì)象,返回一個(gè)新的一維數(shù)組

y = np.fromiter(it, dtype=float); print(y) # [0. 1. 2. 3. 4.]

# arange函數(shù)返回ndarray對(duì)象,包含給定范圍內(nèi)的等間隔值

# numpy.arange(start, stop, step, dtype), start起始值,默認(rèn)為0;stop終止值,不包含; step間隔,默認(rèn)為1

x = np.arange(4, dtype=float); print(x) # [0. 1. 2. 3.]

x = np.arange(10, 20, 2); print(x) # [10 12 14 16 18]

# numpy.linspace,此函數(shù)類(lèi)似于arange,在此函數(shù)中,指定了范圍之間的均勻間隔數(shù)量,而不是步長(zhǎng)

# numpy.linspace(start, stop, num, endpoint, retstep, dtype)

# start,起始值;stop,終止值,如果endpoint為true,該值包含于序列中;num,要生成的等間隔樣例數(shù)量,默認(rèn)為50;

# endpoint,序列中是否包含stop值,默認(rèn)為true;retstep,如果為true,返回樣例,以及連續(xù)數(shù)字之間的步長(zhǎng)

x = np.linspace(10, 20, 5); print(x) # [10. 12.5 15. 17.5 20.]

x = np.linspace(10, 20, 5, endpoint=False); print(x) # [10. 12. 14. 16. 18.]

x = np.linspace(1,2,5, retstep=True); print(x) # (array([ 1. , 1.25, 1.5 , 1.75, 2. ]), 0.25)

# numpy.logspace,此函數(shù)返回ndarray對(duì)象,包含在對(duì)數(shù)刻度上均勻分布的數(shù)字.刻度的開(kāi)始和結(jié)束端點(diǎn)是某個(gè)底數(shù)的冪,通常為10

# numpy.logscale(start, stop, num, endpoint, base, dtype)

# base,對(duì)數(shù)空間的底數(shù),默認(rèn)為10;其它參數(shù)同numpy.linspace

a = np.logspace(1.0, 2.0, num=5); print(a) # [10. 17.7827941 31.6227766 56.23413252 100.]

a = np.logspace(1, 10, num=5, base=2); print(a) # [2. 9.51365692 45.254834 215.2694823 1024.]

# ndarray對(duì)象的內(nèi)容可以通過(guò)索引或切片來(lái)訪問(wèn)和修改,就像Python的內(nèi)置容器對(duì)象一樣;

# 基本切片:通過(guò)將start、stop和step參數(shù)提供給內(nèi)置的slice函數(shù)來(lái)構(gòu)造一個(gè)Python slice對(duì)象,用來(lái)提前數(shù)組的一部分

a = np.arange(10); s = slice(2,7,2); print(a[s]) # [2 4 6]

# 通過(guò)將由冒號(hào)分隔的切片參數(shù)(start:stop:step)直接提供給ndarray對(duì)象,也可以獲得相同的結(jié)果

a = np.arange(10); b = a[2:7:2]; print(b) # [2 4 6]

a = np.arange(10); b = a[2:]; print(b) # [2 3 4 5 6 7 8 9]

a = np.arange(10); b = a[2:5]; print(b) # [2 3 4]

# 切片還可以包括省略號(hào)(...),來(lái)使選擇元組的長(zhǎng)度與數(shù)組的維度相同

a = np.array([[1,2,3],[3,4,5],[4,5,6]])

b = a[..., 1]; print(b) # [2 4 5]

c = a[1, ...]; print(c) # [3 4 5]

# 高級(jí)索引:如果一個(gè)ndarray是非元組序列,數(shù)據(jù)類(lèi)型為整數(shù)或布爾值的ndarray,或者至少一個(gè)元素為

# 序列對(duì)象的元組,我們就能夠用它來(lái)索引ndarray,高級(jí)索引始終返回?cái)?shù)據(jù)的副本

# 高級(jí)索引:整數(shù):基于N維索引來(lái)獲取數(shù)組中任意元素

x = np.array([[1, 2], [3, 4], [5, 6]])

# y中包括數(shù)組x中(0,0), (1,1), (2,0)位置處的元素

y = x[[0,1,2], [0,1,0]]; print(y) # [1 4 5]

# 高級(jí)索引:布爾值:當(dāng)結(jié)果對(duì)象是布爾運(yùn)算的結(jié)果時(shí),將使用此類(lèi)型的高級(jí)索引

x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])

y = x[x > 5]; print(y) # [6 7 8 9 10 11]

# ~(取補(bǔ)運(yùn)算符)來(lái)過(guò)濾NaN

x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])

y = x[~np.isnan(x)]; print(y) # [1. 2. 3. 4. 5.]

# 從數(shù)組中過(guò)濾掉非復(fù)數(shù)元素

x = np.array([1, 2+6j, 5, 3.5+5j])

y = x[np.iscomplex(x)]; print(y) # [2.0+6.j 3.5+5.j]

# 廣播:是指NumPy在算術(shù)運(yùn)算期間處理不同形狀的數(shù)組的能力, 對(duì)數(shù)組的算術(shù)運(yùn)算通常在相應(yīng)的元素上運(yùn)行

# 如果兩個(gè)數(shù)組的維數(shù)不相同,則元素到元素的操作是不可能的。然而,在NumPy中仍然可以對(duì)形狀不相似的數(shù)組進(jìn)行操作,因?yàn)樗鼡碛袕V播功能。

# 較小的數(shù)組會(huì)廣播到較大數(shù)組的大小,以便使它們的形狀可兼容

a = np.array([1, 2, 3, 4])

b = np.array([10, 20, 30, 40])

c = a * b; print(c) # [10 40 90 160]

a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0]])

b = np.array([1.0,2.0,3.0])

c = a + b; print(c) # [[1.0 2.0 3.0]

# [11. 12. 13.]]

# 數(shù)組上的迭代:NumPy包包含一個(gè)迭代器對(duì)象numpy.nditer。它是一個(gè)有效的多維迭代器對(duì)象,可以用于在數(shù)組上進(jìn)行迭代。

# 數(shù)組的每個(gè)元素可使用Python的標(biāo)準(zhǔn)Iterator接口來(lái)訪問(wèn)

a = np.arange(0, 60, 5)

a = a.reshape(3,4)

for x in np.nditer(a):

print(x, end=' ') # 0 5 10 15 20 25 30 35 40 45 50 55

print('n')

# 修改數(shù)組的值: nditer對(duì)象的一個(gè)可選參數(shù)op_flags,其默認(rèn)值為只讀,但可以設(shè)置為讀寫(xiě)或只寫(xiě)模式.這將允許使用此迭代器修改數(shù)組元素

for x in np.nditer(a, op_flags=['readwrite']):

x[...]=2*x; print(x, end=' ') # 0 10 20 30 40 50 60 70 80 90 100 110

print('n')

# numpy.raval:返回展開(kāi)的一維數(shù)組,并且按需生成副本。返回的數(shù)組和輸入數(shù)組擁有相同數(shù)據(jù)類(lèi)型

a = np.arange(8).reshape(2,4)

b = a.ravel(); print(b) # [0 1 2 3 4 5 6 7]

# numpy.unique: 返回輸入數(shù)組中的去重元素?cái)?shù)組

a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])

u = np.unique(a); print(u) # [2 5 6 7 8 9]

# 位操作:bitwise_and, bitwise_or, invert, left_shift, right_shift

a,b = 13,17; print(bin(a), bin(b)) # 0b1101 0b10001

c = np.bitwise_and(13, 17); print(c) # 1

c = np.bitwise_or(13, 17); print(c) # 29

# 字符串函數(shù):add, multiply, center, capitalize, title, lower, upper, split, splitlines, strip, join, replace, decode, encode

print(np.char.add(['hello'],[' Spring'])) # ['hell Spring']

print(np.char.multiply('Hello ',3)) # Hello Hello Hello

# numpy.char.center: 此函數(shù)返回所需寬度的數(shù)組,以便輸入字符串位于中心,并使用fillchar在左側(cè)和右側(cè)進(jìn)行填充

print(np.char.center('hello', 20, fillchar = '*')) # *******hello********

a = np.char.encode('hello', 'cp500'); print(a) # b'x88x85x93x93x96'

b = np.char.decode(a, 'cp500'); print(b) # hello

# 三角函數(shù):sin, cos, tan, arcsin, arccos, arctan

a = np.array([0, 30, 45, 60, 90])

b = np.sin(a*np.pi/180); print(b) # [ 0. 0.5 0.70710678 0.8660254 1.]

# 舍入函數(shù): around, floor, ceil

a = np.array([1.0, 5.55, 123, 0.567, 25.532])

b = np.around(a); print(b) # [1. 6. 123. 1. 26.]

# 算數(shù)運(yùn)算:add, subtract, multiply, divide, reciprocal, power, mod 輸入數(shù)組必須具有相同的形狀或符合數(shù)組廣播規(guī)則

a, b = [5, 6], [7, 10]

c = np.subtract(a, b); print(c) # [-2 -4]

# 統(tǒng)計(jì)函數(shù):用于從數(shù)組中給定的元素中查找最小,最大,百分標(biāo)準(zhǔn)差和方差等, amin, amax, ptp, percentile, median, mean, average, std

a = np.array([1, 2, 3, 4, 5])

print(np.amin(a)) # 1

print(np.median(a)) # 3.0

print(np.mean(a)) # 3.0

# 副本和視圖: 在執(zhí)行函數(shù)時(shí),其中一些返回輸入數(shù)組的副本,而另一些返回視圖。 當(dāng)內(nèi)容物理存儲(chǔ)在另一個(gè)位置時(shí),稱(chēng)為副本。

# 另一方面,如果提供了相同內(nèi)存內(nèi)容的不同視圖,我們將其稱(chēng)為視圖

a = np.arange(6); print(a) # [0 1 2 3 4 5]

print(id(a)) # 54667664

b = a

print(id(b)) # 54667664

b.shape = 2,3

print(a); # [[0 1 2]

# [3 4 5]]

# IO: ndarray對(duì)象可以保存到磁盤(pán)文件并從磁盤(pán)文件加載

# load()和save()函數(shù)處理NumPy二進(jìn)制文件(帶npy擴(kuò)展名)

# loadtxt()和savetxt()函數(shù)處理正常的文本文件

a = np.array([1, 2, 3, 4, 5])

np.save('E:/GitCode/Python_Test/test_data/outfile.npy', a)

b = np.load('E:/GitCode/Python_Test/test_data/outfile.npy')

print(b) # [1 2 3 4 5]

np.savetxt('E:/GitCode/Python_Test/test_data/outfile.txt', a)

b = np.loadtxt('E:/GitCode/Python_Test/test_data/outfile.txt')

print(b) # [1. 2. 3. 4. 5.]

# Matplotlib是Python的繪圖庫(kù),在http://matplotlib.org/examples/ 中含有大量的matplotlib使用用例

x = np.arange(1,11)

y = 2 * x + 5

plt.title("Matplotlib demo")

plt.xlabel("x axis caption")

plt.ylabel("y axis caption")

plt.plot(x,y, 'ob')

plt.show()

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶(hù)希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱(chēng),數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱(chēng)"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉