Python第三方包-你了解numpy吗(numpy基础)
簡(jiǎn)介
NumPy 是一個(gè) Python 包。 它代表 “Numeric Python”。 它是一個(gè)由多維數(shù)組對(duì)象和用于處理數(shù)組的例程集合組成的庫(kù)。
安裝
Python官方版本,可以通過(guò)pip或者源碼安裝,推薦使用anaconda,這是發(fā)行版的python,內(nèi)含了numpy等重要第三方庫(kù)。
anaconda安裝使用見(jiàn)Windows下Anaconda3安裝及使用教程_周先森愛(ài)吃素的博客-CSDN博客?。
基本用法
創(chuàng)建ndarray對(duì)象
這是這個(gè)模塊最核心的對(duì)象,表示一個(gè)n維數(shù)組,它是相同類型的數(shù)據(jù)集合。
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)其中,
object? 任何暴露數(shù)組接口方法的對(duì)象都會(huì)返回一個(gè)數(shù)組或任何(嵌套)序列。
dtype? 數(shù)組的所需數(shù)據(jù)類型,可選。
copy? 可選,默認(rèn)為true,對(duì)象是否被復(fù)制。
order? C(按行)、F(按列)或A(任意,默認(rèn))。
subok? 默認(rèn)情況下,返回的數(shù)組被強(qiáng)制為基類數(shù)組。 如果為true,則返回子類。
ndimin?指定返回?cái)?shù)組的最小維數(shù)。
import numpy as np# 一維 a = np.array([1, 2, 3]) print(a) # n維 b = np.array([[1, 2, 3], [4, 5, 6]]) print(b) # 帶數(shù)據(jù)類型設(shè)置 c = np.array([1, 2, 3], dtype=complex) print(c) # 帶最小維度設(shè)置 d = np.array([[1, 2, 3], [4, 5, 6]], ndmin=3) print(d)dtype的使用
首先,numpy提供了比python更加豐富的標(biāo)量數(shù)據(jù)類型。
numpy.dtype(object, align, copy)其中,
Object:被轉(zhuǎn)換為數(shù)據(jù)類型的對(duì)象。
Align:如果為true,則向字段添加間隔,使其類似 C 的結(jié)構(gòu)體。
Copy: 生成dtype對(duì)象的新副本,如果為flase,結(jié)果是內(nèi)建數(shù)據(jù)類型對(duì)象的引用。
import numpy as np # 內(nèi)置轉(zhuǎn)換 dt1 = np.dtype(np.int32) print(dt1) # 簡(jiǎn)略寫法 dt2 = np.dtype('i4') print(dt2) # 結(jié)構(gòu)化數(shù)據(jù) dt3 = np.dtype([('year', np.int16)]) print(dt3) a = np.array([(1998, ), (2000, ), (2012, )], dtype=dt3) print(a) # 結(jié)構(gòu)化數(shù)據(jù)的字段訪問(wèn) print(a['year']) # 完整的結(jié)構(gòu)示例,類似C語(yǔ)言結(jié)構(gòu)體 date = np.dtype([('year', np.int32), ('month', np.int8), ('day', np.int8)]) b = np.array([(1998, 5, 26), (2000, 5, 26), (2014, 5, 26)]) print(b)ndarray屬性
? ? shape:包含數(shù)組維度的元組
a = np.array([[1, 2, 3], [4, 5, 6]]) print(a) print(a.shape)# a.shape = (3, 2) # print(a) # 等價(jià)于下面 c = a.reshape(3, 2) print(c)? ? ? ndim: 維度數(shù)
a = np.arange(24) print(a.ndim) b = a.reshape(2, 4, 3) print(b.ndim)? ? ?itemsize:每個(gè)元素占字節(jié)數(shù)
? ? ?flags:數(shù)組的幾個(gè)狀態(tài)
特殊數(shù)組的創(chuàng)建
numpy.empty(shape, dtype = float, order = 'C')? 沒(méi)有初值但是分配空間的數(shù)組
numpy.zeros(shape, dtype = float, order = 'C')? ?初值全為0的數(shù)組
numpy.ones(shape, dtype = None, order = 'C')? ?初值全為1的數(shù)組
其中,
shape是形狀,如(2,2)表示二行二列
dtype表示數(shù)據(jù)類型
order表示組織風(fēng)格
其余數(shù)值類型數(shù)組如arange不細(xì)說(shuō)
import numpy as np a = np.empty((2, 2), dtype=np.int8, order='C') print(a) b = np.zeros((2, 2), dtype=np.int8, order='C') print(b) c = np.ones((2, 2), dtype=np.int8, order='C') print(c)從python內(nèi)置數(shù)據(jù)類型轉(zhuǎn)換為numpy的數(shù)組
asarray
numpy.asarray(a, dtype = None, order = None)
其中,
a?任意形式的輸入?yún)?shù),比如列表、列表的元組、元組、元組的元組、元組的列表
dtype,order 不再說(shuō)明
import numpy as np # 列表 x = [1, 2, 3] a = np.asarray(x) print(a) # 元組 a = np.asarray(x) print(a) # 元組列表 x = [(1, 2, 3), (4, 5)] a = np.asarray(x) print(a)frombuffer
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
其中,
buffer 任何暴露緩沖區(qū)接口的對(duì)象
dtype 同上
count 需要讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)
offset 需要讀取的起始位置,默認(rèn)為0
fromiter
numpy.fromiter(iterable, dtype, count = -1)
其中,
iterable 任何可迭代對(duì)象
dtype 返回?cái)?shù)組的數(shù)據(jù)類型
count 需要讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的Python第三方包-你了解numpy吗(numpy基础)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python实战-获取鼠标键盘事件
- 下一篇: Python第三方包-你了解numpy吗