您的位置 首页 电源

Python中NumPy简介及运用举例

NumPy是Python语言的一个扩展包。支持多维数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。NumPy提供了与Matlab相似的功能与操作方式,因为

NumPy是Python言语的一个扩展包。支撑多维数组与矩阵运算,此外也针对数组运算供给很多的数学函数库。NumPy供给了与Matlab类似的功用与操作方法,由于两者皆为直译言语。

NumPy一般与SciPy(Scientific Python)和Matplotlib(绘图库)一同运用,这种组合广泛用于代替Matlab,是一个盛行的技能渠道。

NumPy中界说的最重要的目标是称为ndarray的N维数组类型。它描绘相同类型的元素调集,能够运用根据零的索引拜访调集中元素。根本的ndarray是运用NumPy中的数组函数创立的: numpy.array。

NumPy支撑比Python更多品种的数值类型。NumPy数值是dtype(数据类型)目标的实例,每个目标具有仅有的特征。

以下对ndarray的介绍来自于 https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html (此链接可检查ndarray中包括的各种函数介绍):

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简略运用比如(参阅: 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]

# 等距离数字的数组

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指定回来数组的最小维数

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:数组的所需数据类型

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

# 运用数组标量类型

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

# int8,int16,int32,int64可替换为等价的字符串’i1′, ‘i2’, ‘i4’, ‘i8’

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

# 调整数组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:回来数组的维数

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

# numpy.reshape: 在不改变数据的条件下修正形状

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

# itemsize:回来数组中每个元素的字节单位长度

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

# 空数组

x = np.empty([3, 2], dtype=’i1′); print(x) # 数组x的元素为随机值,由于它们未初始化

# 含有5个0的数组,若不指定类型,则默以为float

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

# 含有6个1的二维数组,若不指定类型,则默以为float

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

# [1 1 1]]

# 将列表转换为ndarray

x = [1, 2, 3]

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

# 将元组转换为ndarray

x = (1, 2, 3)

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

# 运用内置的range()函数创立列表目标

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

# 从列表中取得迭代器

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

# 运用迭代器创立ndarray, fromiter函数从任何可迭代目标构建一个ndarray目标,回来一个新的一维数组

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

# arange函数回来ndarray目标,包括给定规模内的等距离值

# numpy.arange(start, stop, step, dtype), start起始值,默以为0;stop停止值,不包括; step距离,默以为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,此函数类似于arange,在此函数中,指定了规模之间的均匀距离数量,而不是步长

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

# start,起始值;stop,停止值,假如endpoint为true,该值包括于序列中;num,要生成的等距离样例数量,默以为50;

# endpoint,序列中是否包括stop值,默以为true;retstep,假如为true,回来样例,以及接连数字之间的步长

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,此函数回来ndarray目标,包括在对数刻度上均匀分布的数字.刻度的开端和完毕端点是某个底数的幂,一般为10

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

# base,对数空间的底数,默以为10;其它参数同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目标的内容能够经过索引或切片来拜访和修正,就像Python的内置容器目标相同;

# 根本切片:经过将start、stop和step参数供给给内置的slice函数来结构一个Python slice目标,用来提早数组的一部分

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

# 经过将由冒号分隔的切片参数(start:stop:step)直接供给给ndarray目标,也能够取得相同的成果

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]

# 切片还能够包括省略号(…),来使挑选元组的长度与数组的维度相同

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]

# 高档索引:假如一个ndarray对错元组序列,数据类型为整数或布尔值的ndarray,或许至少一个元素为

# 序列目标的元组,咱们就能够用它来索引ndarray,高档索引一直回来数据的副本

# 高档索引:整数:根据N维索引来获取数组中恣意元素

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

# y中包括数组x中(0,0), (1,1), (2,0)方位处的元素

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

# 高档索引:布尔值:当成果目标是布尔运算的成果时,将运用此类型的高档索引

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]

# ~(取补运算符)来过滤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.]

# 从数组中过滤掉非复数元素

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在算术运算期间处理不同形状的数组的才能, 对数组的算术运算一般在相应的元素上运转

# 假如两个数组的维数不相同,则元素到元素的操作是不可能的。但是,在NumPy中依然能够对形状不类似的数组进行操作,由于它具有播送功用。

# 较小的数组会播送到较大数组的巨细,以便使它们的形状可兼容

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.]]

# 数组上的迭代:NumPy包包括一个迭代器目标numpy.nditer。它是一个有用的多维迭代器目标,能够用于在数组上进行迭代。

# 数组的每个元素可运用Python的规范Iterator接口来拜访

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’)

# 修正数组的值: nditer目标的一个可选参数op_flags,其默认值为只读,但能够设置为读写或只写形式.这将答应运用此迭代器修正数组元素

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:回来打开的一维数组,而且按需生成副本。回来的数组和输入数组具有相同数据类型

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

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

# numpy.unique: 回来输入数组中的去重元素数组

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

# 字符串函数: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: 此函数回来所需宽度的数组,以便输入字符串坐落中心,并运用fillchar在左边和右侧进行填充

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

# 三角函数: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.]

# 舍入函数: 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.]

# 管用运算:add, subtract, multiply, divide, reciprocal, power, mod 输入数组有必要具有相同的形状或契合数组播送规矩

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

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

# 计算函数:用于从数组中给定的元素中查找最小,最大,百分规范差和方差等, 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

# 副本和视图: 在履行函数时,其间一些回来输入数组的副本,而另一些回来视图。 当内容物理存储在另一个方位时,称为副本。

# 另一方面,假如供给了相同内存内容的不同视图,咱们将其称为视图

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目标能够保存到磁盘文件并从磁盘文件加载

# load()和save()函数处理NumPy二进制文件(带npy扩展名)

# loadtxt()和savetxt()函数处理正常的文本文件

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的绘图库,在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()

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/dianyuan/155746.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部