Learn Machine Learning in One Lesson Book
Learn Machine Learning in One Lesson Book
For Beginners
Ashraf M. Awwad
Oct 2024
Learn Machine Learning in One Lesson: For Beginners
• Supervised Learning: The algorithm learns from labeled data (where the correct
answer is known).
• Labels: The output or target variable (e.g., whether someone is sick or not).
3. Data Preprocessing
Data Cleaning
Data Normalization
• Divide data into training and testing sets, often in an 80/20 split.
python
Copy code
import pandas as pd
# Sample dataset
# Create DataFrame
df = pd.DataFrame(data)
df.fillna(df.mean(), inplace=True)
y = df['Label']
# Train/Test split
Linear Regression
Logistic Regression
Hypothesis Function
The logistic regression hypothesis is based on the logistic function that outputs
probabilities between 0 and 1.
Cost Function
Used to measure how well the model is performing. The goal is to minimize this.
Gradient Descent
An optimization algorithm used to find the best parameters that minimize the cost function.
python
Copy code
y_train = [1, 0, 0, 1]
model = LogisticRegression()
model.fit(X_train, y_train)
# Test data
y_test = [1, 1]
# Predict
predictions = model.predict(X_test)
Decision Trees
python
Copy code
dt_model = DecisionTreeClassifier()
dt_model.fit(X_train, y_train)
dt_predictions = dt_model.predict(X_test)
5. Model Evaluation
python
Copy code
print("Confusion Matrix:")
print(conf_matrix)
print("\nClassification Report:")
print(report)
6. Unsupervised Learning
K-Means Clustering
python
Copy code
data = [[25, 5.5, 65], [34, 6.1, 80], [30, 5.8, 75], [23, 5.0, 52]]
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)
# Predict clusters
clusters = kmeans.predict(data)
print(f"Clusters: {clusters}")