Python-装饰器Decorator
在 Python 中,装饰器(decorator)是一种特殊的函数或类,用于在不修改原始代码的情况下,动态地修改或增强函数、方法或类的行为。 函数:独立的代码块,可以在任何地方定义和调用。 ...
在 Python 中,装饰器(decorator)是一种特殊的函数或类,用于在不修改原始代码的情况下,动态地修改或增强函数、方法或类的行为。 函数:独立的代码块,可以在任何地方定义和调用。 ...
tdigest python工具 是一种用于高效计算近似分位数的数据结构和算法。相较于传统的分位数计算方法,tdigest可以实现以较小的内存开销和较快的计算速度,提供近似但足够精确的分位数估计。 ...
lambda表达式可用于定义简单的一行式函数,并且可搭配其它函数时有多种衍生用法。 https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html https://www.learncodewithmike.com/2019/12/python-lambda-functions.html https://pythonviz.com/pandas/3-ways-to-use-pandas-apply-in-python/ 1、基础用法 如下所示,lambda表达式有3个组分 关键字 lambda 函数所需的参数,根据需要可以有多个 函数表达式,通常就是一行。 1 2 3 4 5 6 7 x = lambda a : a + 10 print(x(5)) x = lambda a, b : a * b print(x(5, 6)) (lambda a, b : a * b)(4, 3) 2、搭配用法 2.1 map()迭代 map()接受两个参数:逐元素进行某个函数的计算,返回相应的结果 ...
python内置的threading模块可以实现多任务的多线程并行处理,如下简单记录一下用法。 参考链接 https://zhuanlan.zhihu.com/p/34004179 https://www.runoob.com/python3/python3-multithreading.html 1、threading 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import threading import random import time def calculate(result): t1 = random.randint(2, 10) t2 = t1-1 print(threading.current_thread().name, ": waiting time is", t2) #查看当前线程名 time.sleep(t2) threadLock = threading.Lock() threadLock.acquire() #上锁 result.append(t2) #保证只有一个线程对result进行操作 threadLock.release() #解锁 ths = [] #线程列表 result = [] #储存所有线程的结果 print(time.ctime(time.time())) for i in range(5): #相当于调用5个线程 th = threading.Thread(target = calculate, args=(result,), name = 'thread {}'.format(i)) #args参数用于传参 th.start() ths.append(th) # 如下循环语句常用来等待所有线程结束后,再执行后面的命令; # 如果不加此语句,会直接继续后面的命令,但之后result也可以得到相同的结果 for th in ths: th.join() print(time.ctime(time.time())) print(result) # Mon Sep 19 10:28:23 2022 #开始时间 # thread 0 : waiting time is 2 # thread 1 : waiting time is 4 # thread 2 : waiting time is 7 # thread 3 : waiting time is 3 # thread 4 : waiting time is 8 # Mon Sep 19 10:28:31 2022 #结束时间,即运行时间为8s # [2, 3, 4, 7, 8] 上面的例子作用是:随机初始化一个数,等待相应的时间后,再加入到同一个列表中。 ...
Python有不少解释器,默认使用的是CPython。而IPython,interactive python可以提供交互式开发环境;是Jupyter的内核。如果要使用Jupyter,使用的就是IPython开发环境。 ...
NumPy,Numerical Python创建的ndarray数组在某些方面与Python中的list对象很相似;但是二者还是很多区别,比如数据类型一致性的要求、维度等。 ...
Pandas相比Numpy数组支持行列标签、多种数据类型,类似R语言中data.frame数据框。 1、Pandas结构 1.1 Series:有索引的一维数组 1.1.1 创建Series对象 通过pd.Serise函数创建 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ##(1) 通过index参数设置索引 import pandas as pd data = pd.Series([1,2,3,4,5], index=['a','b','c','d','e']) #a 1 #b 2 #c 3 #d 4 #e 5 #dtype: int64 ##(2)如不设置index参数,则索引默认为 range(len) data1 = pd.Series([1,2,3,4,5]) ##(3)也可以以字典的形式创建,键值分别对应索引与值 data2 = pd.Series({'a':1,'b':2,'c':3,'d':4,'e':5}) 1.1.2 Series对象操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import pandas as pd data = pd.Series([1,2,3,4,5], index=['a','b','c','d','e']) ##(1) 提取Series的一维数组与索引标签 data.values # array([1, 2, 3, 4, 5]) data.index # Index(['a', 'b', 'c', 'd', 'e'], dtype='object') ##(2) 按显示索引取Series子集 data['a'] data['a':'c'] data[["a","c","e"]] data[data>3] data.loc["a":"c"] ##(3) 按隐式索引取Series子集 data[0] data[0:3] data[[0,2,4]] data.iloc[1:3] .loc与.iloc的作用主要体现在:Series的index也是数值型时,容易与隐式索引混淆。 ...
1、启动方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import matplotlib.pyplot as plt %matplotlib inline import numpy as np ##(1)在普通python解释器/python脚本中 x = np.linspace(0,10,100) plt.plot(x, np.sin(x), '-') plt.show() ##(2)在ipython解释器中 %matplotlib #只需声明一次 x = np.linspace(0,10,100) plt.plot(x, np.sin(x), '-') ##(3)在jupyter中 %matplotlib inline #只需声明一次 x = np.linspace(0,10,100) plt.plot(x, np.sin(x), '-') 下述命令均在jupyter中学习 ...
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html pandas的dataframe自带一些绘图语法用以简单的表格数据可视化,适合于数据的初步探索、分析 1 import pandas as pd 1、barplot柱状图 1 2 3 4 5 6 7 8 df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]}) # lab val # 0 A 10 # 1 B 30 # 2 C 20 ax = df.plot.bar(x='lab', y='val', rot=0, color="green") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 speed = [0.1, 17.5, 40, 48] lifespan = [2, 8, 70, 1.5] index = ['snail', 'pig', 'elephant', 'rabbit'] # 类别名为index df = pd.DataFrame({'speed': speed, 'lifespan': lifespan}, index=index) # speed lifespan #snail 0.1 2.0 #pig 17.5 8.0 #elephant 40.0 70.0 #rabbit 48.0 1.5 ax = df.plot.bar(rot=0) #所有列(如下图) ax = df.plot.bar(rot=0,stacked=True) ax = df["speed"].plot.bar(rot=45) #指定列 ax = df.plot.bar( rot=0, subplots=True, layout=(1,2), color={"speed": "red", "lifespan": "green"} ) pandas.DataFrame.plot.barh()支持绘制横向的柱状图(Dataframe数据内容需要是横向的) ...
官方教程链接:https://seaborn.pydata.org/tutorial.html 1 2 3 4 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd 1. theme主题设置 示例展示效果见链接 context: 用于调整整体字体和标注的大小 ...