0% found this document useful (0 votes)
8 views8 pages

LAB 1 Amin Modified

The document outlines a series of laboratory problems focused on implementing logistic regression using Python libraries such as NumPy, pandas, and scikit-learn. Each problem involves constructing a dataset, fitting a logistic regression model, making predictions, and calculating accuracy. The document also introduces concepts like regularization and custom class implementation for logistic regression.

Uploaded by

aqasmv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

LAB 1 Amin Modified

The document outlines a series of laboratory problems focused on implementing logistic regression using Python libraries such as NumPy, pandas, and scikit-learn. Each problem involves constructing a dataset, fitting a logistic regression model, making predictions, and calculating accuracy. The document also introduces concepts like regularization and custom class implementation for logistic regression.

Uploaded by

aqasmv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Laboratory work 1

Amin Qurbanov 652i

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

➝ 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 = {'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

➝ Retrieves the feature columns in NumPy array format

y = df['Outcome'].values

➝ Retrieves the target variable in NumPy array format

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

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 3

3. mesele

from sklearn.linear_model import LogisticRegression

➝ The logistic regression model from scikit-learn

import pandas as pd

➝ Serves for data handling and analysis in tabular form

from sklearn.metrics import accuracy_score

➝ Used to evaluate the accuracy of predictions

data = {

'Feature1': [0.1, 0.4, 0.2, 0.5],

'Feature2': [0.3, 0.5, 0.1, 0.7],

'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

➝ Retrieves the feature columns in NumPy array format

y = df['Outcome'].values

➝ Retrieves the target variable in NumPy array format

model = LogisticRegression(multi_class='multinomial', solver='lbfgs')

➝ 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

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 4

from sklearn.preprocessing import MinMaxScaler

->Imports a tool to scale features between 0 and 1

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

import pandas as pd

->Serves for data handling and analysis in tabular form

data = {

'Feature1': [2, 3, 4, 5],


'Feature2': [8, 6, 9, 7],

'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

-> Retrieves the feature columns in NumPy array format

y = df['Outcome'].values

-> Retrieves the target variable in NumPy array format

scaler = MinMaxScaler()

-> sets up the scaler that will normalize feature values

X_scaled = scaler.fit_transform(X)

-> Scales the features in X to a 0–1 range based on min and max values

model = LogisticRegression()

sets up the logistic regression model

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

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 5

import numpy as np

Serves for numerical operations

import pandas as pd

->Serves for data handling and analysis in tabular form

from sklearn.metrics import accuracy_score

Used to evaluate the accuracy of predictions

class SimpleLogisticRegression:

->Defines a custom-defined class named simplelogisticregression with

Various functions

def _init_(self, learning_rate=0.01, epochs=1000):

self.learning_rate = learning_rate

self.epochs = epochs

-> sets up the model with a learning rate and number of training iterations (epochs)

def sigmoid(self, z):

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)

def fit(self, X, y):

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

self.theta -= self.learning_rate * gradient

-> Fits the model to data by using gradient descent to update weights (theta) and minimize
the cost function

def predict(self, X):

z = np.dot(X, self.theta)

return self.sigmoid(z) >= 0.5

-> produces binary classification outcomes by applying the sigmoid function and classifying
as 1 if ≥ 0.5, else 0

data = {

'Feature1': [0.1, 0.4, 0.6, 0.8],

'Feature2': [0.5, 0.7, 0.2, 0.9],

'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

-> Retrieves the feature columns in NumPy array format

y = df['Outcome'].values

-> Retrieves the target variable in NumPy array format

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()

-> Defines an instance of your custom SimpleLogisticRegression class

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

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

You might also like