【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除 ...
随机推荐
- Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)
在使用git pull拉取服务器最新版本时,如果出现error: Your local changes to the following files would be overwritten by m ...
- python模块之XlsxWriter 详解
Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,numbers,formulas 公式以及hyperlinks超链接. 可以完成xlsx文件的自动化构造,包括 ...
- 华农校赛--G,用set比较大小,缩短时间复杂度
Array C Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 581 Solved: 101[Submit][Status][Web Board] D ...
- 一款基于jquery的喜欢动画按钮
今天给大家带来一款基于jquery的喜欢动画按钮.这个实例中给了三种动画特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <p class='heading'> C ...
- 网页打开新窗口——Window.open()详解
转载自:http://blog.csdn.net/business122/article/details/8281142 Window.Open详解 一.window.open()支持环境:JavaS ...
- 示例 - 如何在多线程中应用SpiderStudio生成的DLL?
>> 接上文 "示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?", 将其改成多线程: 代码: using System; using ...
- elasticsearch安装与使用(3)-- 安装中文分词插件elasticsearch-analyzer-ik
前言 elasticsearch(下面简称ES,安装ES点击这里)的自带standard分词只能把汉语分割成一个个字,而不能分词.分段,这就是我们需要分析器ik的地方了. http://{ip}:92 ...
- C++ 类的对象管理模型初讲
//类的对象管理模型初讲 #include<iostream> using namespace std; class PointA{ private: int x;//占据4个字节大小的内 ...
- Android SDK代理server解决国内不能更新下载问题
读者须知:本篇文章中最靠谱的是第三种方式,近期有读者反映第三种方式也不行了,以下提供一点其它途径的开源镜像网站: 国内高校的开源镜像站 中国科学技术大学(debian.ustc.edu.cn) 上海交 ...
- epplus excel数据导出(数据量有点大的情况) Web和Client
Asp.net MVC后台代码 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.Exc ...