0% found this document useful (0 votes)
15 views6 pages

K Fold

The document contains multiple questions demonstrating the use of machine learning techniques on the Iris and Libras datasets using Python's scikit-learn library. It includes implementations of Logistic Regression and Random Forest classifiers with cross-validation, as well as performance metrics like accuracy, precision, recall, and F1-score. The results show varying accuracies for different models and datasets, highlighting the effectiveness of these classifiers.
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)
15 views6 pages

K Fold

The document contains multiple questions demonstrating the use of machine learning techniques on the Iris and Libras datasets using Python's scikit-learn library. It includes implementations of Logistic Regression and Random Forest classifiers with cross-validation, as well as performance metrics like accuracy, precision, recall, and F1-score. The results show varying accuracies for different models and datasets, highlighting the effectiveness of these classifiers.
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/ 6

QUESTION 1:

from sklearn.datasets import load_iris

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import cross_val_score

from sklearn.datasets import load_iris

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import cross_val_score

import numpy as np

iris = load_iris()

x,y = iris.data, iris.target

logreg = LogisticRegression(max_iter=200,random_state=42)

k = 10

cv_scores = cross_val_score(logreg, x, y, cv=k, scoring='accuracy')

accuracy = np.mean(cv_scores)

print(f"Cross-validation scores:{cv_scores}")

print(f"accuracy:{accuracy: .4f}")

OUTPUT: Accuracy: 0.9733

QUESTION 2:

from sklearn.datasets import load_iris

from sklearn.model_selection import StratifiedKFold, cross_val_score

from sklearn.linear_model import LogisticRegression

from sklearn.preprocessing import StandardScaler

import numpy as np

# Load Iris dataset

iris = load_iris()

X, y = iris.data, iris.target

# Standardize the features for better performance


scaler = StandardScaler()

X = scaler.fit_transform(X)

# Define Logistic Regression model

model = LogisticRegression(max_iter=200)

# Set up Stratified K-Fold Cross-Validation

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

# Perform cross-validation and calculate accuracy

scores = cross_val_score(model, X, y, cv=skf, scoring='accuracy')

# Display results

print(f"Accuracy: {scores.mean():.4f} ± {scores.std():.4f}")

OUTPUT: Accuracy: 0.9533 ± 0.0452

QUESTION 3:

from sklearn.datasets import load_iris

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import cross_val_score, StratifiedKFold

import numpy as np

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Initialize the RandomForestClassifier

rf_model = RandomForestClassifier(random_state=42)
# Set up K-Fold Cross-Validation (Stratified to maintain class balance)

kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)

# Perform cross-validation and compute accuracy for each fold

cv_scores = cross_val_score(rf_model, X, y, cv=kfold, scoring='accuracy')

# Display the accuracy and standard deviation

print(f" Accuracy: {np.mean(cv_scores):.4f}")

OUTPUT: Accuracy: 0.9533

QUESTION 4:

from sklearn.datasets import load_iris

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import StratifiedKFold, cross_val_score

import numpy as np

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Initialize the RandomForestClassifier

rf_model = RandomForestClassifier(random_state=42)

# Set up Stratified K-Fold Cross-Validation (10 folds)

stratified_kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)

# Perform cross-validation and compute accuracy for each fold

cv_scores = cross_val_score(rf_model, X, y, cv=stratified_kfold, scoring='accuracy')


# Display the accuracy and standard deviation

print(f"Accuracy: {np.mean(cv_scores):.4f}")

OUTPUT: Accuracy: 0.9533

QUESTION 5:

# Load the Libras dataset

libras_data_path = '/mnt/data/movement_libras.data'

libras_data = pd.read_csv(libras_data_path, header=None)

# Display the first few rows of the dataset

libras_data.head()

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Separate features and target

X_libras = libras_data.iloc[:, :-1] # Features (columns 0 to 89)

y_libras = libras_data.iloc[:, -1] # Target (column 90)

# Split data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X_libras, y_libras, test_size=0.2, random_state=42)

# Train the random forest model

rf_model_libras = RandomForestClassifier(random_state=42)

rf_model_libras.fit(X_train, y_train)

# Make predictions on the test set

y_pred = rf_model_libras.predict(X_test)
# Calculate metrics

accuracy = accuracy_score(y_test, y_pred)

precision = precision_score(y_test, y_pred, average='weighted')

recall = recall_score(y_test, y_pred, average='weighted')

f1 = f1_score(y_test, y_pred, average='weighted')

accuracy, precision, recall, f1

OUTPUT:

Accuracy: 0.8055555555555556

Precision: 0.8413018308851642

Recall: 0.8055555555555556

F1-score: 0.8074919315718324

Classification Report:

precision recall f1-score support

1 0.88 0.88 0.88 8

2 0.83 0.62 0.71 8

3 1.00 0.83 0.91 6

4 1.00 0.91 0.95 11

5 0.75 0.75 0.75 8

6 1.00 0.50 0.67 6

7 0.88 1.00 0.93 7

8 0.40 0.67 0.50 3

9 0.77 1.00 0.87 10

10 1.00 0.78 0.88 9

11 0.60 1.00 0.75 3

12 0.71 0.83 0.77 6

13 0.67 0.67 0.67 6

14 0.64 0.88 0.74 8


15 1.00 0.67 0.80 9

accuracy 0.81 108

macro avg 0.81 0.80 0.78 108

weighted avg 0.84 0.81 0.81 108

You might also like