Lab Assignment-4
Assignment-4- week 4 July
Learning Objective: Apply Logisitc Regression
1.A teacher collected data from previous students regarding the number of hours they studied before an examination and whether they passed the exam.
Tasks:
● Load and preprocess the Pima Indians Diabetes dataset.
● Implement logistic regression for binary classification.
● Evaluate model performance with and without feature scaling.
● Analyze metrics such as accuracy, precision, recall, and F1-score.
Using the given dataset,
- Read the dataset.
- Train a Logistic Regression model.( gradient descent - do not use library)
- Calculate the model accuracy.
- Predict whether a student studying 4.5 hours, 6 hours, and 9 hours is likely to pass.
- Plot the data points along with the logistic regression curve.
Dataset
| Hours Studied | Pass (Target) |
|---|---|
| 1.0 | 0 |
| 1.5 | 0 |
| 2.0 | 0 |
| 2.5 | 0 |
| 3.0 | 0 |
| 3.5 | 0 |
| 4.0 | 0 |
| 4.5 | 1 |
| 5.0 | 1 |
| 5.5 | 1 |
| 6.0 | 1 |
| 6.5 | 1 |
| 7.0 | 1 |
| 7.5 | 1 |
| 8.0 | 1 |
| 8.5 | 1 |
| 9.0 | 1 |
| 9.5 | 1 |
| 10.0 | 1 |
| 10.5 | 1 |
Students can either type this data into Python or save it as student_pass.csv.
2.A hospital has collected several diagnostic measurements from breast cancer patients. Each record contains features extracted from cell nuclei images. The target variable indicates whether the tumor is:
- 0 → Malignant
- 1 → Benign
Build a Logistic Regression model to classify tumors and evaluate its performance using different metrics.
Dataset
Use the built-in dataset available in Scikit-Learn.
from sklearn.datasets import load_breast_cancer data = load_breast_cancer()Dataset Information
Number of samples: 569
Number of features: 30
Number of classes: 2
Target names:
Malignant
Benign
Tasks
Part A – Load and Explore the Dataset
- Load the Breast Cancer dataset.
- Convert it into a Pandas DataFrame.
-
Display:
- First five rows
- Dataset shape
- Column names
- Data types
- Number of missing values
- Class distribution
Part B – Data Preprocessing
-
Separate the independent variables (
X) and target (y). - Split the dataset into 80% training and 20% testing.
-
Standardize the feature values using
StandardScaler.
Part C – Build the Logistic Regression Model
- Train a Logistic Regression classifier.
- Predict the labels of the test dataset.
-
Predict the class probabilities using
predict_proba().
Part D – Evaluate the Model
Calculate the following performance metrics:
- Accuracy
- Precision
- Recall
- F1-score
- Confusion Matrix
- Classification Report
Display the confusion matrix as a heatmap.
Part E – ROC Curve
- Compute the ROC Curve.
- Calculate the Area Under Curve (AUC).
- Plot the ROC curve.
- Interpret the result.
Part F – Probability Prediction
Predict the probability that the following patient has a malignant tumor.
sample = X_test.iloc[0:1]
Additional Exercises
- Change the test size from 20% to 30%. Compare the evaluation metrics.
- Train the model without feature scaling. Compare the accuracy with the scaled version.
- Change the random state from 42 to 10 and observe the change in performance.
- Print the coefficients of the Logistic Regression model and identify the five most influential features.
-
Predict the diagnosis for the first five test samples using
predict()andpredict_proba(). - Change the classification threshold from 0.5 to 0.7 and observe how precision and recall change.
3.Implement a logistic regression model to predict the likelihood of a disease using the Pima Indians Diabetes dataset. Compare the performance with and without feature scaling.
Tasks:
● Load and preprocess the Pima Indians Diabetes dataset.
● Implement logistic regression for binary classification.
● Evaluate model performance with and without feature scaling.
● Analyze metrics such as accuracy, precision, recall, and F1-score.
Comments
Post a Comment