Machine Learning enables computers to learn from data without being explicitly programmed. This guide covers the fundamentals with scikit-learn.
Linear Regression Example
from sklearn.linear_model import LinearRegression
import numpy as np
# Sample data
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# Create and train model
model = LinearRegression()
model.fit(X, y)
# Make predictions
prediction = model.predict([[6]])
print(f"Predicted: {prediction[0]}") # 12.0
Classification with Decision Trees
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
# Sample dataset
X = [[0, 0], [1, 1], [2, 2], [3, 3]]
y = [0, 0, 1, 1]
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
# Train model
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# Evaluate
accuracy = clf.score(X_test, y_test)
print(f"Accuracy: {accuracy * 100}%")
Model Evaluation
from sklearn.metrics import accuracy_score, confusion_matrix
predictions = clf.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
cm = confusion_matrix(y_test, predictions)
print(f"Accuracy: {accuracy}")
print(f"Confusion Matrix:\n{cm}")
Start with these basics and gradually explore more complex algorithms!