我正在尝试进行测试,并按“ train_test_split”划分训练数据。
为什么出现错误“至少需要一个数组作为输入”。

“ train_test_split”的输入可以是array和dataFrame,对吗?

import pandas as pd
import numpy as np
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
import pandas.rpy.common as rpy_common
from sklearn.model_selection import  train_test_split

def la():
 ro.r('library(MASS)')
 pydf = rpy_common.load_data(name = 'Boston', package=None, convert=True)
 pddf = pd.DataFrame(pydf)
 targetIndex = pddf.columns.get_loc("medv")

 # make train and test data
rowNum = pddf.shape[0]
colNum = pddf.shape[1]
print(type(pddf.as_matrix()))
print(pddf.as_matrix().shape)
m = np.asarray(pddf.as_matrix()).reshape(rowNum,colNum)
print(type(m))
x_train, x_test, y_train, y_test = train_test_split(x = m[:, 0:rowNum-2], \
                                                    y = m[:, -1],\
                                                    test_size = 0.5)
# error:     raise ValueError("At least one array required as input")
ValueError: At least one array required as input

最佳答案

sklearn docs中,使用位置项拆包(“ * args”)处理数组。

您正在使用关键字参数“ x =”和“ y =”,试图通过查看“ x”和“ y”是否是特殊关键字选项的名称来尝试处理这些参数。

尝试:

train_test_split(m[:, 0:rowNum-2], m[:, -1], test_size=0.5)


(从数组中删除关键字参数名称)。

10-02 10:57