日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

pythonnumpy教程_Python教程:numpy的基本介绍

發(fā)布時間:2023/12/3 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythonnumpy教程_Python教程:numpy的基本介绍 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

標(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。