【python】常用函数
使用list生成dict(可指定单条长度和数据类型,splen为4即为list中每4行组成dict中一条)
def list2dict(srclist,splen,datatype):# datatype: 0-str 1-int 2-float
dstdict={}
for i in range(0,int(len(srclist)/splen)):
dstdict[srclist[splen*i]]=[]
for j in range(1,splen):
if datatype==0:
dstdict[srclist[splen*i]].append(srclist[splen*i+j])
elif datatype==1:
dstdict[srclist[splen*i]].append(int(srclist[splen*i+j]))
elif datatype==2:
dstdict[srclist[splen*i]].append(float(srclist[splen*i+j]))
else:
print 'please choose the datatype'
return False
return dstdict
计算两个list的距离(注意不能用numpy的linalg.norm,因为用numpy的linalg.norm的话如果两个list相等就会报错(因为求了0向量的范数))
def cal_list_dist(srclist1,srclist2):
if len(srclist1)!=len(srclist2):
print 'error in cal_list_dict, len not equal!'
return False
listerror=0
for i in range(0,len(srclist1)):
listerror+=pow(srclist1[i]-srclist2[i],2)
return pow(listerror,0.5)
比较按行存的label的误差
例如可以比较这样的list中名称相同的训练样本的误差:

将黄色处生成dict的间隔改为3,则可以比较这样的list

import sys def list2dict(srclist,splen,datatype):# datatype: 0-str 1-int 2-float
dstdict={}
for i in range(0,int(len(srclist)/splen)):
dstdict[srclist[splen*i]]=[]
for j in range(1,splen):
if datatype==0:
dstdict[srclist[splen*i]].append(srclist[splen*i+j])
elif datatype==1:
dstdict[srclist[splen*i]].append(int(srclist[splen*i+j]))
elif datatype==2:
dstdict[srclist[splen*i]].append(float(srclist[splen*i+j]))
else:
print 'please choose the datatype'
return False
return dstdict def cal_list_dist(srclist1,srclist2):
if len(srclist1)!=len(srclist2):
print 'error in cal_list_dict, len not equal!'
return False
listerror=0
for i in range(0,len(srclist1)):
listerror+=pow(srclist1[i]-srclist2[i],2)
return pow(listerror,0.5) p_name=sys.argv[1] with open('./10_10_662_withcam0/'+p_name) as f:
withcam0_list=f.read().splitlines() with open('./10_10_662_nocam0/'+p_name) as f:
nocam0_list=f.read().splitlines() withcam0_dict=list2dict(withcam0_list,4,2) nocam0_dict=list2dict(nocam0_list,4,2) sumerror=0
sumcount=0
for elem in withcam0_dict:
if elem in nocam0_dict:
sumerror+=cal_list_dist(withcam0_dict[elem],nocam0_dict[elem])
sumcount+=1
print sumerror/sumcount
【python】常用函数的更多相关文章
- Python常用函数记录
Python常用函数/方法记录 一. Python的random模块: 导入模块: import random 1. random()方法: 如上如可知该函数返回一个[0,1)(左闭右开)的一个随机的 ...
- Python常用函数、方法、模块记录
常用函数: 1.pow():乘方 2.abs():绝对值 3.round():四舍五入 4.int():转换为整数 5.input():键盘输入(会根据用户的输入来做类型的转换) raw_input( ...
- python常用函数年初大总结
1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...
- python常用函数总结
原文地址https://www.cnblogs.com/nice107/p/8118876.html 我们在学习python的时候,接触最多的往往则是那些函数,对于python函数,在这里为大家总结归 ...
- 超级干货,python常用函数大总结
我们在学习python的时候,接触最多的往往则是那些函数,对于python函数,在这里为大家总结归纳了这些,如果有缺漏,还请及时留言指正哦! 话不多说,干货来袭! 1.常用内置函数:(不用import ...
- python常用函数库收集。
学习过Python都知道python中有很多库.python本身就是万能胶水,众多强大的库/模块正是它的优势. 收集一些Python常用的函数库,方便大家选择要学习的库,也方便自己学习收集,熟悉运用好 ...
- python常用函数拾零
Python常用内置函数总结: 整理过程中参考了runoob网站中python内置函数的相关知识点,特此鸣谢!! 原文地址:http://www.runoob.com/python/python-bu ...
- python 常用函数集合
1.常用函数 round() : 四舍五入 参数1:要处理的小数 参数2:可选,如果不加,就是不要小数,如果加,就是保留几位小数 abs() :绝对值 ...
- Python | Python常用函数、方法示例总结(API)
目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...
- python常用函数及模块
原文来源于博客园和CSDN 1.计算函数 abs()--取绝对值 max()--取序列最大值,包括列表.元组 min()--取序列最小值 len()--取长度 divmod(a,b)---取a//b除 ...
随机推荐
- 访问子节点childNodes
访问子节点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...
- CodeForces 558D
Guess Your Way Out! II Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- hdu1078(记忆化搜索)
题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 这道题目,值得我记住它,re了n次,以前写搜索没有注意的一个小地方,导致re这么多次的 ac代码: #inc ...
- fontDialog-字体对话框和colorDialog-颜色对话框
private void button1_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); ...
- js监听文本框变化事件
用js有两种写法: 法一: <!DOCTYPE HTMl> <html> <head> <title> new document </title& ...
- DRBD(Distributed Replicated Block Device) 分布式块设备复制 进行集群高可用方案
DRBD是一个用软件实现的.无共享的.服务器之间镜像块设备内容的存储复制解决方案. 外文名 DRBD drbdadm 高级管理工具 drbdsetup 置装载进kernel的DRBD模块 drbdme ...
- PostgreSQL视频去重 图片去重系列1
PostgreSQL 在视频.图片去重,图像搜索业务中的应用 图片搜索 PostgreSQL的图像搜索插件使用了非常主流的Haar wavelet技术对图像进行变换后存储 gist 索引方法(支持pa ...
- python手册
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
- 007Maven_在Myeclipse创建web项目
创建Web项目 l 选择创建一个普通的web工程
- mysql -- 按时间查询 今天、昨天、明天、上月....
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO ...