pythonnumpy,list操作pickle写操作
with open('../item_desc_smallnvjc.pkl','wb') as in_data_item_desc:
pickle.dump(item_desc,in_data_item_desc,pickle.HIGHEST_PROTOCOL)
pickle读操作
with open('jap_word_list.pkl','rb') as in_data_jap_word_list:
jap2vec = pickle.load(in_data_jap_word_list)
取平均操作
建⽴new matrix
找到列表中满⾜某些条件的元素
a = [0, 1, 2, 3, 4, 0, 2, 3, 6, 7, 5]
lected = [x for x in a if x in range(1, 5)] # 找到a中属于[1,5)中的元素
numpy.ndarray⼀系列骚操作
非限定性定语从句
reshape numpy.ndarray dimensions
train_words.shape : (128,30,10)
shape(train_words,(128*30*10,-1))
output dimension: (38400,1)
aa_squeeze = np.squeeze(aa)
numpy.ndarray index操作
bb=aa_squeeze[aa_squeeze>100000]
numpy 指定连续的iteration
iteration = np.arange(start_index, end_index, increasing_step)
iteration = np.arange(0,iterations,1)
随机打乱list序列,然后从其中取batchsize⼤⼩的index元素来feed进model
用发展的眼光看中国
start_list = list(range(0,train_data.size,args.batch_size))
np.random.shuffle(start_list)
其中第⼀⾏代码⽣成range object, which can not be called by len() or range.shape function
os.listdir(): 输出的是相对路径。
⽐如'/home/zhantiantian/images'中有'/home/zhantiantian/images/1'和'/home/zhantiantian/images/2'两个⽂件夹。这个函数的输出是'1' 和'2'。
但是glob.glob(os.path.join(path_to_dir, '*.jpg'))
输出的则是绝对路径。即⽂件夹中.jpg⽂件的绝对路径。
print(item_id) 和item_id直接输⼊到命令⾏⾥是不⼀样的结果。
⽐如item_id = '6176277’。前者的输出是6176277,⽽后者的输出是‘6176277‘。
python写⽂件处理代码时,特别当⽂件夹⽐较多时,处理的⽂件数量较多时,可以采⽤
bloomersprint('processing {}/{}\n'.format(ur_id, len(ur_dirs))实时更新处理的进度
# 参数意思分别 是从a 中以概率P,随机选择3个, p没有指定的时候相当于是⼀致的分布
thesisa1 = np.random.choice(a=5, size=3, replace=Fal, p=None)
print(a1)
# ⾮⼀致的分布,会以多少的概率提出来
a2 = np.random.choice(a=5, size=3, replace=Fal, p=[0.2, 0.1, 0.3, 0.4, 0.0])
print(a2)
# replacement 代表的意思是抽样之后还放不放回去,如果是Fal的话,那么出来的三个数都不⼀样,如果是True的话, 有可能会出现重复的,因为前⾯的抽的放回去了。
addle
python knowledge graph的⽅法加⼊元素tuple list.append((1,2))其中(1,2)是⼀个tuple。
奥瑞安西
def construct_kg(kg_np):
staffer
print('constructing knowledge graph ...')
kg = dict()
for triple in kg_np:
head = triple[0]
relation = triple[1]
tail = triple[2]
# treat the KG as an undirected graph
if head not in kg:
kg[head] = []
kg[head].append((tail, relation))
warmif tail not in kg:
kg[tail] = []
kg[tail].append((head, relation))
return kg
⼆维numpy.ndarray如何遍历矩阵⾥⾯的元素?
kg_np.shape: (715971, 3)
kg_np[0]: array([0, 0, 3])
python复制粘贴代码:
命令⾏输⼊参数选择
import argpar
np.random.ed(666)
parr = argpar.ArgumentParr()
parr.add_argument('--targetdir', type=str, default='../ACMMM2020', help='which target dir to specify') args = parr.par_args()
去除字符串中的'\'
item_name = place(u'\',u' ')
提取某字符串'()'中间的⽂字内容。ps:这其中括号可以为()英⽂括号,或者()中⽂括号。⽤字符匹配⽅式
中⽂括号:
import re
p1 = re.compile(r'[(](.*?)[)]', re.S)
注册会计师考试用书
print(re.findall(p1, string))
by no means
import re
p1 = re.compile(r'[(](.*?)[)]', re.S)
print(re.findall(p1, string))
匹配两个字符串中间的数值 ('max_acc'和'max_acc'之间的数值)
acc = re.findall(r"max_acc: (.+?)\n", result_line)
auc = re.findall(r"max_auc(.+?),max_acc", result_line)