当前位置:   article > 正文

python常用代码汇总_python代码大全

python代码大全

pytorch常用组建——知乎

基本模块

### 常规设置
import os
import math
import time
import glob 
import warnings
import itertools
import numpy as np
import pandas as pd
from PIL import Image
import multiprocessing
from collections import Counter
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 500 #分辨率
plt.rcParams['savefig.dpi'] = 500 #图片像素
pl.utilities.seed.seed_everything(seed=42)
plt.rcParams['figure.figsize'] = (3.5, 2.5)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

#jupyter 配置信息
%matplotlib inline
warnings.filterwarnings("ignore")
%config InlineBackend.figure_format = 'svg'



### 医学图形数据处理设置
import pydicom # 用于读取dcm文件
import scipy.misc 
from skimage.io import imread # 医学影像常用库
from skimage.transform import resize
from mpl_toolkits.axes_grid import ImageGrid# 网格图形展示  


### pytorch 设置
import cv2
import torch
import functools 
import torchvision
import torch.nn as nn 
from torchvision import models
import pytorch_lightning as pl
import imgaug.augmenters as iaa
import torch.nn.functional as F 
from torch.utils.data import DataLoader,Dataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler,MinMaxScaler
  • 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
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

修改照片尺寸

import cv2
from PIL import Image as ImagePIL
from PIL import Image
im = cv2.imread('123.jpg')
cv2.imwrite('compress123.jpg', im, [cv2.IMWRITE_JPEG_QUALITY, 30])
  • 1
  • 2
  • 3
  • 4
  • 5

字符串纯化


def clean_words(input_words):
    punctuation = '.,;:"!?_-'
    word_list = input_words.lower().replace('\n',' ').split()
    word_list = [word.strip(punctuation) for word in word_list]
    return word_list

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

释放没用的显存

torch.cuda.empty_cache()
  • 1

对import进行排序

myList = ["import os","import warnings","import pandas as pd"]
myList1 = sorted(myList,key = lambda i:len(i),reverse=False)  
for i in myList1:
    print(i)
    
# mycolors = stata_pal("s2color")(15)
mycolors = ["#1a476f","#90353b","#55752f","#e37e00","#6e8e84",
             "#c10534","#938dd2","#cac27e","#a0522d","#7b92a8",
             "#2d6d66","#9c8847","#bfa19c","#ffd200","#d9e6eb"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

BH矫正

def p_adjust_bh(p):
    p = np.asfarray(p)
    by_descend = p.argsort()[::-1]
    by_orig = by_descend.argsort()
    steps = float(len(p)) / np.arange(len(p), 0, -1)
    q = np.minimum(1, np.minimum.accumulate(steps * p[by_descend]))
    return q[by_orig]
result['FDR']=p_adjust_bh(result.P)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

argparse & 配置文件

这种方法是使用–args进行定义

import argparse
parser = argparse.ArgumentParser()
# 导入参数
# 可以不设置 default
# action='store_true' # 默认为 true
parser.add_argument('--batch_size', type=int, 
default=8,
help='size of each image batch')
parser.add_argument('--learning_rate', type=float, 
default=0.001, 
help='learning rate')
parser.add_argument('--cuda_device', type=str, 
default="2,3", 
help='whether to use cuda if available')
parser.add_argument('-d', '--drop', 
action='store_true',
help='Decision to drop input genes from X matrix')
opt = parser.parse_args()
# 调用
opt.batch_size
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

直接使用配置文件,这样修改好配置文件可以直接使用

配置文件的制作方法

import yaml
# 读取配置文件的方式
config_path = 'configs/config_template.yaml'
config_dict = yaml.safe_load(open(config_path, 'r'))
  • 1
  • 2
  • 3
  • 4

计算程序运行时间

# 引入一个time模块, * 表示time模块的所有功能,
# 作用: 可以统计程序运行的时间
from time import *
begin_time = time()
# 图片读取
rgb = Image.open(self.rgb[idx])
end_time = time()
run_time = end_time-begin_time
print ('该循环程序运行时间:',run_time) #该循环程序运行时间: 1.4201874732
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

H5

### h5 存储
df = pd.read_csv('scale_df.csv',index_col=0)
h5 = pd.HDFStore('scale_df.h5','w',complevel=4, complib='blosc')
h5['data'] = df
h5.close()
df = pd.read_hdf('scale_df.h5',key='data')

import h5py
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/203558
推荐阅读
相关标签
  

闽ICP备14008679号