pythonaxis函数_对python中array.sum(axis=?)的用法介绍

根据代码中运行的结果来看,主要由以下几种:

1. sum():将array中每个元素相加的结果

2. axis对应的是维度的相加。

比如:

1、axis=0时,对饮搞得是第一个维度元素的相加,

[[0,1,2,3],[4,5,6,7]]和[[1,2,3,4],[5,6,7,8]]对应元素相加[[0+4,1+2,2+3,3+4],[4+5,5+6,7+7,7+8]]=[[1,3,5,7],[9,11,14,16]]

2、axis=1时, 对应的是第二个维度元素相加,这时候保留第一个维度的结构(第一个维度元素的个数),

第一个维度元素的个数为2。分别是

[[0,1,2,3],[4,5,6,7]]和[[1,2,3,4],[5,6,7,8]]

结构不变,继续向下拆分,可得

(1)[0,1,2,3]和[4,5,6,7], 对应元素相加, 合并到一个数组中, [4,6,8,10]

(2)[1,2,3,4]和[5,6,7,8], 对应元素相加, 合并到一个数组中, ,6,8,10,12]

3、当axis=2时, 因为元素一共是三维,这就到了最后一个维度,将最小单位的数组元素相加即可。

[0+1+2+3,4+5+6+7],[[1+2+3+4],[5+6+7+8]]=[[6,22],[10,26]]

python代码:

import numpy as np

data=np.array([[[0,1,2,3],[4,5,6,7]],[[1,2,3,4],[5,6,7,8]]])

sum=data.sum()

sum0=data.sum(axis=0)

sum1=data.sum(axis=1)

sum2=data.sum(axis=2)

print "sum: ",sum

print "axis=0: ",sum0

print "axis=1: ",sum1

print "axis=2: ",sum2

结果:

以上这篇对python中array.sum(axis=?)的用法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过将多个函数的数据合并成一个 NumPy 数组,然后在同一个子图对象上使用 plt.plot() 函数来绘制多条线路。下面是一个示例代码,其包括两个函数 plot_rate() 的调用,用于在同一张图上绘制两条线路: ``` python import numpy as np import matplotlib.pyplot as plt def plot_rate(rate_his, rolling_intv=50, ylabel='标准化计算速率', ax=None): import pandas as pd import matplotlib as mpl rate_array = np.asarray(rate_his) df = pd.DataFrame(rate_his) if ax is None: fig, ax = plt.subplots(figsize=(15, 8)) mpl.style.use('seaborn') ax.plot(np.arange(len(rate_array))+1, np.hstack(df.rolling(rolling_intv, min_periods=1).mean().values), 'b') ax.fill_between(np.arange(len(rate_array))+1, np.hstack(df.rolling(rolling_intv, min_periods=1).min()[0].values), np.hstack(df.rolling(rolling_intv, min_periods=1).max()[0].values), color='b', alpha=0.2) ax.set_ylabel(ylabel) ax.set_xlabel('Time Frames') # Generate some sample data N = 1000 Q = np.random.normal(0.0, 1.0, (N, 10)) energy = np.random.normal(10.0, 1.0, (N, 10)) # Create a figure with two subplots fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(15, 10)) # Plot the data queue rate on the first subplot plot_rate(Q.sum(axis=1)/N, 100, 'Average Data Queue', ax=ax1) # Plot the energy consumption rate on the second subplot plot_rate(energy.sum(axis=1)/N, 100, 'Average Energy Consumption', ax=ax2) # Show the plot plt.show() ``` 这段代码将生成两个包含随机数据的 NumPy 数组 Q 和 energy。我们使用这些数组的数据来调用 plot_rate() 函数,将数据队列速率和能量消耗速率绘制在同一张图上的两个子图。在这个示例,我们使用了 plt.subplots() 函数来创建一个包含两个子图的图形,然后在每个子图上调用 plot_rate() 函数来绘制数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值