![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
NockinOnHeavensDoor
blog已不用~谢谢关注~
展开
-
defaultdict
defaultdict 接受不带参数的可调用函数:from collections import defaultdictwords = ['hello', 'world', 'nice', 'world']#使用lambda来定义简单的函数counter = defaultdict(lambda: 0) for kw in words: counter[kw] += 1def...转载 2019-03-07 17:45:02 · 227 阅读 · 0 评论 -
collections 模块
collections 模块 namedtuple() factory function for creating tuple subclasses with named fields deque list-like container with fast appends and pops on either end ChainMap dict-like class for...原创 2018-06-11 08:50:06 · 231 阅读 · 0 评论 -
python的Tqdm模块
基本用法: 1.from time import sleepfrom tqdm import tqdm for i in tqdm(range(10000)): sleep(0.1) ``100%|██████████| 10000/10000 [17:00<00:00, 9.80it/s]2.import tqdmfor i in tqdm.t...转载 2018-06-10 22:13:26 · 461 阅读 · 0 评论 -
Python拷贝(深拷贝deepcopy与浅拷贝copy)
Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块。1、copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。2、copy.deepcopy 深拷贝 拷贝对象及其子对象>>> import copy>>> a = [1,2,3,4,['a','b']] #原始对象>>&g...转载 2018-06-10 22:07:49 · 2096 阅读 · 0 评论 -
集合操作
集合操作集合运算比较集合(包含)关系不可变集合frozenset:不提供add()\remove()等集合应用案例集合操作集合只能包含可哈希的数据,如数字、字符串、元组等不可变类型的数据,内置hash函数异常的对象都不能作为集合的元素,也不能作为字典对象的“键”#创建集合a = {3, 5}type(a)seta_set = set(ran...原创 2018-06-10 15:42:27 · 902 阅读 · 0 评论 -
python文件IO
读取键盘输入"""在python3.x中raw_input( )和input( )进行了整合,去除了raw_input( ),仅保留了input( )函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。"""str = input("请输出:")print("你输入的内容是:", str)请输出:Have a nice day!你输入的内容是: Have...转载 2018-06-10 14:31:32 · 196 阅读 · 0 评论 -
python:zipfile模块
zipfile 是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高。zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下,只需要使用这两个class就可以。1)ZipFile是主要的类,用来创建和读取zip文件;2)ZipInfo是存储的zip文件的每个文件的信息的。#!...转载 2018-06-23 08:43:31 · 619 阅读 · 0 评论 -
network API: read_adjlist
Read graph in adjacency list format from path.Parameters: path (string or file) – Filename or file handle to read. Filenames ending in .gz or .bz2 will be uncompressed.create_using (NetworkX graph...原创 2018-05-15 06:02:30 · 1588 阅读 · 0 评论 -
转:Python文件与文件夹常见基本操作总结
1、判断文件(夹)是否存在。2、判断路径名是否为文件。3、判断路径名是否为目录。4、创建文件。5、复制文件。6、删除文件。7、清空文件。8、创建目录。10、重命名文件或目录。11、移动文件或目录。12、删除目录。12.1、清空目录。13、切换目录。14、open常用模式。15、由全路径名的到路径和文件名。16、获取文件大小。17、获取当前文件目录绝对路径。...转载 2018-06-08 09:19:18 · 411 阅读 · 0 评论 -
python:名称前的单下划线
名称前的单下划线(如:_shahriar)程序员使用名称前的单下划线,用于指定该名称属性为“私有”。这有点类似于惯例,为了使其他人(或你自己)使用这些代码时将会知道以“_”开头的名称只供内部使用。正如Python文档中所述:以下划线“_”为前缀的名称(如_spam)应该被视为API中非公开的部分(不管是函数、方法还是数据成员)。此时,应该将它们看作是一种实现细节,在修改它们时无需对外部通知...转载 2018-06-07 07:38:35 · 2287 阅读 · 1 评论 -
python:装饰器
装饰器(Decorators)是Python的一个重要部分。 简单地说: 他们是修改其他函数的功能的函数。 他们有助于让我们的代码更简短, 也更Pythonic( Python范儿)def a_new_decorator(a_func): def wrapTheFunction(): print("I am doing some boring work before ex...原创 2018-07-02 06:52:53 · 193 阅读 · 0 评论 -
python os和sys模块的区别
os与sys模块的官方解释如下:os: This module provides a portable way of using operating system dependent functionality.这个模块提供了一种方便的使用操作系统函数的方法。sys: This module provides access to some variables used or maintain...转载 2018-07-02 13:48:35 · 826 阅读 · 0 评论 -
ConfigParser
基础读取配置文件read(filename) 直接读取文件内容sections() 得到所有的section,并以列表的形式返回options(section) 得到该section的所有optionitems(section) 得到该section的所有键值对g...原创 2018-07-02 21:57:29 · 532 阅读 · 0 评论 -
python对象都用一个字典来保存其命名空间 : __dict__
class C: def __init__(self): self.x = 1 self.y = 2 def regist(self, key, val): self.__dict__[key] = valc = C()print c.__dict__c.regist("z", 3)c.regist("func", lam...转载 2019-03-07 12:16:29 · 602 阅读 · 0 评论 -
networkx:add_edge会把节点同时加入图
原始图没有节点1:现在添加边1-5:final_H.add_edge(1,5)再次输出final_H.nodes()查看:原来节点加入在最后面了,一开始我还奇怪。。。以后会继续记录类似情况...原创 2018-12-21 20:40:09 · 2664 阅读 · 0 评论 -
字典建立反字典的时候values()不能重复
te = {x:y for x,y in zip(range(4),[1,2,1,3])}te{0: 1, 1: 2, 2: 1, 3: 3}{v:x for v,x in zip(te.values(),te.keys())}{1: 2, 2: 1, 3: 3}原创 2018-12-08 09:56:57 · 263 阅读 · 0 评论 -
sklearn:auc、roc_curve、roc_auc_score
sklearn.metrics.auc作用:计算AUC(Area Under the Curve)metrics.roc_curve作用:计算 ROC(Receiver operating characteristic)注意: this implementation is restricted to the binary classification tasksklearn.metric...原创 2018-10-25 19:23:04 · 20677 阅读 · 1 评论 -
python2和python3中的urllib
在Python3中包urllib2归入了urllib中,所以要导入urllib.request,并且要把urllib2替换成urllib.request# python2import urllib2url = 'http://www.jianshu.com/trending/weekly?page={}'headers = {'User-Agent': 'Mozilla/5.0 (Win...转载 2018-10-23 09:59:44 · 170 阅读 · 0 评论 -
argparse:解析命令行选项
argparse模块在每个 add_argument() 调用中,dest 参数指定解析结果被指派给属性的名字。 metavar 参数被用来生成帮助信息。action 参数指定跟属性对应的处理逻辑, 通常的值为 store ,被用来存储某个值或将多个参数值收集到一个列表中。# search.py'''Hypothetical command-line tool for searching ...转载 2018-10-20 16:41:57 · 271 阅读 · 0 评论 -
转:python opencv3不能导入cv问题
根据之前的帖子 https://blog.csdn.net/nanxiaoting/article/details/80396148,直接在anaconda下 pip install&am转载 2018-09-20 19:55:18 · 1508 阅读 · 0 评论 -
import一个不同目录的文件
python中,每个py文件被称之为模块,每个具有init.py文件的目录被称为包。只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用 如果你要使用的模块(py文件)和当前模块在同一目录,只要import相应的文件名就好,比 如在a.py中使用b.py: import b 但是如果要import一个不同目录的文件(例如b.py)该怎么...转载 2018-07-17 21:24:59 · 1518 阅读 · 0 评论 -
文件夹加入到sys.path
第二种方法是创建一个.pth文件,将目录列举出来,像这样# myapplication.pth/some/dir/other/dir这个.pth文件需要放在某个Python的site-packages目录,通常位于/usr/local/lib/python3.3/site-packages 或者 ~/.local/lib/python3.3/sitepackages。当解释器启动时...转载 2018-07-16 14:17:45 · 1576 阅读 · 1 评论 -
functools基本
functools:Higher-order functions and operations on callable objectsfunctools模块是一个高阶函数,即在函数上(any callable object)进行操作,并返回函数。 用一些默认参数包装一个可调用对象,返回结果是可调用对象,并且可以像原始对象一样对待冻结部分函数位置函数或关键字参数,简化函数,更少更灵活的函数...原创 2018-06-07 07:17:19 · 545 阅读 · 0 评论 -
random.seed()设置可复用的随机结果
例子: #Simple python programme to understand random.seed() importanceimport randomrandom.seed(10)print(random.randint(1,100))#和seed 10 结果一样random.seed(10)print(random.randint(1,100))random...原创 2018-05-30 10:28:03 · 2143 阅读 · 0 评论 -
tensorflow API: nn.in_top_k 、top_k
原文连接 对例子解释了一下,直接来吧:tf.nn.top_k(input, k, name=None)解释:这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。import tensorflow as tfimport numpy as npinput = tf.constant(np.random.rand(3,4))k = 2"""输出的每行最大的k个数转载 2018-01-03 18:21:22 · 550 阅读 · 0 评论 -
EffectivePython:str和bytes,生成器表达式,enumerate
1.用pythonic方式思考"""查看版本"""import sysprint(sys.version_info)print(sys.version)sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)3.6.3 |Anaconda custom (64-bit)| (default, Oct原创 2017-12-19 13:36:13 · 364 阅读 · 0 评论 -
评估红酒口感
评估红酒口感¶ 问题描述:属性是红酒的化学成分的测量指标,标签是评酒员的评分。import pandas as pdfrom pandas import DataFramefrom pylab import *import matplotlib.pyplot as plttarget_url = ("http://archive.ics.uci.edu/ml/machine-" "lea原创 2017-12-18 16:40:19 · 710 阅读 · 0 评论 -
机器学习实用指南:Chapter 9 – Up and running with TensorFlow
Linear RegressionUsing the Normal Equation# 再python2和3上都支持from __future__ import division, print_function, unicode_literalsimport numpy as npimport osimport tensorflow as tf# 让botebook的输入不会改变def r原创 2017-12-31 14:31:41 · 583 阅读 · 0 评论 -
EffectivePython-函数:闭包、生成器、迭代器协议
15.在闭包里使用外围作用域中的变量问题描述:比如特殊的排序,给一个list中的数字排序,要求把出现在另一个list中的数字排前面。def sort_priority(values, group): def helper(x): if x in group: return (0, x) return (1, x) value原创 2017-12-21 15:47:57 · 352 阅读 · 0 评论 -
Tensorflow 机器学习项目实战 记录
**Tensorflow 机器学习项目实战 记录**基本操作简单矩阵运算import tensorflow as tfsess = tf.InteractiveSession()x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])原创 2017-12-08 14:51:17 · 2848 阅读 · 0 评论 -
读《ML 预测分析核心算法》(2)
读《ML 预测分析核心算法》(2)2.21 分类问题:用声纳发现未爆炸的水雷原创 2017-12-12 13:20:26 · 1659 阅读 · 0 评论 -
numpy : np.percentile使用
首先明确百分位数:第p个百分位数是这样一个值,它使得至少有p%的数据项小于或等于这个值,且至少有(100-p)%的数据项大于或等于这个值。举个例子:高等院校的入学考试成绩经常以百分位数的形式报告。比如,假设某个考生在入学考试中的语文部分的原始分数为54分。相对于参加同一考试的其他学生来说,他的成绩如何并不容易知道。但是如果原始分数54分恰好对应的是第70百分位数,我们就能知道大约70%的学生的考分比原创 2017-12-10 16:37:06 · 13466 阅读 · 0 评论 -
Word2vec tutorial implementation
基于这篇教程写的代码分析:http://adventuresinmachinelearning.com/word2vec-tutorial-tensorflow/The Word2Vec methodology two components to the Word2Vec methodology. * The first is the mapping of a high原创 2018-01-08 20:31:22 · 563 阅读 · 0 评论 -
anaconda安装pytorch
conda install numpy mkl cffi先加入清华镜像源,这样的话下载速度会快很多conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/conda config --set show_channel_urls yes# only support cpu...原创 2018-04-07 12:36:58 · 2094 阅读 · 1 评论 -
python模块configparser
configparser 模块能被用来读取配置文件。例如,假设你有如下的配置文件:; config.ini; Sample configuration file[installation]library=%(prefix)s/libinclude=%(prefix)s/includebin=%(prefix)s/binprefix=/usr/local# Setting ...转载 2018-04-24 10:10:29 · 348 阅读 · 0 评论 -
argparse 做命令行解析
步骤1.创建 ArgumentParser() 对象parser = argparse.ArgumentParser(description= "caculate X to the power of Y")2.调用 add_argument() 方法添加参数parser.add_argumen使用 parse_args() 获取已添加参数handler...原创 2018-05-29 14:07:00 · 547 阅读 · 0 评论 -
python之中attribute与property么区别
attribute首先把attribute理解为属性,property理解为属性函数。举例1:class Student(object): #把一个getter方法变成属性,只需要加上@property就可以了 @property def score(self): #名称前的单下划线,用于指定该名称属性为“私有”。 ret...原创 2018-06-04 08:26:10 · 2998 阅读 · 0 评论 -
pip自动生成和安装requirements.txt
生成requirements.txt文件pip freeze > requirements.txt安装requirements.txt依赖pip install -r requirements.txt原创 2018-05-16 18:05:30 · 80891 阅读 · 8 评论 -
python字典操作
from gensim.models import Word2VecC:\Users\dell\Anaconda3\lib\site-packages\gensim\utils.py:1197: UserWarning: detected Windows; aliasing chunkize to chunkize_serial warnings.warn("detected Window...原创 2018-05-15 13:33:22 · 1370 阅读 · 0 评论 -
python:类属性和实例属性、静态方法
Class and Instance Attributes类属性一个类的实例拥有各自的实例属性(Instance attributes),所以不同的实例通常所带的实例属性是不一样的也可以在类级别上定义属性:类属性被这个类拥有,该类所有的实例都共享这个属性,因此对所有实例而言这个值都是一样的。通常定义类属性在所有方法之外,通常放在最上方类头(class header)的右下。...原创 2018-05-08 15:44:09 · 440 阅读 · 0 评论