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 ...
随机推荐
- 构建简单Windows Service示例
示例源码:WindowsServiceSample ServiceHelper源码:ServiceHelper 1. 创建Windows Service项目,如图: 2. 配置服务参数 3. 安装,启 ...
- C# 文件监听类 FileSystemWatcher 属性
属性: Path——这个属性告诉FileSystemWatcher它需要监控哪条路径.例如,如果我们将这个属性设为“C:Temp”,对象就监控那个目录发生的所有改变.IncludeSubDirecto ...
- python \r与\b的应用、光标的含义
参考链接:https://www.jianshu.com/p/eb5c23cd6e34 \r 能将光标定位到当前行的行首 \b则是将光标回退一位 光标的含义: 光标后面的输出内容均会消失,光标回退后, ...
- Java常用类StringBuffer详解
内容多为最近学习的自我总结,可能有些地方写的不严谨,甚至会有错误的地方,仅供参考,如发现错误敬请指出,谢谢! 灰色字体为补充扩展内容,多为帮助自己理解. StringBuffer概述: 线程安全的可变 ...
- 安装Ubuntu linux
(1)下载Ubuntu http://www.ubuntu.com/download/desktop (2)制作启动U盘 1. 启动Rufus: 2. 插入U盘: 3. Rufus会提示更新,以自动选 ...
- 隐马尔科夫模型(Hidden Markov Models) 系列之五
转自:http://blog.csdn.net/eaglex/article/details/6458541 维特比算法(Viterbi Algorithm) 找到可能性最大的隐藏序列 通常我们都有一 ...
- vue+element下拉框样式的点击按钮
项目中点击按钮实在太多了,怎么办呢?我们就可以将它们制作成像下拉框那样的类似操作 1.HTML样式部分:关键点在于command 方法和属性 1 <el-dropdown 2 size=&quo ...
- elasticsearch 安装 可视化工具
一.windows下安装Elasticsearch首先计算机需要JAVA环境(已有次此环境跳过)1.java环境安装网址:http://www.oracle.com/technetwork/java/ ...
- MySQL基础:DCL语句总结
SQL语言大致分为DCL.DDL.DML三种,本文主要介绍MySQL 5.7版本DCL语句. 概述 DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可 ...
- Tomcat服务器编码格式设置
/** *1.找到.xml server文件 */ /** * 2. 设置encoding */