python

numpy 1.17.4

增减数组的维度

array = np.expand_dims(array,axis=0)
array = array[:, None, :]
array = np.squeeze(array)

数组指定维度相连

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a, b), axis=0)

matplotlib 3.1.2

绘制折线图

def plot_pic(w_A, w_B, w_C):
    w_A_mean = np.mean(w_A, axis=1)
    w_B_mean = np.mean(w_B, axis=1)
    w_C_mean = np.mean(w_C, axis=1)

    n = w_A_mean.shape[0]
    x = range(0, n)

    # size
    plt.figure(figsize=(20, 8), dpi=80)

    # color
    plt.plot(x, w_A_mean, label="w_A_mean", color="#F08080")
    plt.plot(x, w_B_mean, label="w_B_mean", color="#0000FF", linestyle="--")
    plt.plot(x, w_C_mean, label="w_C_mean", color="#102020", linestyle="-.")

    # x axis
    _xtick_labels = range(0, 25)
    plt.xticks(x, _xtick_labels)
    # plt.yticks(range(0,9))

    # gird
    plt.grid(alpha=0.4, linestyle=':')

    # legend
    plt.legend(loc="upper left")

    # show
    plt.show()

保存图片到本地

plt.savefig('testblueline.png')

os

创建目录

if os.path.exists(FilePath):   ##目录存在,返回为真
	print( 'dir exists'  ) 
else:
	print( 'dir not exists')
	# os.mkdir(FilePath)
	os.makedirs(FilePath) 

调用GPU计算

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

glob

删除指定目录下的所有文件

import os
import glob
def delfile(path):
    fileNames = glob.glob(path + r'\*')
    for fileName in fileNames:
        try:
            os.remove( fileName)
        except:
            try:
                os.rmdir( fileName)
            except:
                delfile( fileName)
                os.rmdir( fileName)
                
delfile(path=r'./results/LaLonde_Jobs')

sys

导入包路径

import sys
sys.path.append('../../../')

string

替换特定字符

str_ = 'Bob beat Alan. Alan beat Bob.'
str1 = 'beat'
x = len(str1)
str2 = 'paly with'
for i in str_:
    if str_.find(str1) != -1:
        print(str_.find(str1))
        str_ = str_.replace(str_[str_.find(str1):str_.find(str1) + x], str2)
print(str_)

防止字符串中反斜杠的转义

s = r'test\t'

tqdm

显示进度条

from tqdm import tqdm
import time

for i in tqdm(range(10)):
    pass
    time.sleep(0.2)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值