目录:

1. 数组每一行除以这一行的总数(numpy divide row by row sum)

2. 数组每一行或者每一列求平均 (python average array columns or rows)

3. 数组每一行或者每一列求加权平均 (python weight average array columns or rows)

4. 计算数组得到每一行或者每一列的和 (python sum columns of an array)

5. 生成指定维度的随机矩阵 (python generate random array)

6. 数组中对元素进行布尔类型判断 (python check elements in array with Boolean type)

7. 数组中是否存在满足条件的数 (python check if exsit element in array satisfies a condition)

8. 数组中所有元素是否有0元素 (python check whether all elements in numpy is zero)

内容:

1. 数组每一行除以这一行的总数(numpy divide row by row sum)

https://stackoverflow.com/questions/16202348/numpy-divide-row-by-row-sum

方法1:

>>> e
array([[ 0., 1.],
[ 2., 4.],
[ 1., 5.]])
>>> e/e.sum(axis=1)[:,None]
array([[ 0. , 1. ],
[ 0.33333333, 0.66666667],
[ 0.16666667, 0.83333333]])

方法2:

>>> (e.T/e.sum(axis=1)).T
array([[ 0. , 1. ],
[ 0.33333333, 0.66666667],
[ 0.16666667, 0.83333333]])

方法3:

>>> e/e.sum(axis=1, keepdims=True)
array([[ 0. , 1. ],
[ 0.33333333, 0.66666667],
[ 0.16666667, 0.83333333]])

2. 数组每一行或者每一列求平均 (python average array columns or rows)

import numpy as np
In [50]: a=np.arange(1,13).reshape(3,4)
In [51]: a
Out[51]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
In [52]: np.average(a, axis=1)
Out[52]: array([ 2.5, 6.5, 10.5])
In [53]: np.average(a, axis=0)
Out[53]: array([5., 6., 7., 8.])

3. 数组每一行或者每一列求加权平均 (python weight average array columns or rows)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html

>>> data = np.arange(6).reshape((3,2))
>>> data
array([[0, 1],
[2, 3],
[4, 5]])
>>> np.average(data, axis=1, weights=[1./4, 3./4])
array([0.75, 2.75, 4.75])

  

4. 计算数组得到每一行或者每一列的和 (python sum columns of an array)

https://stackoverflow.com/questions/13567345/how-to-calculate-the-sum-of-all-columns-of-a-2d-numpy-array-efficiently

>>> import numpy as np
>>> a = np.arange(12).reshape(4,3)
>>> a.sum(axis=0)
array([18, 22, 26])
>>> a.sum(axis=1)
array([ 3, 12, 21, 30])

5. 生成指定维度的随机矩阵 (python generate random array)

https://www.codespeedy.com/how-to-create-matrix-of-random-numbers-in-python-numpy/

(1)生成指定维度的小数数组

In [1]: import numpy as np

In [2]: a=np.random.rand(3,4)

In [3]: a
Out[3]:
array([[0.03403289, 0.31416715, 0.42700029, 0.49101901],
[0.70750959, 0.4852401 , 0.11448147, 0.21570702],
[0.87512839, 0.82521751, 0.56915875, 0.67623931]])

(2)生成只能维度的整数数组

In [8]: np.random.randint(1,10,size=(3,4))
Out[8]:
array([[8, 1, 4, 3],
[7, 1, 8, 7],
[2, 5, 4, 3]])

6. 数组中对元素进行布尔类型判断 (python check elements in array with Boolean type)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.all.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.any.html

>>> np.all([-1, 4, 5])
True >>> np.all([[True,False],[True,True]])
False >>> np.all([[True,False],[True,True]], axis=0)
array([ True, False]) // 如果要判断至少存在一个元素则使用 >>> np.any([-1, 0, 5])
True >>> np.any([[True, False], [True, True]])
True >>> np.any([[True, False], [False, False]], axis=0)
array([ True, False])

7. 数组中是否存在满足条件的数 (python check if exsit element in array satisfies a condition)

In [1]: import numpy as np

In [2]: a=np.arange(1, 13).reshape(3, 4)

In [3]: a
Out[3]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]]) In [4]: a>7
Out[4]:
array([[False, False, False, False],
[False, False, False, True],
[ True, True, True, True]]) In [5]: np.any(a>7)
Out[5]: True In [6]: np.all(a>7)
Out[6]: False

8. 数组中所有元素是否有0元素 (python check whether all elements in numpy is zero)

https://stackoverflow.com/questions/18395725/test-if-numpy-array-contains-only-zeros

In [1]: import numpy as np

In [2]: not np.any(np.array([0, 0, 2]))
Out[2]: False In [3]: not np.any(np.array([0, 0, 0]))
Out[3]: True

// 计算非零个数再进行判断
In [4]: np.count_nonzero(np.array([0, 0, 2]))
Out[4]: 1 In [5]: np.count_nonzero(np.array([0, 0, 0]))
Out[5]: 0

// 用集合去掉重复元素再判断
In [6]: set(np.array([0, 0, 2]))
Out[6]: {0, 2} In [7]: set(np.array([0, 0, 0]))
Out[7]: {0}

9.

python 常用技巧 — 数组 (array)的更多相关文章

  1. python 常用技巧

    一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...

  2. python常用技巧 — 杂

    目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...

  3. python 常用技巧 — 列表(list)

    目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...

  4. python常用技巧

    1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中:    通过fi ...

  5. python 常用技巧 — 字典 (dictionary)

    目录: 1. python 相加字典所有的键值 (python sum all values in dictionary) 2. python 两个列表分别组成字典的键和值 (python two l ...

  6. Python NumPy中数组array.min(0)返回数组

    如果没有参数min()返回一个标量,如果有参数0表示沿着列,1表示沿着行.

  7. #1 Python灵活技巧

    前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...

  8. python算法常用技巧与内置库

    python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...

  9. php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】

    本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...

随机推荐

  1. 理解Java构造器中的"this"

    Calling Another Constructor if the first statement of a constructor has the form this(...), then the ...

  2. Sublime Text 注册及使用相关

    sublime text3 注册码 2019-07-01 注册码可以直接用 地址: 2019-07-01 亲测可用 2019-07-18 亲测可用 -– BEGIN LICENSE -– Die So ...

  3. hdu 5964:平行四边形 【计算几何】

    打重现赛时,一点思路也没有,然后又看到这题AC数那么少,就直接放弃了.今天重新看了看,借鉴了下别人的,发现此题应该算是一道可解题. 看上去,这题的ans是同时有两个点作为自变量的函数(然而n^2复杂度 ...

  4. JMeter生成UUID方式

    1. 使用JMeter工具中自带的函数__UUID 2. 使用Beanshell组件,在脚本中引入java.util.UUID,通过java来生成 import java.util.UUID; UUI ...

  5. BZOJ 4399: 魔法少女LJJ(线段树)

    传送门 解题思路 出题人真会玩..操作\(2\)线段树合并,然后每棵线段树维护元素个数和.对于\(6\)这个询问,因为乘积太大,所以要用对数.时间复杂度\(O(nlogn)\) 代码 #include ...

  6. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  7. [CSP-S模拟测试]:施工(DP+单调栈+前缀和)

    题目描述 小$Y$家门前有一条街道,街道上顺序排列着$n$幢建筑,其中左起第$i$幢建筑的高度为$h_i$.小$Y$定义街道的不美观度为所有相邻建筑高度差的绝对值之和乘上常数$c$,为了改善街道环境, ...

  8. Extjs的一些基础使用!

    一.获取元素(Getting Elements) 1. Ext.get() var el = Ext.getCmp('id');//获取元素,等同于document.getElementById('i ...

  9. MySQL-初始化和自动更新TIMESTAMP和DATETIME

    https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html 例,添加自动更新的保存最后一次修改该条记录的时间戳的字段: ...

  10. JS中基本数据类型和引用类型最根本的区别

    栈内存和堆内存:https://segmentfault.com/a/1190000015118062 https://segmentfault.com/a/1190000016389376 变量:内 ...