python 判断矩阵中每行非零个数的方法
python 判断矩阵中每行非零个数的方法:
# -*- coding: utf-8 -*-
# @Time : 2018/5/17 15:05
# @Author : Sizer
# @Site :
# @File : test.py
# @Software: PyCharm
import time
import numpy as np # data = np.array([
# [5.0, 3.0, 4.0, 4.0, 0.0],
# [3.0, 1.0, 2.0, 3.0, 3.0],
# [4.0, 3.0, 4.0, 3.0, 5.0],
# [3.0, 3.0, 1.0, 5.0, 4.0],
# [1.0, 5.0, 5.0, 2.0, 1.0]
# ])
data = np.random.random((1000, 1000))
print(data.shape)
start_time = time.time()
# avg = [float(np.mean(data[i, :])) for i in range(data.shape[0])]
# print(avg) start_time = time.time()
avg = []
for i in range(data.shape[0]):
sum = 0
cnt = 0
for rx in data[i, :]:
if rx > 0:
sum += rx
cnt += 1
if cnt > 0:
avg.append(sum/cnt)
else:
avg.append(0)
end_time = time.time()
print("op 1:", end_time - start_time) start_time = time.time()
avg = []
isexist = (data > 0) * 1
for i in range(data.shape[0]):
sum = np.dot(data[i, :], isexist[i, :])
cnt = np.sum(isexist[i, :])
if cnt > 0:
avg.append(sum / cnt)
else:
avg.append(0)
end_time = time.time()
print("op 2:", end_time - start_time)
#
# print(avg)
factor = np.mat(np.ones(data.shape[1])).T
# print("facotr :")
# print(factor)
exist = np.mat((data > 0) * 1.0)
# print("exist :")
# print(exist)
# print("res :")
res = np.array(exist * factor)
end_time = time.time()
print("op 3:", end_time-start_time) start_time = time.time()
exist = (data > 0) * 1.0
factor = np.ones(data.shape[1])
res = np.dot(exist, factor)
end_time = time.time()
print("op 4:", end_time - start_time)
第四种实现方式的效率最高!
python 判断矩阵中每行非零个数的方法的更多相关文章
- python 判断字符串中是否只有中文字符
python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...
- python判断字符串中是否包含子字符串
python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1: print('存在') else: print('不存在' ...
- python判断list中是否包含某个元素
python判断list中是否包含某个元素 theList = ['a','b','c'] if 'a' in theList: print 'a in the list' if 'd' not in ...
- lintcode-401-排序矩阵中的从小到大第k个数
401-排序矩阵中的从小到大第k个数 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 样例 给出 k = 4 和一个排序矩阵: [ [1 ,5 ,7], [ ...
- Java:判断字符串中包含某字符的个数
Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...
- JAVA判断字符串中某个字符存在的个数
/** * 判断字符串中某个字符存在的个数 * @param str1 完整字符串 * @param str2 要统计匹配个数的字符 * @return */ public static int co ...
- [google面试CTCI] 1-7.将矩阵中特定行、列置0
[字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- python 删除文件中指定行
代码适用情况:xml文件,循环出现某几行,根据这几行中的某个字段删掉这几行这段代码的作用删除jenkins中config.xml中在自动生成pipline报错的时的回滚 start = '<se ...
随机推荐
- 记录RFID操作错误
如果代码操作不了RFID设备,查看下通信协议的设置
- Sharding-Jdbc概念与使用技巧
1. Sharding-Jdbc概念与使用技巧 此讲解版本为4.0.0-RC1,目前最新的版本 2019年5月21日发布 1.1. 绑定表 指分片规则一致的主表和子表.例如:t_order表和t_or ...
- tp5.0在控制器中和在模板中调用配置文件中的常量
框架配置文件config.php中定义 'view_replace_str' => [ '__MEMBER__'=> '/static/member', '__uplo ...
- 结合 Vue.observable 写一个简易 Vuex
作为 Vue 全家桶的一员,Vuex 的重要性不言而喻,不管是用来管理状态,还是封装 Controler 都很好用 不过在一些体量较小的项目中,为了几个简单的状态或者处理函数而引入 Vuex,就像是高 ...
- ecshop 中的$GLOBALS
ec二次开发 或研究ec的一些网友 经常在论坛里提到 $GLOBALS['db']; $GLOBALS['ecs'];在那定义的等帖子. 下来就$GLOBALS我说一点: 想搞明白的朋友 ...
- 《Android开发艺术探索》读书笔记之IntentFillter的匹配规则
使用intent启动不同组件的方法 组件类型 启动方法 Activity startActivity(Intent intent) startActivityForResult(Intent inte ...
- Tomcat服务器编码格式设置
/** *1.找到.xml server文件 */ /** * 2. 设置encoding */
- springmvc配置访问静态文件
xmlns:mvc="http://www.springframework.org/schema/mvc" <mvc:annotation-driven /><m ...
- 目标检测论文解读7——YOLO v2
背景 YOLO v1检测效果不好,且无法应用于检测密集物体. 方法 YOLO v2是在YOLO v1的基础上,做出如下改进. (1)引入很火的Batch Normalization,提高mAP和训练速 ...
- ThinkPHP5框架引入的css等外部资源文件没有生效
静态资源文件一般是放在public目录里,不只是css,只要是静态资源文件都没有显示出来. (更好的阅读体验可访问 这里 ) 问题陈述 文件结构 文件内容 三个文件分别为:Index.php.test ...