16 4 2 2 1 1 python articles

Python读取配置文件

推荐使用configobj,无需配置文件有标题: 1 pip install configobj 使用方法如下: 1 2 3 4 5 from configobj import ConfigObj arg = ConfigObj('config.ini') val = arg['key']……

Continue reading

sklearn常用代码

训练集测试集拆分: 1 2 3 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1000) 分类任务报告: 1 2 3 from sklearn.metrics import classification_report print(classification_report(y_true, y_pred, target_names=target_names))……

Continue reading

H5PY常用代码

写入数据: 1 2 3 4 import h5py f = h5py.File('data.h5', 'w') f.create_dataset('a', shape=(255, 1024, 768), dtype=np.uint8, chunks=(1, 1024, 768), data=a, compression='gzip', compression_opts=9) 读取数据 1 2 3 4 import h5py f = h5py.File('data.h5', 'r') a = f['a']……

Continue reading

conda配置SadTalker+wav2lip环境

安装: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 conda create -n sadtalker python=3.8 -y conda activate sadtalker conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 paddlepaddle-gpu==2.4.2 cudatoolkit=11.6 -c paddle -c pytorch -c conda-forge -y conda env config vars set LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/ubuntu/anaconda3/envs/sadtalker/include:/home/ubuntu/anaconda3/envs/sadtalker/lib conda update ffmpeg -y pip install dlib-bin pip install -r SadTalker/requirements.txt pip install gfpgan==1.3.8 gradio==3.24.1 socksio==1.0.0 pip install paddlehub==2.3.1 typeguard==2.13.3 #pip install jupyterlab jupyter-archives hub install first_order_motion==1.0.0 hub install fastspeech2_baker==1.0.0 hub install wav2lip EOF 删除: 1 2 3 4 5 conda deactivate conda env remove -n sadtalker conda env list EOF……

Continue reading

opencv常用代码

读写包含中文文件名的图片 1 2 3 4 5 6 7 8 9 10 import cv2 import glob import numpy as np def read_image(path): return cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_UNCHANGED) def save_image(path, img): cv2.imencode(".jpg", img)[1].tofile(path) 图片旋转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import cv2 def rotateAndScale(img, scaleFactor=1.0, degreesCCW=30, borderValue=(255,255,255)): oldY, oldX = img.shape[:2] M = cv2.getRotationMatrix2D(center=(oldX / 2, oldY / 2), angle=degreesCCW, scale=scaleFactor) newX, newY = oldX * scaleFactor, oldY * scaleFactor r = np.deg2rad(degreesCCW) newX, newY = (abs(np.sin(r) * newY) + abs(np.cos(r) * newX), abs(np.sin(r) * newX) + abs(np.cos(r) * newY)) (tx, ty) = ((newX - oldX) / 2, (newY - oldY) / 2) M[0, 2] += tx M[1, 2] += ty return cv2.warpAffine(img, M, dsize=(int(newX), int(newY)), borderValue=borderValue)……

Continue reading

Pickle常用代码

写入数据: 1 2 3 4 5 import pickle, gzip def save_zipped_pickle(obj, fname, protocol=-1): with gzip.open(fname, 'wb') as f: pickle.dump(obj, f, protocol) 读取数据 1 2 3 4 5 6 import pickle, gzip def load_zipped_pickle(fname): with gzip.open(fname, 'rb') as f: loaded_object = pickle.load(f) return loaded_object……

Continue reading

Pandas常用代码

最佳迭代方法: 1 2 3 4 5 import pandas as pd from tqdm import tqdm for row in tqdm(df.to_dict(orient="records")): # do something 获取行数和列数 1 2 3 4 5 6 7 import pandas as pd rows = len(df.axes[0]) cols = len(df.axes[1]) rows = df.shape[0] cols = df.shape[1] 分块读取超大文件 1 2 3 4 5 6 7 8 import pandas as pd from tqdm import tqdm data = pd.read_csv('dataset.csv', chunksize=1000) for chunk in data: for row in tqdm(chunk.to_dict(orient="records")): # do something 根据已有单个列扩充新列 1 2 3 4 5 6 import pandas as pd def valFunc(val): return val+1 df['D'] = df['C'].apply(valFunc) 自定义函数筛选 1 2 3 4 5 6 import pandas as……

Continue reading

XGBoost常用代码

显示特征重要性 1 2 3 4 5 6 import pandas as pd f_importance = xbg_reg.get_booster().get_score(importance_type='gain') importance_df = pd.DataFrame.from_dict(data=f_importance, orient='index') importance_df.plot.bar() 辅助数据文件:组输入 适用于排序任务中的pairwise和listwise;组文件自动加载,若数据文件为train.txt,则组文件为train.txt.group;组文件为单列数字,按顺序指示数据文件中每个组的实例数。 辅助数据文件……

Continue reading

TQDM常用代码

与Pandas结合: 1 2 3 4 from tqdm.autonotebook import tqdm for row in tqdm(df.itertuples(), total=df.shape[0], ncols=128): # do something……

Continue reading