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)
plt.figure(figsize=(20, 8), dpi=80)
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="-.")
_xtick_labels = range(0, 25)
plt.xticks(x, _xtick_labels)
plt.grid(alpha=0.4, linestyle=':')
plt.legend(loc="upper left")
plt.show()
保存图片到本地
plt.savefig('testblueline.png')
os
创建目录
if os.path.exists(FilePath):
print( 'dir exists' )
else:
print( 'dir not exists')
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)