不使用Scikit-Learn的train_test_split分割数据集

我想在不使用sklearn库的情况下分割我的数据集。以下是我使用的方法。

我当前的代码:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)

我尝试过的方法:

def non_shuffling_train_test_split(X, y, test_size=0.2):    i = int((1 - test_size) * X.shape[0]) + 1    X_train, X_test = np.split(X, [i])    y_train, y_test = np.split(y, [i])    return X_train, X_test, y_train, y_test

然而,上述代码并未实现随机化。


回答:

您可以使用np.random.permutation创建一个打乱的顺序,然后使用np.take进行子集选择,这对numpy数组和pandas数据框都适用:

def tt_split(X, y, test_size=0.2):    i = int((1 - test_size) * X.shape[0])     o = np.random.permutation(X.shape[0])        X_train, X_test = np.split(np.take(X,o,axis=0), [i])    y_train, y_test = np.split(np.take(y,o), [i])    return X_train, X_test, y_train, y_test

在numpy数组上测试:

X = np.random.normal(0,1,(50,10))y = np.random.normal(0,1,(50,))X_train, X_test, y_train, y_test = tt_split(X,y)[X_train.shape,y_train.shape][(40, 10), (40,)]

在pandas数据框上测试:

X = pd.DataFrame(np.random.normal(0,1,(50,10)))y = pd.Series(np.random.normal(0,1,50))X_train, X_test, y_train, y_test = tt_split(X,y)[X_train.shape,y_train.shape][(40, 10), (40,)]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注