pythonnumpy教程_Python教程:numpy的基本介绍
標(biāo)準(zhǔn)安裝的Python中用列表(list)保存一組值,可以用來當(dāng)作數(shù)組使用,不過由于列表的元素可以是任何對象,因此列表中所保存的是對象的指針。這樣為了保存一個簡單的[1,2,3],需要有3個指針和三個整數(shù)對象。對于數(shù)值運(yùn)算來說這種結(jié)構(gòu)顯然比較浪費(fèi)內(nèi)存和CPU計算時間。此外Python還提供了一個array模塊,array對象和列表不同,它直接保存數(shù)值,和C語言的一維數(shù)組比較類似。但是由于它不支持多維,也沒有各種運(yùn)算函數(shù),因此也不適合做數(shù)值運(yùn)算。
NumPy的誕生彌補(bǔ)了這些不足,NumPy提供了兩種基本的對象:
ndarray(N-dimensional array object)和ufunc(universal function
object)。ndarray(下文統(tǒng)一稱之為數(shù)組)是存儲單一數(shù)據(jù)類型的多維數(shù)組,而ufunc則是能夠?qū)?shù)組進(jìn)行處理的函數(shù)。
簡單的示范
import numpy as np
a = np.arange(10)
print(a)
a.reshape(2,5)
print(a)
print(a.dtype)
print(a.shape)
print(a.ndim)
print(a.size)
print(type(a))
創(chuàng)建Array
使用array函數(shù),從Python列表或元組中創(chuàng)建
import numpy as np
a = np.array([2,3,4])
print(a)
print(a.dtype)
b = np.array([1.2, 4.6, 7.8])
print(b)
print(b.dtype)
c = np.array([(1,2,3), (4,5,6)])
print(c)
print(c.dtype)
創(chuàng)建復(fù)數(shù)數(shù)組
d = np.array( [ [1,2], [3,4] ], dtype=complex )
print(d)
print(d.dtype)
使用numpy中的函數(shù)來創(chuàng)建數(shù)組 創(chuàng)建全是0的數(shù)組
e = np.zeros((3,4))
print(e)
print(e.dtype)
創(chuàng)建全是1的數(shù)組
f = np.ones( (2,3,4), dtype=np.int16 )
print(f)
print(f.dtype)
使用隨機(jī)數(shù)來填充
g = np.empty( (2,3) )
print(g)
print(g.dtype)
創(chuàng)建序列
h = np.arange( 10, 30, 5 )
print(h)
i = np.arange(0,2,0.3)
print(i)
j = np.linspace(0,2,9)
print(j)
print(len(j))
打印數(shù)組
打印numpy數(shù)組與Python列表基本一樣,但有些差別。
print(np.arange(10000))
print(np.arange(10000).reshape(100,100))
總結(jié)
以上是生活随笔為你收集整理的pythonnumpy教程_Python教程:numpy的基本介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 草木之心比喻是什么意思 草木之心比喻什么
- 下一篇: python统计段落单词词频_使用Pyt