numpy数据处理

介绍了获取、设置、调整 NumPy 数组数值的方法与工具,包括取值操作(如arr[2, 1])、切片操作(如 arr[:, 1:5])、掩码操作(如 arr[arr > 0])、花哨的索引操作(如 arr[0, [1, 5]]),以及组合操作(如 arr[:, [1, 5]])。

import numpy as np 
np.random.seed(0)
x3 = np.random.randint(10,size=(3,4,5))
print(x3)
print(type(x3))
print(x3.ndim)
print(x3.shape)
print(x3.size)
[[[5 0 3 3 7]
  [9 3 5 2 4]
  [7 6 8 8 1]
  [6 7 7 8 1]]

 [[5 9 8 9 4]
  [3 0 3 5 0]
  [2 3 8 1 3]
  [3 3 7 0 1]]

 [[9 9 0 4 7]
  [3 2 7 2 0]
  [0 4 5 5 6]
  [8 4 1 4 9]]]
<class 'numpy.ndarray'>
3
(3, 4, 5)
60
#通用函数: 数组计算
x = np.arange(9).reshape((3,3))
print(2 ** x)  #python3 : **表示乘方

[[  1   2   4]
 [  8  16  32]
 [ 64 128 256]]
#指定输出:大量计算时有用
y = np.empty((3,3))
np.multiply(x,10, out=y)
print(y)

[[ 0. 10. 20.]
 [30. 40. 50.]
 [60. 70. 80.]]
#每隔一个写入
xx = np.arange(5)
yy = np.zeros(10)
np.power(2,xx, out=yy[::2])
print(yy)
[ 1.  0.  2.  0.  4.  0.  8.  0. 16.  0.]
#聚合:reduce方法,对给定的元素和操作重复执行
x = np.arange(1,5)
print(np.add.reduce(x))
#保留中间结果
print(np.add.accumulate(x))
10
[ 1  3  6 10]
#外积
#任何通用函数都可以用 outer 方法获得两个不同输入数组所有元素对的函数运算结果。
x = np.arange(1,6)
np.multiply.outer(x,x)
array([[ 1,  2,  3,  4,  5],
       [ 2,  4,  6,  8, 10],
       [ 3,  6,  9, 12, 15],
       [ 4,  8, 12, 16, 20],
       [ 5, 10, 15, 20, 25]])
#聚合
#np.sum比sum更快
big_array = np.random.rand(1000000) 
%timeit sum(big_array) 
%timeit np.sum(big_array)
142 ms ± 4.36 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
977 µs ± 118 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
#聚合函数还有一个参数,用于指定沿着哪个轴的方向进行聚合。例如,可以通过指定axis=0 找到每一列的最小值
M = np.random.random((3,4))
print(M)
print(np.min(M,axis=0))
#也可以简单写成
print(M.min(axis=0))
[[0.91144496 0.1512867  0.42149518 0.76388113]
 [0.813865   0.90006791 0.71559529 0.26995452]
 [0.97107599 0.99902256 0.97262125 0.78889139]]
[0.813865   0.1512867  0.42149518 0.26995452]
[0.813865   0.1512867  0.42149518 0.26995452]
#广播:低维向高位扩展
a = np.array([0,1,2])
print(a)
b = np.ones((3,3))
print(b)
print(a + b)
[0 1 2]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[1. 2. 3.]
 [1. 2. 3.]
 [1. 2. 3.]]
#矩阵增加维度
a=np.array([1,2,3,4,5])
aa=a[np.newaxis,:]
aa2 = a[:, np.newaxis]
print(aa.shape)
print (aa)
print (aa2)
(1, 5)
[[1 2 3 4 5]]
[[1]
 [2]
 [3]
 [4]
 [5]]
#两个数组的同时广播
print(aa + aa2)
[[ 2  3  4  5  6]
 [ 3  4  5  6  7]
 [ 4  5  6  7  8]
 [ 5  6  7  8  9]
 [ 6  7  8  9 10]]
#画一个二维函数 z = f(x,y) 
x = np.linspace(0,5,50) #第三个参数指定元素个数
y = np.linspace(0,5,50)[:,np.newaxis]
z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x) #利用广播计算二维数组
#利用matplotlib画图
%matplotlib inline
import matplotlib.pyplot as plt 
plt.imshow(z,origin='lower', extent=[0,5,0,5],cmap='viridis')
plt.colorbar();

在这里插入图片描述

#布尔掩码
import numpy as np 
import pandas as pd 
rng = np.random.RandomState(0) 
x = rng.randint(10, size=(3, 4)) 
print(x)
[[5 0 3 3]
 [7 9 3 5]
 [2 4 7 6]]
#统计
print(x > 3)
print(np.sum(x > 3))
print(np.sum(x > 3,axis=0))#对列统计
#组合:布尔运算
print(np.sum((x > 1) & (x < 4))) #注意不能是and、or,numpy中布尔数组可以看成一串二进制
[[ True False False False]
 [ True  True False  True]
 [False  True  True  True]]
7
[2 2 1 2]
4
#布尔数组作为掩码
print(x[x > 5])
yanma = (x > 2) & (x < 5) #建立布尔数组(掩码)
print(x[yanma])
[7 9 7 6]
[3 3 3 4]
#花哨的索引:用一个数组作为索引
#利用花哨的索引,结果的形状与索引数组的形状一致,而不是与被索引数组的形状一致
x = np.array([1,2,3,4,5])
ind = np.array([[1,2],[3,4]])
print(x[ind])
[[2 3]
 [4 5]]
#对多个维度的数组也适用
X = np.arange(12).reshape(3,4)
print(X)
row = np.array([0, 1, 2]) 
col = np.array([2, 1, 3]) 
print(X[row, col])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[ 2  5 11]
#索引如果是一个行向量和一个列向量,会广播
print(X[row[:,np.newaxis], col])
[[ 2  1  3]
 [ 6  5  7]
 [10  9 11]]
#花哨的索引与掩码配合
print(X)
mask = np.array([0,1,0,1],dtype='bool')
print(X[row[:,np.newaxis], mask])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 1  3]
 [ 5  7]
 [ 9 11]]
#at() 函数在这里对给定的操作、给定的索引(这里是 i)以及给定的值(这里是 1)执行的是就地操作。
x = np.zeros(10)
print(x)
i = [1,2,2,3,3,3]
x[i] += 1
print(x)
#使用at
x = np.zeros(10)
np.add.at(x,i,1)
print(x)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[0. 1. 2. 3. 0. 0. 0. 0. 0. 0.]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值