data_io.py 文件源码

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

项目:Kaggler 作者: qqgeogor 项目源码 文件源码
def load_data(path, dense=False):
    """Load data from a CSV or libsvm format file.

    Args:
        path (str): A path to the CSV or libsvm format file containing data.
        dense (boolean): An optional variable indicating if the return matrix
                         should be dense.  By default, it is false.
    """

    with open(path, 'r') as f:
        line = f.readline().strip()

    if ':' in line:
        X, y = load_svmlight_file(path)
        X = X.astype(np.float32)
        if dense:
            X = X.todense()
    elif ',' in line:
        X = np.loadtxt(path, delimiter=',',
                       skiprows=0 if is_number(line.split(',')[0]) else 1)
        y = X[:, 0]
        X = X[:, 1:]
    else:
        raise NotImplementedError, "Neither CSV nor LibSVM formatted file."

    return X, y
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号