Introduction
Proper evaluation helps us check if our model really learned to solve the problem or just memorized the training data. This stops the model from making mistakes on new data.
Split your data into training and testing sets. Train your model on the training set. Evaluate your model on the testing set. Use metrics like accuracy, loss, or error to measure performance.
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model.fit(X_train, y_train) score = model.score(X_test, y_test)
from sklearn.model_selection import cross_val_score scores = cross_val_score(model, X, y, cv=5) print(scores.mean())
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 data X, y = load_iris(return_X_y=True) # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Train model model = DecisionTreeClassifier(random_state=42) model.fit(X_train, y_train) # Predict on test data predictions = model.predict(X_test) # Evaluate accuracy accuracy = accuracy_score(y_test, predictions) print(f"Test Accuracy: {accuracy:.2f}")
from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score X = [[1], [2], [3], [4], [5], [6]] y = [0, 0, 0, 1, 1, 1] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(round(accuracy, 2))What will be the printed accuracy?
Pass the test and this pattern moves unknown