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

lOGISTIC REGRESSION

Uploaded by

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

lOGISTIC REGRESSION

Uploaded by

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

DEMONSTRATE LOGISTIC REGRESSION THROUGH PYTHON CODE.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.datasets import load_iris

# Load the Iris dataset


iris = load_iris()

# Create a DataFrame from the dataset


data = pd.DataFrame(data=iris.data, columns=iris.feature_names)
data['species'] = iris.target

# View the first few rows


print(data.head())

# BINARY CLASSIFICATION: FILTER FOR SETOSA (CLASS 0) VS. NOT-SETOSA (CLASS 1)


DATA['IS_SETOSA'] = (DATA['SPECIES'] == 0).ASTYPE(INT)

# FEATURES (INDEPENDENT VARIABLES)


X = DATA.ILOC[:, :-2] # USE ALL FEATURE COLUMNS

# TARGET (DEPENDENT VARIABLE)


Y = DATA['IS_SETOSA']

# Split into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print("Training Features Shape:", X_train.shape)


print("Testing Features Shape:", X_test.shape)

Training Features Shape: (120, 4)


Testing Features Shape: (30, 4)
[5]:

# Initialize and train the Logistic Regression model


model = LogisticRegression()
model.fit(X_train, y_train)

# Print model coefficients


print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)

Coefficients: [[-0.42762216 0.88771927 -2.21471658 -0.91610036]]


Intercept: [6.24415327]
# Make predictions
y_pred = model.predict(X_test)

# Predict probabilities (optional)


y_prob = model.predict_proba(X_test)
print("Predicted Probabilities:\n", y_prob)

# Accuracy score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", conf_matrix)

# Classification report
class_report = classification_report(y_test, y_pred)
print("Classification Report:\n", class_report)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.datasets import load_iris

# Step 1: Load the dataset


iris = load_iris()
data = pd.DataFrame(data=iris.data, columns=iris.feature_names)
data['species'] = iris.target

# Step 2: Filter for binary classification (Setosa vs. Not-Setosa)


data['is_setosa'] = (data['species'] == 0).astype(int)
X = data.iloc[:, :-2] # Features
y = data['is_setosa'] # Target

# Step 3: Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Train Logistic Regression model


model = LogisticRegression()
model.fit(X_train, y_train)

# Step 5: Make predictions


y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)

# Step 6: Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)

# Print results
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Classification Report:\n", class_report)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy