regression_nn.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:NTHU-Machine-Learning 作者: YuChunLOL 项目源码 文件源码
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')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号