Python库的多样性使得它们可以在不同的应用场景中发挥作用,不同场景下同一个库可能拥有不同的功能,本系列未来会有不同的分类,可能会出现相同的库。数量很多,慢慢整理。文中的示例都是最基础的用法,有些就顺手写了,有些就丢给Ai,看个乐就行。
功能: Optuna 是一个自动超参数优化框架,支持各种优化算法。
使用场景:
- 机器学习模型的超参数调优。
- 优化深度学习网络结构。
- 实验设计和策略优化。
示例:
| import optuna |
| |
| def objective(trial): |
| x = trial.suggest_float('x', -10, 10) |
| return (x - 2) ** 2 |
| |
| |
| study = optuna.create_study(direction='minimize') |
| study.optimize(objective, n_trials=100) |
| |
| |
| print(f"最优值: {study.best_value}") |
| print(f"最优参数: {study.best_params}") |
功能: PyFVCOM 是 FVCOM (Finite Volume Coastal Ocean Model) 的 Python 接口,用于海洋模拟。
使用场景:
- 海洋流动和水质模拟。
- 海岸工程和环境研究。
- 海洋物理过程的模拟。
示例:
| from pyfvcom import read_file |
| |
| |
| data = read_file('fvcom_output.nc') |
| |
| |
| print(f"Time steps: {data.time.shape[0]}") |
| print(f"Number of nodes: {data.node.shape[0]}") |
| print(f"Number of elements: {data.elem.shape[0]}") |
| |
| |
| print(f"Temperature at first time step: {data.temperature[:, 0]}") |
功能: pythermalcomfort 是一个用于计算热舒适指标的库。
使用场景:
- 建筑环境中的热舒适分析。
- 人体热舒适度评估。
- 环境控制系统设计。
示例:
| from pythermalcomfort.models import pmv_ppd |
| |
| |
| pmv, ppd = pmv_ppd( |
| ta=25, |
| tr=25, |
| vel=0.1, |
| rh=50, |
| met=1.2, |
| clo=0.5 |
| ) |
| |
| print(f"PMV: {pmv}, PPD: {ppd}") |
功能: SpiceyPy 是 NASA SPICE 工具集的 Python 接口,用于天体力学和空间任务计算。
使用场景:
- 轨道计算和天体位置确定。
- 空间任务规划和分析。
- 天文数据处理和可视化。
示例:
| from spiceypy import spice |
| |
| |
| spice.furnsh('de430.bsp') |
| spice.furnsh('naif0012.tls') |
| |
| |
| et = spice.str2et('2023-10-10') |
| earth_pos, lt = spice.spkpos('Earth', et, 'J2000', 'NONE', 'SUN') |
| moon_pos, lt = spice.spkpos('Moon', et, 'J2000', 'NONE', 'SUN') |
| |
| print(f"Earth position: {earth_pos}") |
| print(f"Moon position: {moon_pos}") |
功能: Mordred 是一个用于生成分子描述符的库,适用于化学信息学。
使用场景:
- 分子性质预测和分析。
- 药物设计和虚拟筛选。
- 化学信息学研究。
示例:
| from mordred import Calculator, descriptors |
| |
| |
| calc = Calculator(descriptors, ignore_3D=True) |
| |
| |
| |
| |
| |
| result = calc(mol) |
| |
| |
| for desc in result: |
| print(f"{desc.__class__.__name__}: {desc.value}") |
功能: pyswmm 是 SWMM (Storm Water Management Model) 的 Python 接口,用于城市排水系统模拟。
使用场景:
- 城市排水系统设计和分析。
- 降雨径流模拟。
- 污水处理系统优化。
示例:
| from pyswmm import Simulation |
| |
| |
| with Simulation('example.inp') as sim: |
| |
| for step in sim: |
| |
| pass |
| |
| |
| print(f"Total rainfall: {sim['Total Rainfall']} mm") |
功能: SDSS (Sloan Digital Sky Survey) 提供了 Python 工具来分析其星系数据库。
使用场景:
示例:
| from sdss import SDSS |
| |
| |
| sdss = SDSS() |
| |
| |
| query = "SELECT objid, ra, dec, u, g, r, i, z FROM PhotoObj WHERE type = 3" |
| results = sdss.query(query) |
| |
| |
| for row in results[:5]: |
| print(row) |
功能: MNE-Python 是用于分析脑电图(EEG)和磁脑图(MEG)数据的工具包。
使用场景:
- 脑电信号处理和分析。
- 神经科学研究。
- 脑机接口开发。
示例:
| import mne |
| |
| |
| raw = mne.io.read_raw_fif('sample_audvis_filt-0-40_raw.fif') |
| |
| |
| raw.filter(1., 40., fir_design='firwin') |
| |
| |
| raw.plot() |
| |
| |
| events = mne.find_events(raw) |
| |
| |
| event_id = {'auditory/left': 1, 'auditory/right': 2} |
| |
| |
| epochs = mne.Epochs(raw, events, event_id, tmin=-0.2, tmax=0.5, preload=True) |
| |
| |
| epochs['auditory'].average().plot() |
功能: pyphs 是一个用于物理系统建模和符号计算的 Python 库。
使用场景:
- 物理系统的动态建模。
- 符号计算和系统分析。
- 控制系统设计。
示例:
| from pyphs import PortHamiltonianSystem |
| |
| |
| system = PortHamiltonianSystem( |
| H = "0.5 * x**2 + 0.5 * y**2", |
| z = ["x", "y"], |
| u = ["u"], |
| y = ["y"] |
| ) |
| |
| |
| system.generate() |
| |
| |
| print(system.M) |
| print(system.J) |
| print(system.R) |
功能: GridPy 是一个用于电力系统模拟和分析的 Python 库。
使用场景:
- 电力系统的动态模拟。
- 电力市场分析。
- 电网规划和优化。
示例:
| from gridpy import Grid |
| |
| |
| grid = Grid() |
| |
| |
| grid.add_bus('Bus1') |
| grid.add_bus('Bus2') |
| grid.add_line('Bus1', 'Bus2', r=0.01, x=0.1) |
| |
| |
| grid.add_load('Bus2', p=100, q=50) |
| |
| |
| grid.run_power_flow() |
| |
| |
| print(f"Voltage at Bus1: {grid.get_bus_voltage('Bus1')}") |
| print(f"Voltage at Bus2: {grid.get_bus_voltage('Bus2')}") |