python で書かれた次のコードを R に書き込むにはどうすればよいですか?
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
トレーニング セットとテスト セットへの分割は 80/20 の比率です。
python で書かれた次のコードを R に書き込むにはどうすればよいですか?
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
トレーニング セットとテスト セットへの分割は 80/20 の比率です。
caret
のcreateDataPartition
関数を使用してこれを行うことができます。
library(caret)
# Make example data
X = data.frame(matrix(rnorm(200), nrow = 100))
y = rnorm(100)
#Extract random sample of indices for test data
set.seed(42) #equivalent to python's random_state arg
test_inds = createDataPartition(y = 1:length(y), p = 0.2, list = F)
# Split data into test/train using indices
X_test = X[test_inds, ]; y_test = y[test_inds]
X_train = X[-test_inds, ]; y_train = y[-test_inds]
test_inds
を使用して「ゼロから」作成することもできますtest_inds = sample(1:length(y), ceiling(length(y) * 0.2))