【问题标题】:how to classify a large csv file of signals without headers in python?如何在python中对没有标题的大型csv信号文件进行分类?
【发布时间】:2018-02-25 02:24:24
【问题描述】:

我有一个没有标题的大型 csv 文件 (3000*20000) 的数据,我添加了一列来表示类。当特征没有标题并且由于大量列而无法手动添加时,我如何将数据拟合到模型中。 有没有办法自动迭代一行中的每一列?

当我有一个 4 列的小文件时,我使用了以下代码:

import pandas as pd
pd = pd.ExcelFile("bcs.xlsx")
col = [0, 1, 2, 3]
data = pd.parse(pd.sheet_names[0], parse_cols = col)

pdc = list(data["pdc"])
pds = list(data["pds"])
pdsh = list(data["pdsh"])
pd_class = list(data["class"])

features = []
for i in range(len(pdc)):
    features.append([pdc[i],pds[i],pdsh[i]])

labels = []
labels = pd_class

但是对于 3000 x 20000 的文件,我不知道如何识别特征和标签/目标

【问题讨论】:

    标签: python-3.x pandas machine-learning classification


    【解决方案1】:

    假设你有一个这样的 csv:

    1,2,3,4,0
    1,2,3,4,1
    1,2,3,4,1
    1,2,3,4,0
    

    其中前 4 列是特征,最后一列是您想要的标签或类别。您可以使用pandas.read_csv 阅读该文件,并为您的功能创建一个数据框,并为您的标签创建一个数据框,然后您可以将其安装到您的模型中。

    import pandas as pd
    
    #CSV localPath
    mypath ='C:\\...'
    
    #The names of the columns you want to have in your dataframe
    colNames = ['Feature1','Feature2','Feature3','Feature4','class']
    
    #Read the data as dataframe
    df = pd.read_csv(filepath_or_buffer = mypath, 
                     names = colNames , sep  = ',' , header = None)
    
    #Get the first four columns as features
    features = df.ix[:,:4]
    #and last columns as label
    labels = df['class']
    

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2015-10-21
      • 1970-01-01
      • 2019-09-18
      • 2018-08-01
      相关资源
      最近更新 更多