Python重要模块(待整理)
一、基础模块
- sys 模块
函数/变量 | 描述 |
---|
sys.exit([arg]) | 退出当前程序 |
sys.modules | 已导入的模块和模块名(返回一个字典) |
sys.path | 模块的目录的名称(返回一个列表) |
sys.platform | 平台标识符(如win32) |
- os模块
函数/变量 | 描述 |
---|
os.environ | 包含环境变量的映射 |
os.sep | 路径中使用的分隔符(如windows是\\) |
os.pathsep | 分隔不同路径的分隔符 |
os.linesep | 行分隔符 |
os.system(command) | 在子shell中执行操作系统命令 |
- fileinput 模块
函数/变量 | 描述 |
---|
fileinput.input([file[,inplace[,backup]]]) | 迭代多个输入流中的行 |
fileinput.filename() | 返回当前文件名称 |
fileinput.lineno() | 返回累计的当前行号 |
fileinput.filelineno() | 返回当前文件中的行号 |
fileinput.isfirstline() | 检查当前行是否是文件第一行 |
fileinput.nextfile() | 关闭当前文件并移到下一个 |
fileinput.close() | 关闭 |
- heapq(堆) 模块
函数/变量 | 描述 |
---|
heapq.heappush(heap,x) | 将x压入堆中 |
heapq.heappop(heap) | 从堆中弹出最下的元素 |
heapq.heapify(heap) | 让列表具有堆的特征 |
heapq.heapreplace(heap,x) | 弹出最小的元素,并将x压入堆中 |
heapq.nlargest(n,iter) | 返回iter中n个最大的元素 |
heapq.nsmallest(n,iter) | 返回iter中n个最小的元素 |
- time 模块
日期元组中的字段
索引 | 字段 | 值 |
---|
0 | 年 | 如2000,2001 |
1 | 月 | 1-12 |
2 | 日 | 1-31 |
3 | 时 | 0-23 |
4 | 分 | 0-59 |
5 | 秒 | 0-61 |
6 | 星期 | 0-6,0是星期一 |
7 | 儒略日 | 1-366 |
8 | 夏令时 | 0,1,-1 |
函数
函数/变量 | 描述 |
---|
time.asctime([tuple]) | 将时间元组转换为字符串 |
time.localtime([secs]) | 将秒数转换为表示当地时间的日期元组 |
time.mktime(tuple) | 将时间元组转换为当地时间 |
time.sleep(secs) | 休眠secs秒 |
time.strptime{string[,format]) | 将字符串转换为时间元组 |
time.time() | 当前时间 |
6. random 模块
函数/变量 | 描述 |
---|
random.random() | 返回一个0-1的随机实数 |
random.getrandbits(b) | 以长整数方式返回n个随机的二进制位 |
random.uniform(a,b) | 返回一个a-b的随机实数 |
random.randrange([start],stop,[step]) | |
random.choice(seq) | |
random.shuffle(seq[,random]) | |
random.sample(seq,n) | |
7. re 模块(正则表达式)
函数/方法 | 描述 |
---|
re.compile(pattern[,flags]) | 将用字符串表达的正则表达式转换为模式对象 |
re.search(pattern.string[,flags]) | 在字符串中查找对应的模式 |
re.match(pattern.string[,flags]) | 在字符串开头匹配模式 |
re.split(pattern,string[maxsplit=0]) | 根据模式分割字符串 |
re.findall(pattern,string) | 返回一个列表,包含字符串中所有与模式匹配的字符串 |
re.sub(pat,repl,string[,count=0]) | 将字符串中与模式匹配的字符串换成repl |
re.escape(string) | 转义字符串在所有正则表达式的特殊字符 |
object1.group([group1,…]) | 通常先用match方法进行,返回与模式中给定编组匹配的子串,没有指定在默认为0 |
object1.start([group]) | 返回与给定编组匹配的字符串的起始索引 |
object1.end([group]) | 返回与给定编组匹配的字符串的结束索引+1 |
object1.span([group]) | 返回与给定编组匹配的字符串的起始索引和结束索引+1 |
8. operator 模块(运算符)
9. math 模块
二、进阶模块
- NumPy模块(处理存储大型矩阵数组)
创建数组 (简化numpy名为np)
import numpy as np
arr1=np.array([1,2,3])
arr2=np.array([(1,2,3),(2,3)])
arr3=np.zeros((m,n))
arr4=np.identity(n)
arr5=np.random.rand(m,n)
arr6=np.arange(起点,终点,步长值)
arr7=np.linspace(起点,终点,n)
访问数组,矩阵
方法 | 描述 |
---|
arr1.shape | 返回矩阵的规格 |
arr1.ndim | 返回矩阵的秩 |
arr1.size | 返回矩阵元素总数 |
arr1.dtype.name | 返回矩阵元素的数据类型 |
type(arr1) | 返回整个数组对象的类型 |
索引切片
arr1[m,n]
arr1[m:n,x:y]
arr1[:,y]
arr1[-1]
运算与其他函数
运算 | 描述 |
---|
+,-,*,** | 返回加法,减法,普通乘法和幂运算(如arry1-arry2) |
np.dot(arr1,arr2) | 矩阵乘法 |
arr1.T | 转置 |
arr1.I | 求逆矩阵 |
np.linalg.inv(arr1) | 返回逆矩阵(结果同上) |
arr1.sum() | 数组元素求和 |
arr1.max() | 返回数组元素最大值 |
arr1.cumsum(axis=1) | 按行累计总和 |
arr1.shape或arr1,shape(0)等 | 返回一个元组显示该数组的大小 |
np.tile(A,B) | 将A数组重复B次(B可以是一个元组,表示行/列重复次数 |
arr1.argsort() | 将数组从小到大排列后,返回其索引 |
通用函数、合并与分割
函数 | 描述 |
---|
np.exp(arr1) | 指数函数 |
np.sin(arr1) | 正弦函数 |
np.sqrt(arr1) | 开方 |
np.add(arr1) | 加法(等同于+) |
np.vstack((arr1,arr2)) | 纵向合并数组 |
np,hstack((arr1,arr2)) | 横向合并数组 |
np.hsplit(arr1,n) | 横向分为n部分 |
np.vsplit(arr1,n) | 纵向分为n部分 |
其他常用方法
函数 | 描述 |
---|
np.empty((m,n)) | 返回一个给定规模的数组 |
np.all(array) | 测试数组元素是否均为True |
np.any(array) | 测试数组元素是否至少有一个为True |
np.average(array,axis,weights) | 计算加权平均值 |
np.nonzero | 返回数组非0元素的位置 |
np.sort | 对数组元素进行排序 |
np.var | 计算方差 |
np.where | 返回数组满足条件的元素 |
np.reshape | 转换数组的规模 |
np.eye | 生产单位矩阵 |
np.transpose | 转置,等同于.T |
np.std | 计算标准差 |
np.cov | 计算协方差矩阵 |
其他:numpy库的其他介绍.
2. Pandas模块(分析复杂的数据)
创建数据框(简称pandas为pd)
import pandas as pd
df=pd.DataFrame(数据集名,columns=['a','b'],index=list2)
df['a']
创建系列与向量化操作
df=pd.Series(数据集名,name)
df['a']或df[:3]
import numpy as np
print(np.sqrt(df))
df['d']=df['a']>1
常用方法
方法 | 描述 |
---|
pd.read_csv() | 将.csv文件中的数据读入内存,快速构建数据框 |
pd.concat() | 按横向或纵向合并两个Pandas数据结构 |
pd.get_dummies | 将类别变量转变为独热编码 |
Series.isnull | 判断系列中所有值是否含有空值 |
Series.is_unique | 判断系列中所有值是否重复 |
Series.value_counts() | 统计系列中所有取值的出现次数 |
DataFrame.mean() | 按行或列分别计算平均值 |
DataFrame.dropna() | 删除所有缺失数据的行或列 |
DataFrame.drop_duplicates() | 删除所有重复的行 |
DataFrame.head() | 默认返回数据框中的前五行 |
DataFrame.tail() | 默认返回数据框中的后五行 |
3. scikit-learn模块(机器学习)
4. Matplotlib模块(绘图)
import matplotlib.pyplot as plt
plt.scatter(x, y,s=shapesize,c=color,marker="散点的形状")
plt.title("标题")
plt.xlabel("行标题")
plt.ylabel("列标题")
plt.savefig("路径")
plt.show()