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 ...
随机推荐
- Console程序下监控EFCore生成的SQL语句!
最近这两天在使用控制台程序学习EFCore,突然想看看生成的SQL语句,所以在网上找到一位大神的分享的方法! 准备工作: 1). MySqlEFCore NuGet: Pomelo.EntityF ...
- windows 10 配置Java 环境变量(5步骤)
前提:1.windows 10 系统(不是win8,也不是win7)2.安装JDK步骤 1. 打开 环境变量窗口右键 This PC(此电脑) -> Properties(属性) -> A ...
- 【C#】Winform 令人困擾的畫面閃爍問題解法
DoubleBuffered = true 如果 Control 沒有這個屬性,可以使用我下列擴充函式進行設定︰ public static void SetDoubleBuffered<T&g ...
- python 内建模块与第三方模块
*)datetime模块 包括时间.时间对象.时间戳.时区.时区的转换 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/101764878 ...
- C#实现outlook自动签名
Outlook下实现自动签名的方式 网上找到一篇资料是在outlook里用vba实现的,但是这样实现的方式由于数字认证的问题不便于部署 在此介绍一种C#下实现的方式,目前确定的outlook版本为 ...
- 关于final
最近见的一道选择题 刚学习一直认为final修饰,为常量,必须声明时被初始化,现在又明白第二种情况可以通过创建对象之后由构造方法立即初始化. 1.final修饰类不能被继承 2.final修饰方法不能 ...
- i春秋——“百度杯”CTF比赛 九月场——Test(海洋cms / seacms 任意代码执行漏洞)
打开发现是海洋cms,那就搜索相关漏洞 找到一篇介绍海洋cms的命令执行漏洞的文章:https://www.jianshu.com/p/ebf156afda49 直接利用其中给出的poc /searc ...
- Odoo甘特图
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826366.html 甘特图(注意:社区版不支持甘特图!所以不会显示) 水平条状的甘特图通常用于显示项目计 ...
- MySQL字符集、information_schema元数据(八)
一.SQL字符集 它是一个系统支持的所有抽象字符的集合.字符是各种文字和符号的总称,包括各国家的文字.标点符号.图形符号.数字等 常用的字符集有:utf8.utf8mb4.utf8mb3(8.0),现 ...
- 网传英特尔酷睿第十代桌面处理器(Comet Lake 14nm)规格
自从农企(AMD)2016年开始崛起时,牙膏厂(英特尔)就开始发力,陆续两代推出性价比颇高的桌面处理器, 第八代.第九代酷睿桌面处理器相当的给力,而第十代酷睿桌面处理器会很猛啊,据传从酷睿i3到酷睿i ...