Your First Machine Learning Model in 20 Lines of Python
Machine learning isn't magic — let's prove it
You can build, train, and evaluate a real ML model in under 20 lines. Here's how with scikit-learn.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate
predictions = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2%}")What just happened?
- We loaded a real dataset (150 iris flower samples)
- Split it 80/20 for training vs. testing
- Trained a Random Forest (an ensemble of decision trees)
- Got ~97% accuracy
The hardest part of ML isn't the code — it's understanding your data. Start here, then go deeper.
Comments
0
Loading comments…
No comments yet. Be the first to share your thoughts!