Dataframe初始化
将数据存储为Dataframe格式并输出到文件中
df = pd.DataFrame(columns=['userId', 'vector'])
df = df.append({'userId':u_id,'vector':u_vector},ignore_index=True)
outputpath = 'result/user_{}.csv'.format(epoch)
df.to_csv(outputpath, sep=',', index=False, header=False)
写文件
- r:读模式(默认)
- x:写模式,打开时会清空文件
- a:添加模式,写文件只能写到文件末尾,不能读
- b:二进制模式
- t:文本模式(默认)
- +: 打开一个文件进行更新(可读可写)
- U:通用换行模式(不推荐)
- w:可读写,打开时清空文件
- r+:可读写,打开时不清空文件,可写到文件任何位置。默认在文件开始,因此会覆写文件
- a+:可读写,打开时不清空文件,只能写到文件末尾
#文件读
with open('a.csv') as f:
for line in f:
print(line)
#文件写
with open('result/user_{}.csv'.format(epoch), 'a', encoding='utf-8', newline='') as f:
writer = csv.writer(f, dialect='excel')
for i in user_output:
writer.writerow(i)
截取数组
array = array[start: end]
求数组平均值
np.mean(array)
Linux服务器后台执行Python文件
cd /root/srj/anaconda3/envs
source activate tensorflow
cd /root/srj/DMF
nohup python -u main.py > main.log 2>&1 &