Python 将数据集的X值特征读入其自己的数组列,将y(目标)读入其数组,但在主数组中

Python 将数据集的X值特征读入其自己的数组列,将y(目标)读入其数组,但在主数组中,python,arrays,numpy,Python,Arrays,Numpy,我正在尝试读取我的csv文件,以获得以下数组: [[[1.1, 1.2],[+1.0]], [[2.3, 1.0],[+1.0]], [[2.9, 2.8],[-1.0]], [[5.2, 1.2],[+1.0]], [[5.4, 4.1],[+1.0]] ] [[[1.1, 1.2],[+1.0]], [[2.3, 1.0],[+1.0]], [[2.9, 2.8],[-1.0]], [[5.2, 1.2],[+1.0]], [[5.4, 4.1],[+1.0]] ] 在主

我正在尝试读取我的csv文件,以获得以下数组:

[[[1.1, 1.2],[+1.0]],
 [[2.3, 1.0],[+1.0]],
 [[2.9, 2.8],[-1.0]],
 [[5.2, 1.2],[+1.0]],
 [[5.4, 4.1],[+1.0]]
]
[[[1.1, 1.2],[+1.0]],
 [[2.3, 1.0],[+1.0]],
 [[2.9, 2.8],[-1.0]],
 [[5.2, 1.2],[+1.0]],
 [[5.4, 4.1],[+1.0]]
]

在主数组中,第一个数组部分[1.1,1.2]表示X列(x1,x2),第二个部分[+1.0]表示目标列(y值)。如何在python中实现这一点?谢谢

好的,找到了一个似乎有效的解决方案:

import csv
with open('filename.csv', 'r') as f:
    reader = csv.DictReader(f, delimiter=r',', fieldnames=(0,1, 'y')) #0 is column x1, 1 is column x2, and 'y' is the target column (y values)
    headers = next(f) #to remove the first header row names
z = [[[float(row[0]), float(row[1])], [float(row['y'])]] for row in reader] #create the array of arrays
z #display the array of arrays as needed.

输出