python arff稀疏格式文件传pandas DataFrame

#ecoding=utf-8
import numpy as np
from scipy.sparse import csr_matrix
import pandas as pd
from xml.dom.minidom import parse

def read_sparse_arff(f_path , xml_path):
    #读文件
    f = open(f_path)
    f_data = f.read().split('@data')
    f.close()
    #解析数据列名
    column = [i.split(' ')[1] for i in  f_data[0].split('@attribute')[1:]]
    #解析数据
    row , col , data = [] , [] , []
    for l in enumerate(f_data[1].replace(' ' , ':').split('\n')[1:-1]):
        print(l)
        l_v_dict = eval(l[1])
        col.extend(l_v_dict.keys())
        data.extend(l_v_dict.values())
        row.extend([l[0] for i in range(len(l_v_dict))])
    arff_data = csr_matrix((data , (row , col)) , shape=(row[-1] + 1 , len(column)))
    arff_data = pd.DataFrame(arff_data.toarray() , columns=column)
    #解析label列名
    DOMTree = parse(xml_path)
    collection = DOMTree._get_documentElement()
    labels = collection.getElementsByTagName('label')
    labels = [l.getAttribute('name') for l in labels]
    X_arff , Y_arff = arff_data[[f for f in arff_data.columns if f not in labels]] , arff_data[labels].astype(int)
    return X_arff , Y_arff
def read_arff(f_path , xml_path):
    #读文件
    f = open(f_path)
    f_data = f.read().split('@data')
    f.close()
    #解析列名
    column = [i.split(' ')[1] for i in  f_data[0].split('@attribute')[1:]]
    #解析数据
    data = [i.split(',') for i in f_data[1].split('\n')[1:-1]]
    data = np.array(data).astype(float)
    #转化为dataframe
    arff_data = pd.DataFrame(data , columns=column)
    #解析label列名
    DOMTree = parse(xml_path)
    collection = DOMTree._get_documentElement()
    labels = collection.getElementsByTagName('label')
    labels = [l.getAttribute('name') for l in labels]
    X_arff , Y_arff = arff_data[[f for f in arff_data.columns if f not in labels]] , arff_data[labels].astype(int)
    return X_arff , Y_arff

 

你可能感兴趣的:(python,数据分析)