Home
Omar Hosney
Machine Learning Techniques Cheatsheet
Linear Regression 📈
- Predicts a continuous dependent variable.
- Minimizes the sum of squared residuals.
- Assumes a linear relationship.
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Logistic Regression 🚀
- Classifies binary outcomes.
- Uses the logistic function.
- Estimates probabilities.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Decision Trees 🌳
- Splits data into branches.
- Handles both classification and regression.
- Interpretable model with clear rules.
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Random Forest 🌲🌲
- Ensemble method using multiple trees.
- Reduces overfitting.
- Combines multiple decision trees.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Support Vector Machines (SVM) 🛡️
- Classifies by finding the optimal hyperplane.
- Effective in high-dimensional spaces.
- Works well with clear margin of separation.
from sklearn.svm import SVC
model = SVC()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
K-Nearest Neighbors (KNN) 👥
- Classifies based on closest data points.
- Simple and intuitive method.
- Non-parametric and lazy learning.
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
K-Means Clustering 🎯
- Clusters data into K distinct clusters.
- Iterative approach to minimize variance.
- Unsupervised learning algorithm.
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(X)
clusters = model.predict(X)
DBScan 🚀
- Density-based clustering algorithm.
- Identifies clusters with arbitrary shape.
- Does not require a predefined number of clusters.
from sklearn.cluster import DBSCAN
model = DBSCAN(eps=0.5, min_samples=5)
model.fit(X)
clusters = model.labels_
Hierarchical Clustering 🏰
- Builds a hierarchy of clusters.
- Uses a tree-like structure (dendrogram).
- Two types: agglomerative and divisive.
from scipy.cluster.hierarchy import dendrogram, linkage
linked = linkage(X, 'single')
dendrogram(linked)