Introduction
We split data into training and testing parts to teach the model and then check how well it learned on new data.
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, shuffle=False)
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score # Load example data iris = load_iris() X, y = iris.data, iris.target # Split data: 30% test, 70% train X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # Create and train model model = DecisionTreeClassifier(random_state=1) model.fit(X_train, y_train) # Predict on test data predictions = model.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, predictions) print(f"Test set size: {len(X_test)} samples") print(f"Accuracy on test set: {accuracy:.2f}")
train-test split in machine learning?train_test_split function from scikit-learn?X_train and X_test after this code?from sklearn.model_selection import train_test_split X = [[1], [2], [3], [4], [5]] X_train, X_test = train_test_split(X, test_size=0.4, random_state=42) print(len(X_train), len(X_test))
from sklearn.model_selection import train_test_split X = [1, 2, 3, 4, 5] X_train, X_test = train_test_split(X, test=0.3) print(X_train, X_test)
test_size value should you use in train_test_split?Pass the test and this pattern moves unknown