我正在尝试按"train_test_split"拆分测试和训练数据。为什么我收到错误"至少需要一个数组作为输入"。
"train_test_split"的输入可以是数组和数据帧,对吧?
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 文档中,数组通过位置项目解包("*args"(进行处理。
您正在使用关键字参数"x="和"y=",它试图通过查看"x"和"y"是否是特殊关键字选项的名称来处理它们。
尝试:
train_test_split(m[:, 0:rowNum-2], m[:, -1], test_size=0.5)
(从数组中删除关键字参数名称(。