目录:

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. C#高级编程笔记 (6至10章节)运算符/委托/字符/正则/集合

    数学的复习,4^-2即是1/4/4的意思, 4^2是1*2*2的意思,而10^-2为0.01! 7.2运算符 符号 说明 例   ++ 操作数加1 int i=3; j=i++; 运算后i的值为4,j ...

  2. centos 6.5 解压 tar.gz

    只查看 tar.gz 文件内容而不解压 tar -tvf filename.tar.gz 解压为 tar 文件 gzip -d filename.tar.gz 解压为文件或目录 tar xvf fil ...

  3. yield关键字详解与三种用法

    本篇文章比较硬核, 适合有一定Python基础的读者阅读, 如果您对Python还不甚了解可以先关注我哦, 我会持续更新Python技术文章 yield详解 yield与return相同每次调用都会返 ...

  4. 【leetcode】1020. Number of Enclaves

    题目如下: Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists ...

  5. ubuntu下安装pyaudio

    首先安装依赖库,不安装依赖库会安装失败: sudo apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocp ...

  6. Bugku | 入门逆向

    感觉这题偏向于misc ,Orz 用ida打开: 解码:

  7. Win7系统取消登录界面的两种方法(图文)

    windows7系统设置电脑密码后,即使取消密码,也会出现登录界面 ,每次都要点击用户图标才能进入系统,这样比较麻烦.那么有什么办法可以取消登录界面呢?方法当然是有的,阅读下文教程,我们一起来看下Wi ...

  8. php面试专题---2、常量及数据类型考点

    php面试专题---2.常量及数据类型考点 一.总结 一句话总结: 变量为null和变量判断为false的情况需要仔细注意下 1.PHP中字符串可以使用哪三种定义方法以及各自的区别是什么? 单引号:不 ...

  9. English-accomplish、complete、finish、achieve and fulfill

    accomplish to succeed in doing something, especially after trying very hard vt. 完成:实现:达到 complete us ...

  10. Linux 下使用 nohup

    参考: https://www.cnblogs.com/klb561/p/10153834.html ppending output to nohup.out 嗯,证明运行成功,同时把程序运行的输出信 ...