def to_csv(self, filepath='hypothesis/SGD_hypothesis_header.csv'):
df = pd.DataFrame()
df = pd.concat([df, pd.DataFrame([['depth', self.depth]])], ignore_index=True)
df = pd.concat([df, pd.DataFrame([['sizes'] + [self.input_size+1] \
+ [hidden_size+1 for hidden_size in self.hidden_sizes] \
+ [self.output_size]])], ignore_index=True)
for i, weight in enumerate(self.best_weights):
df = pd.concat([df, pd.DataFrame([['W_{}'.format(i)] + weight.T.flatten().tolist()])], ignore_index=True)
# Fill nan with None[]
df = df.where((pd.notnull(df)), None)
# Since pd.to_csv converts int to float if there's `None` in the same row,
# we need to handle this.
with open(filepath, 'w') as f:
for row in range(df.shape[0]):
for col in range(df.shape[1]):
if (row == 0 and col != 0) or (row == 1 and col != 0):
val = int(df[col][row]) if df[col][row] is not None else ''
else:
val = df[col][row] if df[col][row] is not None else ''
f.writelines('{},'.format(val))
if row != df.shape[0]-1: f.writelines('\n')
评论列表
文章目录