【发布时间】:2016-12-22 00:16:23
【问题描述】:
我正在使用 Pandas 在内存中加载二维数据集,并执行 4 个简单的机器学习预处理任务,例如添加/删除列、重新索引、训练/测试拆分。
#Read file
MLMe = pd.read_table("data/dtCTG.txt", ",")
#Label target column to "class"
MLMe.rename(columns={'NSP' : 'class'}, inplace=True)
#Create train/test indices
MLMe_class = MLMe['class'].values
training_indices, validation_indices = training_indices, testing_indices = train_test_split(
MLMe.index, stratify = MLMe_class, train_size=0.75, test_size=0.25)
#Create train/test data sets
X_train = MLMe.drop('class',axis=1).loc[training_indices].values
y_train = MLMe.loc[training_indices,'class'].values
X_test = MLMe.drop('class',axis=1).loc[validation_indices].values
y_test = MLMe.loc[validation_indices, 'class'].values
#Final datasets to be used for training
X_train, y_train, X_test, y_test
现在,当我将 X_train、y_train 数据帧传递给某些库时,我收到一条错误消息,指出缓冲区不再是 C 连续的。
BufferError: memoryview: underlying buffer is not C-contiguous
我的问题是: 如何制作 X_train、y_train C 连续缓冲区?我尝试使用 C 和 F 选项进行整形,但没有成功。
编辑:以下是数据帧的形状、数据类型和标志:
X_train.shape, y_train.shape, X_test.shape, y_test.shape
((1104, 9), (1104,), (369, 9), (369,))
X_train.dtype, y_train.dtype, X_test.dtype, y_test.dtype
(dtype('int64'), dtype('int64'), dtype('int64'), dtype('int64'))
X_train.flags, y_train.flags, X_test.flags, y_test.flags
( C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False,
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False,
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False,
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
)
【问题讨论】:
-
向我们展示相关数组的 dtype、shape、FLAGS。
-
你试过
X_train=np.ascontiguousarray(X_train)吗? -
我为数据集添加了 dtype、shape、FLAGS 信息。两个不是 C_Contiguous:我想解决方案是让它们成为 C_Contiguous,但不确定如何。
-
@Happy001 哇,您的解决方案成功了!谢谢!
标签: python python-3.x pandas numpy scikit-learn