python 常用技巧 — 数组 (array)
目录:
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)
>>> 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)的更多相关文章
- python 常用技巧
一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...
- python常用技巧 — 杂
目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...
- python常用技巧
1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中: 通过fi ...
- python 常用技巧 — 字典 (dictionary)
目录: 1. python 相加字典所有的键值 (python sum all values in dictionary) 2. python 两个列表分别组成字典的键和值 (python two l ...
- Python NumPy中数组array.min(0)返回数组
如果没有参数min()返回一个标量,如果有参数0表示沿着列,1表示沿着行.
- #1 Python灵活技巧
前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...
- python算法常用技巧与内置库
python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...
- php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】
本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...
随机推荐
- Python 语音识别
调用科大讯飞语音听写,使用Python实现语音识别,将实时语音转换为文字. 参考这篇博客实现的录音,首先在官网下载了关于语音听写的SDK,然后在文件夹内新建了两个.py文件,分别是get_audio. ...
- python基础:1.位、字节、字的关系
1.位,简称b,或bit,比特,数据存储的最小单位.每个二进制数字0或1就是一个位(bit),网络通信常用bps,bit per second ,每秒传输多少位 2.字节,简称byte, 1byte ...
- 【Shiro】SpringBoot集成Shiro
项目版本: springboot2.x shiro:1.3.2 Maven配置: <dependency> <groupId>org.apache.shiro</grou ...
- session应用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...
- hdu 1451 Area in Triangle(计算几何 三角形)
Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope t ...
- C#正则表达式将html代码中的所有img标签提取
/// <summary> /// 取得HTML中所有图片的 URL. /// </summary> /// <param name="sHtmlText&qu ...
- 转载:VScode 好用插件集合
--------------------- 作者:JayveeWong 来源:CSDN 原文:https://blog.csdn.net/weixin_42776111/article/details ...
- sublime常用基础插件合集
插件介绍 Package Control 功能:安装包管理简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能使用方法:快捷键 Ctrl+Shift+P,输入 install 选中In ...
- (转)JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
转:https://my.oschina.net/feichexia/blog/196575?p=3 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不 ...
- 广播接收者实现IP拨号
广播接收者实现IP拨号 效果图: 实现的功能就是自动监听我们要拨打的号码,在我们拨打的号码前加上179521 分析: 1.敲个类来继承广播接收者 并且将从打电话应用位置获取的号码加上179521,并将 ...