15分钟带你快速入门Numpy


Numpy的相关属性

import numpy as np 
a = np.array([1,2,3])  
print (a)
[1 2 3]

Numpy的相关属性说明:

属性 说明
ndarray.ndim 秩,即轴的数量或维度的数量
ndarray.shape 数组的维度,对于矩阵,n 行 m 列
ndarray.size 数组元素的总个数,相当于 .shape 中 n*m 的值
ndarray.dtype ndarray 对象的元素类型
ndarray.itemsize ndarray 对象中每个元素的大小,以字节为单位
ndarray.flags ndarray 对象的内存信息
ndarray.real ndarray元素的实部
ndarray.imag ndarray 元素的虚部
ndarray.data 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性

a.shape,a.ndim,a.size
((3,), 1, 3)
# 默认为浮点数
x = np.zeros(5) 
print(x)

# 设置类型为整数
y = np.zeros((5,), dtype = np.int) 
print(y)
[0. 0. 0. 0. 0.]
[0 0 0 0 0]
import numpy as np

# 默认为浮点数
x = np.ones(5) 
print(x)

# 自定义类型
x = np.ones([2,2], dtype = int)
print(x)
[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]
np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
import numpy as np
x = np.arange(10,20,2)  
print (x)
[10 12 14 16 18]
import numpy as np

# 设置了 dtype
x = np.arange(5, dtype =  float)  
print (x)
[0. 1. 2. 3. 4.]
import numpy as np
a =np.linspace(1,10,10,retstep= True)

print(a)
# 拓展例子
b =np.linspace(1,10,10).reshape([10,1])
print(b)
(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)
[[ 1.]
 [ 2.]
 [ 3.]
 [ 4.]
 [ 5.]
 [ 6.]
 [ 7.]
 [ 8.]
 [ 9.]
 [10.]]
import numpy as np

a = np.linspace(10, 20,  5, endpoint =  False)  
print(a)
[10. 12. 14. 16. 18.]
import numpy as np
# 切片
a = np.arange(10)  # [0 1 2 3 4 5 6 7 8 9]
print(a[2:5])
[2 3 4]
import numpy as np
# 范围切片
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
b = a[1:3, 1:3]
c = a[1:3,[1,2]]
d = a[...,1:]
print(b)
print(c)
print(d)
[[5 6]
 [8 9]]
[[5 6]
 [8 9]]
[[2 3]
 [5 6]
 [8 9]]
import numpy as np 
# 筛选值
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  
print ('我们的数组是:')
print (x)
print ('\n')
# 现在我们会打印出大于 5 的元素  
print  ('大于 5 的元素是:')
print (x[x >  5])
我们的数组是:
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]


大于 5 的元素是:
[ 6  7  8  9 10 11]
import numpy as np 
# 过滤非nan
a = np.array([np.nan,  1,2,np.nan,3,4,5])  
print (a[~np.isnan(a)])
[1. 2. 3. 4. 5.]
import numpy as np

a = np.arange(9).reshape(3,3) 
print ('原始数组:')
for row in a:
    print (row)
# flat 元素数组迭代器
# 对数组中每个元素都进行处理,可以使用flat属性,该属性是一个数组元素迭代器:
print ('迭代后的数组:')
for element in a.flat:
    print (element)
原始数组:
[0 1 2]
[3 4 5]
[6 7 8]
迭代后的数组:
0
1
2
3
4
5
6
7
8

numpy.ndarray.flatten 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组,格式如下:

ndarray.flatten(order=‘C’)
参数说明:

order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。

import numpy as np

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

print ('原数组:')
print (a)
print ('\n')
# 默认按行

print ('展开的数组:')
print (a.flatten())
print ('\n')

print ('以 F 风格顺序展开的数组:')
print (a.flatten(order = 'F'))
原数组:
[[0 1 2 3]
 [4 5 6 7]]


展开的数组:
[0 1 2 3 4 5 6 7]


以 F 风格顺序展开的数组:
[0 4 1 5 2 6 3 7]

numpy.ravel

numpy.ravel() 展平的数组元素,顺序通常是"C风格",返回的是数组视图(view,有点类似 C/C++引用reference的意味),修改会影响原始数组。

该函数接收两个参数:

numpy.ravel(a, order=‘C’)
参数说明:

order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘K’ – 元素在内存中的出现顺序。
实例

import numpy as np

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

print ('原数组:')
print (a)
print ('\n')

print ('调用 ravel 函数之后:')
print (a.ravel())
print ('\n')

print ('以 F 风格顺序调用 ravel 函数之后:')
print (a.ravel(order = 'F'))
原数组:
[[0 1 2 3]
 [4 5 6 7]]


调用 ravel 函数之后:
[0 1 2 3 4 5 6 7]


以 F 风格顺序调用 ravel 函数之后:
[0 4 1 5 2 6 3 7]
import numpy as np

a = np.arange(12).reshape(3,4)

print ('原数组:')
print (a )
print ('\n')

print ('对换数组:')
print (np.transpose(a)) # =a.T
原数组:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


对换数组:
[[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]

维度

# 维度扩展
import numpy as np

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

print ('数组 x:')
print (x)
print ('\n')
y = np.expand_dims(x, axis = 0)

print ('数组 y:')
print (y)
print ('\n')

print ('数组 x 和 y 的形状:')
print (x.shape, y.shape)
print ('\n')
# 在位置 1 插入轴
y = np.expand_dims(x, axis = 1)

print ('在位置 1 插入轴之后的数组 y:')
print (y)
print ('\n')

print ('x.ndim 和 y.ndim:')
print (x.ndim,y.ndim)
print ('\n')

print ('x.shape 和 y.shape:')
print (x.shape, y.shape)
数组 x:
[[1 2]
 [3 4]]


数组 y:
[[[1 2]
  [3 4]]]


数组 x 和 y 的形状:
(2, 2) (1, 2, 2)


在位置 1 插入轴之后的数组 y:
[[[1 2]]

 [[3 4]]]


x.ndim 和 y.ndim:
2 3


x.shape 和 y.shape:
(2, 2) (2, 1, 2)
# 压缩维度
import numpy as np

x = np.arange(9).reshape(1,3,3)

print ('数组 x:')
print (x)
print ('\n')
y = np.squeeze(x)

print ('数组 y:')
print (y)
print ('\n')

print ('数组 x 和 y 的形状:')
print (x.shape, y.shape)
数组 x:
[[[0 1 2]
  [3 4 5]
  [6 7 8]]]


数组 y:
[[0 1 2]
 [3 4 5]
 [6 7 8]]


数组 x 和 y 的形状:
(1, 3, 3) (3, 3)
# 连接数组
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])

print ('第二个数组:')
print (b)
print ('\n')
# 两个数组的维度相同

print ('沿轴 0 连接两个数组:')
print (np.concatenate((a,b)))
print ('\n')

print ('沿轴 1 连接两个数组:')
print (np.concatenate((a,b),axis = 1))
第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


沿轴 0 连接两个数组:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]


沿轴 1 连接两个数组:
[[1 2 5 6]
 [3 4 7 8]]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])

print ('第二个数组:')
print (b)
print ('\n')

print ('沿轴 0 堆叠两个数组:')
print (np.stack((a,b),0))
print ('\n')

print ('沿轴 1 堆叠两个数组:')
print (np.stack((a,b),1))
第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


沿轴 0 堆叠两个数组:
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]


沿轴 1 堆叠两个数组:
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])

print ('第二个数组:')
print (b)
print ('\n')

print ('水平堆叠:')
c = np.hstack((a,b))
print (c)
print ('\n')
第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


水平堆叠:
[[1 2 5 6]
 [3 4 7 8]]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])

print ('第二个数组:')
print (b)
print ('\n')

print ('竖直堆叠:')
c = np.vstack((a,b))
print (c)
第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


竖直堆叠:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

分割数组

import numpy as np
# 分割数组 
a = np.arange(9)

print ('第一个数组:')
print (a)
print ('\n')

print ('将数组分为三个大小相等的子数组:')
b = np.split(a,3)
print (b)
print ('\n')

print ('将数组在一维数组中表明的位置分割:')
b = np.split(a,[4,7])
print (b)
第一个数组:
[0 1 2 3 4 5 6 7 8]


将数组分为三个大小相等的子数组:
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]


将数组在一维数组中表明的位置分割:
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
import numpy as np

harr = np.floor(10 * np.random.random((2, 6)))
print ('原array:')
print(harr)

print ('水平拆分后:')
print(np.hsplit(harr, 3))
原array:
[[2. 8. 7. 8. 5. 4.]
 [2. 1. 4. 4. 8. 7.]]
水平拆分后:
[array([[2., 8.],
       [2., 1.]]), array([[7., 8.],
       [4., 4.]]), array([[5., 4.],
       [8., 7.]])]
import numpy as np

a = np.arange(16).reshape(4,4)

print ('第一个数组:')
print (a)
print ('\n')

print ('竖直分割:')
b = np.vsplit(a,2)
print (b)
第一个数组:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]


竖直分割:
[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

数组元素的添加与删除

函数 元素及描述
resize 返回指定形状的新数组
append 将值添加到数组末尾
insert 沿指定轴将值插入到指定下标之前
delete 删掉某个轴的子数组,并返回删除后的新数组
unique 查找数组内的唯一元素

import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')

print ('第一个数组的形状:')
print (a.shape)
print ('\n')
b = np.resize(a, (3,2))

print ('第二个数组:')
print (b)
print ('\n')

print ('第二个数组的形状:')
print (b.shape)
print ('\n')
# 要注意 a 的第一行在 b 中重复出现,因为尺寸变大了

print ('修改第二个数组的大小:')
b = np.resize(a,(3,3))
print (b)
第一个数组:
[[1 2 3]
 [4 5 6]]


第一个数组的形状:
(2, 3)


第二个数组:
[[1 2]
 [3 4]
 [5 6]]


第二个数组的形状:
(3, 2)


修改第二个数组的大小:
[[1 2 3]
 [4 5 6]
 [1 2 3]]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')

print ('向数组添加元素:')
print (np.append(a, [7,8,9]))
print ('\n')

print ('沿轴 0 添加元素:')
print (np.append(a, [[7,8,9]],axis = 0))
print ('\n')

print ('沿轴 1 添加元素:')
print (np.append(a, [[5,5,5],[7,8,9]],axis = 1))
第一个数组:
[[1 2 3]
 [4 5 6]]


向数组添加元素:
[1 2 3 4 5 6 7 8 9]


沿轴 0 添加元素:
[[1 2 3]
 [4 5 6]
 [7 8 9]]


沿轴 1 添加元素:
[[1 2 3 5 5 5]
 [4 5 6 7 8 9]]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')

print ('未传递 Axis 参数。 在删除之前输入数组会被展开。')
print (np.insert(a,3,[11,12]))
print ('\n')
print ('传递了 Axis 参数。 会广播值数组来配输入数组。')

print ('沿轴 0 广播:')
print (np.insert(a,1,[11],axis = 0))
print ('\n')

print ('沿轴 1 广播:')
print (np.insert(a,1,11,axis = 1))
第一个数组:
[[1 2]
 [3 4]
 [5 6]]


未传递 Axis 参数。 在删除之前输入数组会被展开。
[ 1  2  3 11 12  4  5  6]


传递了 Axis 参数。 会广播值数组来配输入数组。
沿轴 0 广播:
[[ 1  2]
 [11 11]
 [ 3  4]
 [ 5  6]]


沿轴 1 广播:
[[ 1 11  2]
 [ 3 11  4]
 [ 5 11  6]]
import numpy as np

a = np.arange(12).reshape(3,4)

print ('第一个数组:')
print (a)
print ('\n')

print ('未传递 Axis 参数。 在插入之前输入数组会被展开。')
print (np.delete(a,5))
print ('\n')

print ('删除第二列:')
print (np.delete(a,1,axis = 1))
print ('\n')

print ('包含从数组中删除的替代值的切片:')
a = np.array([1,2,3,4,5,6,7,8,9,10])
print (np.delete(a, np.s_[::2]))
第一个数组:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


未传递 Axis 参数。 在插入之前输入数组会被展开。
[ 0  1  2  3  4  6  7  8  9 10 11]


删除第二列:
[[ 0  2  3]
 [ 4  6  7]
 [ 8 10 11]]


包含从数组中删除的替代值的切片:
[ 2  4  6  8 10]
import numpy as np

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

print ('第一个数组:')
print (a)
print ('\n')

print ('第一个数组的去重值:')
u = np.unique(a)
print (u)
print ('\n')

print ('去重数组的索引数组:')
u,indices = np.unique(a, return_index = True)
print (indices)
print ('\n')

print ('我们可以看到每个和原数组下标对应的数值:')
print (a)
print ('\n')

print ('去重数组的下标:')
u,indices = np.unique(a,return_inverse = True)
print (u)
print ('\n')

print ('下标为:')
print (indices)
print ('\n')

print ('使用下标重构原数组:')
print (u[indices])
print ('\n')

print ('返回去重元素的重复数量:')
u,indices = np.unique(a,return_counts = True)
print (u)
print (indices)
第一个数组:
[5 2 6 2 7 5 6 8 2 9]


第一个数组的去重值:
[2 5 6 7 8 9]


去重数组的索引数组:
[1 0 2 4 7 9]


我们可以看到每个和原数组下标对应的数值:
[5 2 6 2 7 5 6 8 2 9]


去重数组的下标:
[2 5 6 7 8 9]


下标为:
[1 0 2 0 3 1 2 4 0 5]


使用下标重构原数组:
[5 2 6 2 7 5 6 8 2 9]


返回去重元素的重复数量:
[2 5 6 7 8 9]
[3 2 2 1 1 1]

numpy.char

以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作。 它们基于 Python 内置库中的标准字符串函数。

这些函数在字符数组类(numpy.char)中定义。

函数 描述
add() 对两个数组的逐个字符串元素进行连接
multiply() 返回按元素多重连接后的字符串
center() 居中字符串
capitalize() 将字符串第一个字母转换为大写
title() 将字符串的每个单词的第一个字母转换为大写
lower() 数组元素转换为小写
upper() 数组元素转换为大写
split() 指定分隔符对字符串进行分割,并返回数组列表
splitlines() 返回元素中的行列表,以换行符分割
strip() 移除元素开头或者结尾处的特定字符
join() 通过指定分隔符来连接数组中的元素
replace() 使用新字符串替换字符串中的所有子字符串
decode() 数组元素依次调用str.decode
encode() 数组元素依次调用str.encode

import numpy as np 

print ('连接两个字符串:')
print (np.char.add(['hello'],[' xyz']))
print ('\n')

print ('连接示例:')
print (np.char.add(['hello', 'hi'],[' abc', ' xyz']))
连接两个字符串:
['hello xyz']


连接示例:
['hello abc' 'hi xyz']
import numpy as np 

print (np.char.multiply('Runoob ',3))
Runoob Runoob Runoob 
import numpy as np 

# np.char.center(str , width,fillchar) :
# str: 字符串,width: 长度,fillchar: 填充字符
print (np.char.center('Runoob', 20,fillchar = '*'))
*******Runoob*******
import numpy as np 

print (np.char.capitalize('runoob'))
Runoob

Numpy运算

NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等。

import numpy as np

a = np.array([0,30,45,60,90])  
print ('含有正弦值的数组:')
sin = np.sin(a*np.pi/180)  
print (sin)
print ('\n')
print ('计算角度的反正弦,返回值以弧度为单位:')
inv = np.arcsin(sin)  
print (inv)
print ('\n')
print ('通过转化为角度制来检查结果:')
print (np.degrees(inv))
print ('\n')
print ('arccos 和 arctan 函数行为类似:')
cos = np.cos(a*np.pi/180)  
print (cos)
print ('\n')
print ('反余弦:')
inv = np.arccos(cos)  
print (inv)
print ('\n')
print ('角度制单位:')
print (np.degrees(inv))
print ('\n')
print ('tan 函数:')
tan = np.tan(a*np.pi/180)  
print (tan)
print ('\n')
print ('反正切:')
inv = np.arctan(tan)  
print (inv)
print ('\n')
print ('角度制单位:')
print (np.degrees(inv))
含有正弦值的数组:
[0.         0.5        0.70710678 0.8660254  1.        ]


计算角度的反正弦,返回值以弧度为单位:
[0.         0.52359878 0.78539816 1.04719755 1.57079633]


通过转化为角度制来检查结果:
[ 0. 30. 45. 60. 90.]


arccos 和 arctan 函数行为类似:
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]


反余弦:
[0.         0.52359878 0.78539816 1.04719755 1.57079633]


角度制单位:
[ 0. 30. 45. 60. 90.]


tan 函数:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
 1.63312394e+16]


反正切:
[0.         0.52359878 0.78539816 1.04719755 1.57079633]


角度制单位:
[ 0. 30. 45. 60. 90.]

NumPy 统计函数

NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等。 函数说明如下

import numpy as np 

a = np.array([[3,7,5],[8,4,3],[2,4,9]])  
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 amin() 函数:')
print (np.amin(a,1))
print ('\n')
print ('再次调用 amin() 函数:')
print (np.amin(a,0))
print ('\n')
print ('调用 amax() 函数:')
print (np.amax(a))
print ('\n')
print ('再次调用 amax() 函数:')
print (np.amax(a, axis =  0))
我们的数组是:
[[3 7 5]
 [8 4 3]
 [2 4 9]]


调用 amin() 函数:
[3 3 2]


再次调用 amin() 函数:
[2 4 3]


调用 amax() 函数:
9


再次调用 amax() 函数:
[8 7 9]

numpy.ptp()

numpy.ptp()函数计算数组中元素最大值与最小值的差(最大值 - 最小值)

import numpy as np 

a = np.array([[3,7,5],[8,4,3],[2,4,9]])  
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 ptp() 函数:')
print (np.ptp(a))
print ('\n')
print ('沿轴 1 调用 ptp() 函数:')
print (np.ptp(a, axis =  1))
print ('\n')
print ('沿轴 0 调用 ptp() 函数:')
print (np.ptp(a, axis =  0))
我们的数组是:
[[3 7 5]
 [8 4 3]
 [2 4 9]]


调用 ptp() 函数:
7


沿轴 1 调用 ptp() 函数:
[4 5 7]


沿轴 0 调用 ptp() 函数:
[6 3 6]

numpy.percentile()

百分位数是统计中使用的度量,表示小于这个值的观察值的百分比。 函数numpy.percentile()接受以下参数。

numpy.percentile(a, q, axis)
参数说明:

a: 输入数组
q: 要计算的百分位数,在 0 ~ 100 之间
axis: 沿着它计算百分位数的轴
首先明确百分位数:

第 p 个百分位数是这样一个值,它使得至少有 p% 的数据项小于或等于这个值,且至少有 (100-p)% 的数据项大于或等于这个值。

举个例子:高等院校的入学考试成绩经常以百分位数的形式报告。比如,假设某个考生在入学考试中的语文部分的原始分数为 54 分。相对于参加同一考试的其他学生来说,他的成绩如何并不容易知道。但是如果原始分数54分恰好对应的是第70百分位数,我们就能知道大约70%的学生的考分比他低,而约30%的学生考分比他高。

这里的 p = 70。

import numpy as np 

a = np.array([[10, 7, 4], [3, 2, 1]])
print ('我们的数组是:')
print (a)

print ('调用 percentile() 函数:')
# 50% 的分位数,就是 a 里排序之后的中位数
print (np.percentile(a, 60)) 

# axis 为 0,在纵列上求
print (np.percentile(a, 60, axis=0)) 

# axis 为 1,在横行上求
print (np.percentile(a, 60, axis=1)) 

# 保持维度不变
print (np.percentile(a, 60, axis=1, keepdims=True))
我们的数组是:
[[10  7  4]
 [ 3  2  1]]
调用 percentile() 函数:
4.0
[7.2 5.  2.8]
[7.6 2.2]
[[7.6]
 [2.2]]
import numpy as np 
# numpy.median()
# numpy.median() 函数用于计算数组 a 中元素的中位数(中值)
a = np.array([[30,65,70],[80,95,10],[50,90,60]])  
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 median() 函数:')
print (np.median(a))
print ('\n')
print ('沿轴 0 调用 median() 函数:')
print (np.median(a, axis =  0))
print ('\n')
print ('沿轴 1 调用 median() 函数:')
print (np.median(a, axis =  1))
我们的数组是:
[[30 65 70]
 [80 95 10]
 [50 90 60]]


调用 median() 函数:
65.0


沿轴 0 调用 median() 函数:
[50. 90. 60.]


沿轴 1 调用 median() 函数:
[65. 80. 60.]

numpy.mean()

numpy.mean() 函数返回数组中元素的算术平均值。 如果提供了轴,则沿其计算。

算术平均值是沿轴的元素的总和除以元素的数量。

import numpy as np 

a = np.array([[1,2,3],[3,4,5],[4,5,6]])  
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 mean() 函数:')
print (np.mean(a))
print ('\n')
print ('沿轴 0 调用 mean() 函数:')
print (np.mean(a, axis =  0))
print ('\n')
print ('沿轴 1 调用 mean() 函数:')
print (np.mean(a, axis =  1))
我们的数组是:
[[1 2 3]
 [3 4 5]
 [4 5 6]]


调用 mean() 函数:
3.6666666666666665


沿轴 0 调用 mean() 函数:
[2.66666667 3.66666667 4.66666667]


沿轴 1 调用 mean() 函数:
[2. 4. 5.]

numpy.average()

numpy.average() 函数根据在另一个数组中给出的各自的权重计算数组中元素的加权平均值。

该函数可以接受一个轴参数。 如果没有指定轴,则数组会被展开。

加权平均值即将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数。

考虑数组[1,2,3,4]和相应的权重[4,3,2,1],通过将相应元素的乘积相加,并将和除以权重的和,来计算加权平均值。

import numpy as np 

a = np.array([1,2,3,4])  
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 average() 函数:')
print (np.average(a))
print ('\n')
# 不指定权重时相当于 mean 函数
wts = np.array([4,3,2,1])  
print ('再次调用 average() 函数:')
print (np.average(a,weights = wts))
print ('\n')
# 如果 returned 参数设为 true,则返回权重的和  
print ('权重的和:')
print (np.average([1,2,3,  4],weights =  [4,3,2,1], returned =  True))
我们的数组是:
[1 2 3 4]


调用 average() 函数:
2.5


再次调用 average() 函数:
2.0


权重的和:
(2.0, 10.0)

标准差

标准差是一组数据平均值分散程度的一种度量。

标准差是方差的算术平方根。

标准差公式如下:

std = sqrt(mean((x - x.mean())**2))
如果数组是 [1,2,3,4],则其平均值为 2.5。 因此,差的平方是 [2.25,0.25,0.25,2.25],并且再求其平均值的平方根除以 4,即 sqrt(5/4) ,结果为 1.1180339887498949。

import numpy as np 

print (np.std([1,2,3,4]))
1.118033988749895

方差

统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数,即 mean((x - x.mean())** 2)。

换句话说,标准差是方差的平方根。

import numpy as np

print (np.var([1,2,3,4]))
1.25

NumPy 排序、条件筛选函数

numpy.argsort()
numpy.argsort() 函数返回的是数组值从小到大的索引值。
numpy.sort()
numpy.sort() 函数返回输入数组的排序副本。函数格式如下:
numpy.argmax() 和 numpy.argmin()
numpy.argmax() 和 numpy.argmin()函数分别沿给定轴返回最大和最小元素的索引。
numpy.argmax() 和 numpy.argmin()
numpy.argmax() 和 numpy.argmin()函数分别沿给定轴返回最大和最小元素的索引。
numpy.extract()
numpy.extract() 函数根据某个条件从数组中抽取元素,返回满条件的元素。

import numpy as np 

x = np.arange(9.).reshape(3,  3)  
print ('我们的数组是:')
print (x)
print ( '大于 3 的元素的索引:')
y = np.where(x >  3)  
print (y)
print ('使用这些索引来获取满足条件的元素:')
print (x[y])
我们的数组是:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
大于 3 的元素的索引:
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
使用这些索引来获取满足条件的元素:
[4. 5. 6. 7. 8.]
import numpy as np 

x = np.arange(9.).reshape(3,  3)  
print ('我们的数组是:')
print (x)
# 定义条件, 选择偶数元素
condition = np.mod(x,2)  ==  0  
print ('按元素的条件值:')
print (condition)
print ('使用条件提取元素:')
print (np.extract(condition, x))
我们的数组是:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
按元素的条件值:
[[ True False  True]
 [False  True False]
 [ True False  True]]
使用条件提取元素:
[0. 2. 4. 6. 8.]

ndarray.view() 方会创建一个新的数组对象,该方法创建的新数组的维数变化不会改变原始数据的维数。
ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。

NumPy 矩阵库(Matrix)

NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象。

一个 的矩阵是一个由行(row)列(column)元素排列成的矩形阵列。

numpy.matlib.zeros()
numpy.matlib.zeros() 函数创建一个以 0 填充的矩阵。
numpy.matlib.ones()
numpy.matlib.ones()函数创建一个以 1 填充的矩阵。
numpy.matlib.eye()
numpy.matlib.eye() 函数返回一个矩阵,对角线元素为 1,其他位置为零。
numpy.matlib.identity()
numpy.matlib.identity() 函数返回给定大小的单位矩阵。
numpy.matlib.rand()
numpy.matlib.rand() 函数创建一个给定大小的矩阵,数据是随机填充的。
矩阵总是二维的,而 ndarray 是一个 n 维数组。 两个对象都是可互换的

import numpy.matlib 
import numpy as np 

print (np.matlib.rand(3,3))
[[0.71882497 0.9178754  0.18258573]
 [0.47369131 0.44273881 0.59564433]
 [0.51435259 0.36844582 0.78807934]]
import numpy.matlib 
import numpy as np  

i = np.matrix('1,2;3,4')  
print (i)
[[1 2]
 [3 4]]
import numpy.matlib 
import numpy as np  

j = np.asarray(i)  
print (j)
[[1 2]
 [3 4]]
import numpy.matlib 
import numpy as np  

k = np.asmatrix (j)  
print (k)
[[1 2]
 [3 4]]

NumPy IO

Numpy 可以读写磁盘上的文本数据或二进制数据。

NumPy 为 ndarray 对象引入了一个简单的文件格式:npy。

npy 文件用于存储重建 ndarray 所需的数据、图形、dtype 和其他信息。

常用的 IO 函数有:

load() 和 save() 函数是读写文件数组数据的两个主要函数,默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为 .npy 的文件中。
savez() 函数用于将多个数组写入文件,默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为 .npz 的文件中。
loadtxt() 和 savetxt() 函数处理正常的文本文件(.txt 等)

import numpy as np 


a=np.arange(0,10,0.5).reshape(4,-1)
np.savetxt("out.txt",a,fmt="%d",delimiter=",") # 改为保存为整数,以逗号分隔
b = np.loadtxt("out.txt",delimiter=",") # load 时也要指定为逗号分隔
print(b)
[[0. 0. 1. 1. 2.]
 [2. 3. 3. 4. 4.]
 [5. 5. 6. 6. 7.]
 [7. 8. 8. 9. 9.]]
import numpy as np 

a = np.array([[1,2,3],[4,5,6]])
b = np.arange(0, 1.0, 0.1)
c = np.sin(b)
# c 使用了关键字参数 sin_array
np.savez("runoob.npz", a, b, sin_array = c)
r = np.load("runoob.npz")  
print(r.files) # 查看各个数组名称
print(r["arr_0"]) # 数组 a
print(r["arr_1"]) # 数组 b
print(r["sin_array"]) # 数组 c
['sin_array', 'arr_0', 'arr_1']
[[1 2 3]
 [4 5 6]]
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[0.         0.09983342 0.19866933 0.29552021 0.38941834 0.47942554
 0.56464247 0.64421769 0.71735609 0.78332691]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值