【问题标题】:Separate last column from the actual dataset using numpy使用 numpy 将最后一列与实际数据集分开
【发布时间】:2017-11-28 23:26:59
【问题描述】:

我有一个csv 格式的数据集(没有标题),我想将它分成两部分:(1)没有最后一列的实际数据集,(2)最后一列(类标签)。我的数据集有 100K 行和 65 个特征(最后一列,第 65 列是我要分离的类标签)。我写了以下内容:

dataset_path = 'dataset.csv'

dataset = np.genfromtxt(dataset_path, delimiter=',')
class_label = dataset[:-1]
dataset.drop(class_label, axis=1, inplace=True)

print dataset.shape
print class_label

这实际上是错误的。我无法实现我想要的。任何帮助表示赞赏。

【问题讨论】:

  • 怎么了?你收到错误信息吗?显示出来。

标签: python python-2.7 numpy


【解决方案1】:

假设您的数据集没有标题

class_label = dataset[:, -1] # for last column
dataset = dataset[:, :-1] # for all but last column

【讨论】:

    【解决方案2】:

    如果您对使用 numpy 数组感兴趣,可以将 csv 文件中的数据读入 numpy 数组:

     from numpy import genfromtxt
     my_data = genfromtxt('E:\Book1.csv', delimiter=',', dtype = 'str',  skip_header=1, unpack=True)
    

    my_data 中的每个项目将是 csv 文件中每一列的列表。 现在您可以通过以下方式删除最后一列:

     my_data_without_last_column = my_data[:-1].copy()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-12
      • 2016-03-18
      • 2018-09-29
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多