LAB 1 Amin Modified
LAB 1 Amin Modified
Problem 1.
import numpy as np
➝ Serves for numerical operations
import pandas as pd
➝ Serves for data handling and analysis in tabular form
import matplotlib.pyplot as plt
➝ Useful for plotting, although it’s not used in this code.
from sklearn.linear_model import LogisticRegression
➝ The logistic regression model from scikit-learn
from sklearn.metrics import accuracy_score
➝ Used to evaluate the accuracy of predictions
data dictionary
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
pd.DataFrame(data)
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
X = df[['Feature1', 'Feature2']].values
➝ Retrieves the feature columns in NumPy array format
y = df['Outcome'].values
➝ Retrieves the target variable in NumPy array format
model = LogisticRegression()
➝ sets up the logistic regression model
model.fit(X, y)
➝ Fits the model to data using the input data X and target labels y
predictions = model.predict(X)
➝ Applies the trained model to predict the labels (0 or 1) for the same input data
accuracy = accuracy_score(y, predictions)
➝ Evaluates predictions with actual ones and calculates the accuracy (how many are
correct)
print(f'Accuracy: {accuracy * 100:.2f}%')
➝ Displays the performance metric as a percentage, formatted to two decimal places
Problem 2
import pandas as pd
data = {'Feature1': [0.1, 0.6, 1.0, 1.4], 'Feature2': [1.0, 1.2, 0.8, 0.5], 'Outcome': [0, 1, 1, 0]}
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
df = pd.DataFrame(data)
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
X = df[['Feature1', 'Feature2']].values
y = df['Outcome'].values
lambda_reg = 0.1
➝ This sets the regularization strength (λ). Regularization helps prevent overfitting (when
the model learns noise instead of the actual pattern).
model = LogisticRegression(penalty='l2',C=1/lambda_reg)
➝ This tells the model to use L2 regularization (also known as Ridge regularization). L2
penalizes large weights by adding the sum of squared weights to the loss function. In scikit-
learn, C is the inverse of the regularization strength (C = 1/λ)
model.fit(X, y)
➝ Fits the model to data using the input data X and target labels y
predictions = model.predict(X)
➝ Applies the trained model to predict the labels (0 or 1) for the same input data
➝ Evaluates predictions with actual ones and calculates the accuracy (how many are
correct)
Problem 3
3. mesele
import pandas as pd
data = {
'Outcome': [0, 1, 0, 1]
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
df = pd.DataFrame(data)
➝ A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
X = df[['Feature1', 'Feature2']].values
y = df['Outcome'].values
➝ Defines a logistic regression model for multiclass classification using the LBFGS optimizer
model.fit(X, y)
➝ Fits the model to data using the input data X and target labels y
predictions = model.predict(X)
➝ Applies the trained model to predict the labels (0 or 1) for the same input data
➝ Evaluates predictions with actual ones and calculates the accuracy (how many are
correct)
Problem 4
import pandas as pd
data = {
'Outcome': [1, 0, 1, 0]
-> A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
df = pd.DataFrame(data)
-> A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
X = df[['Feature1', 'Feature2']].values
y = df['Outcome'].values
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
-> Scales the features in X to a 0–1 range based on min and max values
model = LogisticRegression()
model.fit(X_scaled, y)
-> Trains the logistic regression model using the scaled data
predictions = model.predict(X_scaled)
-> Applies the trained model to predict the labels (0 or 1) for the same input data
-> Evaluates predictions with actual ones and calculates the accuracy (how many are
correct)
print(f'Accuracy: {accuracy * 100:.2f}%')
-> Displays the performance metric as a percentage, formatted to two decimal places
Problem 5
import numpy as np
import pandas as pd
class SimpleLogisticRegression:
Various functions
self.learning_rate = learning_rate
self.epochs = epochs
-> sets up the model with a learning rate and number of training iterations (epochs)
return 1 / (1 + np.exp(-z))
-> specifies the sigmoid function, which maps input values to a range between 0 and 1
(Serves for prediction probabilities)
m, n = X.shape
self.theta = np.zeros(n)
for _ in range(self.epochs):
z = np.dot(X, self.theta)
h = self.sigmoid(z)
gradient = np.dot(X.T, (h - y)) / m
-> Fits the model to data by using gradient descent to update weights (theta) and minimize
the cost function
z = np.dot(X, self.theta)
-> produces binary classification outcomes by applying the sigmoid function and classifying
as 1 if ≥ 0.5, else 0
data = {
'Outcome': [0, 1, 1, 0]
-> A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
df = pd.DataFrame(data)
-> A small dataset is constructed where a small dataset with two input features and one
output (target/label):
Feature1 and Feature2 are the inputs (X).
Outcome is the label (y), either 0 or 1.
X = df[['Feature1', 'Feature2']].values
y = df['Outcome'].values
X_bias = np.c_[np.ones(X.shape[0]), X]
-> Adds a column of 1s to X so that the model can learn a bias term (intercept) — this turns
X into [1, x1, x2, ...]
model = SimpleLogisticRegression()
model.fit(X_bias, y)
-> Fits the model to data using the modified feature matrix X_bias (which includes the bias
term) and the labels y
predictions = model.predict(X_bias)
-> Applies the trained model to predict the labels (0 or 1) for the same input data
-> Evaluates predictions with actual ones and calculates the accuracy (how many are
correct)
-> Displays the performance metric as a percentage, formatted to two decimal places