03/12/2024, 23:43 Untitled4.
ipynb - Colab
import pandas as pd
import [Link] as plt
import numpy as np
from sklearn.model_selection import train_test_split
import seaborn as sns
from [Link] import classification_report
from [Link] import StandardScaler, RobustScaler, MinMaxScaler, Binarizer
from [Link] import DecisionTreeClassifier
from [Link] import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from [Link] import resample
from [Link] import KMeans
#Loading the dataset
gym_data = pd.read_excel('/content/project_final.xlsx')
gym_df = [Link](gym_data)
gym_df.head()
Weight Height Session_Duration Water
Age Gender Max_BPM Avg_BPM Resting_BPM Calories_Burned Workout_Type Fat_Percentage
(kg) (m) (hours) (
0 56 Male 88.3 1.71 180 157 60 1.69 1313 Yoga 12.6
1 46 Female 74.9 1.53 179 151 66 1.30 883 HIIT 33.9
2 32 Female 68.1 1.66 167 122 54 1.11 677 Cardio 33.4
3 25 Male 53.2 1.70 190 164 56 0.59 532 Strength 28.8
4 38 Male 46.1 1.79 188 158 68 0.64 556 Strength 29.2
Next steps: Generate code with gym_df
toggle_off View recommended plots New interactive sheet
#check for missing values
gym_df.isnull().sum()
Age 0
Gender 0
Weight (kg) 0
Height (m) 0
Max_BPM 0
Avg_BPM 0
Resting_BPM 0
Session_Duration (hours) 0
Calories_Burned 0
Workout_Type 0
Fat_Percentage 0
Water_Intake (liters) 0
Workout_Frequency (days/week) 0
Experience_Level 0
BMI 0
dtype: int64
gym_df.describe()
[Link] 1/9
03/12/2024, 23:43 [Link] - Colab
Weight Height Session_Duration Wate
Age Max_BPM Avg_BPM Resting_BPM Calories_Burned Fat_Percentage
(kg) (m) (hours)
count 311.000000 311.000000 311.000000 311.000000 311.000000 311.000000 311.000000 311.000000 311.000000 3
mean 38.987138 74.181029 1.721093 179.228296 144.189711 62.250804 1.265531 913.112540 25.027653
std 12.384427 22.174464 0.122922 11.691297 13.989836 7.089847 0.331039 265.231129 6.107415
min 18.000000 40.000000 1.500000 160.000000 120.000000 50.000000 0.510000 333.000000 10.200000
25% 28.000000 57.400000 1.620000 169.000000 132.000000 56.000000 1.080000 734.500000 20.950000
50% 40.000000 69.700000 1.710000 179.000000 144.000000 62.000000 1.270000 903.000000 26.500000
75% 50.000000 86.950000 1.790000 189.000000 156.000000 68.000000 1.460000 1077.500000 29.400000
max 59.000000 129.500000 2.000000 199.000000 169.000000 74.000000 1.990000 1701.000000 34.800000
gym_df.info()
<class '[Link]'>
RangeIndex: 311 entries, 0 to 310
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Age 311 non-null int64
1 Gender 311 non-null object
2 Weight (kg) 311 non-null float64
3 Height (m) 311 non-null float64
4 Max_BPM 311 non-null int64
5 Avg_BPM 311 non-null int64
6 Resting_BPM 311 non-null int64
7 Session_Duration (hours) 311 non-null float64
8 Calories_Burned 311 non-null int64
9 Workout_Type 311 non-null object
10 Fat_Percentage 311 non-null float64
11 Water_Intake (liters) 311 non-null float64
12 Workout_Frequency (days/week) 311 non-null int64
13 Experience_Level 311 non-null int64
14 BMI 311 non-null float64
dtypes: float64(6), int64(7), object(2)
memory usage: 36.6+ KB
gym_df.shape
(311, 15)
cols = ['Weight (kg)','Height (m)','Max_BPM','Avg_BPM','Session_Duration (hours)','Calories_Burned','Water_Intake (liters)','Workout_Freq
gym_df = gym_df.drop(cols, axis=1)
gym_df.head()
Age Gender Resting_BPM Workout_Type Fat_Percentage BMI
0 56 Male 60 Yoga 12.6 30.20
1 46 Female 66 HIIT 33.9 32.00
2 32 Female 54 Cardio 33.4 24.71
3 25 Male 56 Strength 28.8 18.41
4 38 Male 68 Strength 29.2 14.39
Next steps: Generate code with gym_df toggle_off View recommended plots New interactive sheet
gym_df.describe()
[Link] 2/9
03/12/2024, 23:43 [Link] - Colab
Age Resting_BPM Fat_Percentage BMI
count 311.000000 311.000000 311.000000 311.000000
mean 38.987138 62.250804 25.027653 25.028264
std 12.384427 7.089847 6.107415 6.907137
min 18.000000 50.000000 10.200000 12.470000
25% 28.000000 56.000000 20.950000 19.705000
50% 40.000000 62.000000 26.500000 24.500000
75% 50.000000 68.000000 29.400000 29.160000
max 59.000000 74.000000 34.800000 47.720000
gym_df.shape
(311, 6)
gym_df = gym_df.dropna()
mapping1 = {'Male':0 , 'Female':1}
gym_df['Gender'] = gym_df['Gender'].map(mapping1)
mapping2 = {'Yoga': 1, 'HIIT': 2, 'Cardio': 3, 'Strength': 4}
gym_df['Workout_Type'] = gym_df['Workout_Type'].map(mapping2)
gym_df.head()
Age Gender Resting_BPM Workout_Type Fat_Percentage BMI
0 56 0 60 1 12.6 30.20
1 46 1 66 2 33.9 32.00
2 32 1 54 3 33.4 24.71
3 25 0 56 4 28.8 18.41
4 38 0 68 4 29.2 14.39
gym_df = gym_df.dropna()
type(gym_df)
[Link]
def __init__(data=None, index: Axes | None=None, columns: Axes | None=None, dtype: Dtype |
None=None, copy: bool | None=None) -> None
Two-dimensional, size-mutable, potentially heterogeneous tabular data.
Data structure also contains labeled axes (rows and columns).
Arithmetic operations align on both row and column labels. Can be
thought of as a dict-like container for Series objects. The primary
pandas data structure.
gym_df = gym_df.interpolate()
numerical_columns = ['Age', 'Gender', 'Workout_Type', 'Resting_BPM', 'Fat_Percentage', 'BMI']
#removing outliers
for column in numerical_columns :
Q1 = gym_df[column].quantile(0.25)
Q3 = gym_df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
gym_df = gym_df[(gym_df[column] >= lower_bound) & (gym_df[column] <= upper_bound)]
print("outliers removed")
[Link] 3/9
03/12/2024, 23:43 [Link] - Colab
outliers removed
#specify features/independent variables and target/dependent variable
X = ['Age', 'Gender', 'Resting_BPM', 'Fat_Percentage', 'BMI']
Y = 'Workout_Type'
type(gym_df)
[Link]
def __init__(data=None, index: Axes | None=None, columns: Axes | None=None, dtype: Dtype |
None=None, copy: bool | None=None) -> None
Dict can contain Series, arrays, constants, dataclass or list-like objects. If
data is a dict, column order follows insertion-order. If a dict contains Series
which have an index defined, it is aligned by its index. This alignment also
occurs if data is a Series or a DataFrame itself. Alignment is done on
Series/DataFrame inputs.
If data is a list of dicts column order follows insertion-order
#boxplot
[Link](figsize=(20, 5))
gym_df.boxplot(column=X, vert=False)
[Link]('Boxplot of Features')
[Link]()
# Visualizing outliers with scatter plots
[Link](figsize=(12,6))
[Link](data = gym_df, x='BMI', y='Fat_Percentage', hue='Gender', palette='viridis')
[Link]('Outlier Detection using KMeans - BMI vs Fat Percentage')
[Link]()
[Link] 4/9
03/12/2024, 23:43 [Link] - Colab
# Using KMeans for outlier detection is inefficient since it does not give a true picture of the data
kmeans = KMeans(n_clusters=2, random_state=42)
X_data = gym_df[['Age', 'Gender', 'Resting_BPM']]
[Link](X_data)
▾ KMeans i ?
KMeans(n_clusters=2, random_state=42)
# Assigning the cluster labels
gym_df['Cluster'] = kmeans.labels_
# Visualizing outliers with scatter plots
[Link](figsize=(12,6))
[Link](data = gym_df, x='BMI', y='Fat_Percentage', hue='Cluster', palette='viridis')
[Link]('Outlier Detection using KMeans - BMI vs Fat Percentage')
[Link]()
[Link] 5/9
03/12/2024, 23:43 [Link] - Colab
# Assuming 'gym_df' is your DataFrame and you want to scale all numerical columns:
X = gym_df[['Age', 'Resting_BPM', 'BMI', 'Fat_Percentage']] # Select only numerical features
# Convert 'Gender' to numerical representation if needed using one-hot encoding or label encoding
# For example, using LabelEncoder:
from [Link] import LabelEncoder
encoder = LabelEncoder()
gym_df['Gender_Encoded'] = encoder.fit_transform(gym_df['Gender'])
X['Gender_Encoded'] = gym_df['Gender_Encoded']
# Now apply RobustScaler
robust_scaler = RobustScaler()
x_robust_scaled = robust_scaler.fit_transform(X)
X_robust = [Link](x_robust_scaled, columns=[Link])
<ipython-input-28-81a7807870d4>:9: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: [Link]
X['Gender_Encoded'] = gym_df['Gender_Encoded']
#Handling inconsistent values using Min-Max scaling
min_max_scaler = MinMaxScaler(feature_range=(0, 1))
x_min_max_scaled = min_max_scaler.fit_transform(X_robust)
X_min_max = [Link](x_min_max_scaled, columns=X_robust.columns)
# Standardize the features
scaler = StandardScaler()
scaled_features = scaler.fit_transform(X)
X = [Link](scaled_features, columns= [Link])
#question5
def apply_kmeans(X, n_clusters, max_iter):
kmeans = KMeans(n_clusters=n_clusters, max_iter=max_iter, n_init=10, random_state=42)
MSE_list = []
for i in range(1, max_iter + 1):
kmeans.set_params(max_iter=i)
[Link](X)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
# Calculate MSE using NumPy operations to avoid KeyError
MSE = [Link]([Link](((X - centroids[labels])**2), axis=1))
MSE_list.append(MSE)
return MSE_list
# Parameters
n_clusters = 3 # Number of clusters
max_iter = 20 # Number of iterations
MSE_list = apply_kmeans(X, n_clusters, max_iter)
[Link](range(1, max_iter + 1), MSE_list, marker='o')
[Link](f"MSE for K-means Clustering with k={n_clusters}")
[Link]("Iterations")
[Link]("MSE")
[Link]()
[Link] 6/9
03/12/2024, 23:43 [Link] - Colab
#QUESTION 4
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.naive_bayes import GaussianNB
from [Link] import KNeighborsClassifier
from [Link] import DecisionTreeClassifier
from [Link] import StandardScaler
from [Link] import accuracy_score, precision_score, recall_score, f1_score
# Function to evaluate classifiers
def evaluate_classifier(clf, X_train, X_test, y_train, y_test):
[Link](X_train, y_train)
y_pred = [Link](X_test)
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')
return accuracy, precision, recall, f1
# Define classifiers
classifiers = {
'Naive Bayes': GaussianNB(),
'K-Nearest Neighbors': KNeighborsClassifier(),
'Decision Tree': DecisionTreeClassifier()
}
# Assuming 'df' is your DataFrame and 'Workout_Type' is the target column
Y = gym_df['Workout_Type'].values # Extract the actual target variable data
# Now use the extracted data in train_test_split
X_train, X_test, y_train, y_test = train_test_split(scaled_features, Y, test_size=0.2)
print("Holdout (80/20)")
for name, clf in [Link]():
accuracy, precision, recall, f1 = evaluate_classifier(clf, X_train, X_test, y_train, y_test)
print(f"{name}: Accuracy={accuracy:.4f}, Precision={precision:.4f}, Recall={recall:.4f}, F1={f1:.4f}")
Holdout (80/20)
Naive Bayes: Accuracy=0.2295, Precision=0.2384, Recall=0.2295, F1=0.2272
K-Nearest Neighbors: Accuracy=0.2295, Precision=0.2321, Recall=0.2295, F1=0.2231
Decision Tree: Accuracy=0.2623, Precision=0.2499, Recall=0.2623, F1=0.2444
# Holdout (66.6/33.3 split)
X_train, X_test, y_train, y_test = train_test_split(scaled_features, Y , test_size=0.333)
print("\nHoldout (66.6/33.3)")
for name, clf in [Link]():
accuracy, precision, recall, f1 = evaluate_classifier(clf, X_train, X_test, y_train, y_test)
print(f"{name}: Accuracy={accuracy:.4f}, Precision={precision:.4f}, Recall={recall:.4f}, F1={f1:.4f}")
[Link] 7/9
03/12/2024, 23:43 [Link] - Colab
Holdout (66.6/33.3)
Naive Bayes: Accuracy=0.2745, Precision=0.3234, Recall=0.2745, F1=0.2574
K-Nearest Neighbors: Accuracy=0.2843, Precision=0.3179, Recall=0.2843, F1=0.2873
Decision Tree: Accuracy=0.2647, Precision=0.2559, Recall=0.2647, F1=0.2575
# 10-fold Cross-validation
print("\n10-fold Cross-validation")
for name, clf in [Link]():
scores = cross_val_score(clf, scaled_features, Y , cv=10, scoring='accuracy')
print(f"{name}: Accuracy (mean)={[Link]():.4f}, Accuracy (std)={[Link]():.4f}")
# 5-fold Cross-validation
print("\n5-fold Cross-validation")
for name, clf in [Link]():
scores = cross_val_score(clf, scaled_features, Y, cv=5, scoring='accuracy')
print(f"{name}: Accuracy (mean)={[Link]():.4f}, Accuracy (std)={[Link]():.4f}")
/usr/local/lib/python3.10/dist-packages/ipykernel/[Link]: DeprecationWarning: `should_run_async` will not call `transform_c
and should_run_async(code)
10-fold Cross-validation
Naive Bayes: Accuracy (mean)=0.2749, Accuracy (std)=0.0388
K-Nearest Neighbors: Accuracy (mean)=0.2844, Accuracy (std)=0.0728
Decision Tree: Accuracy (mean)=0.2524, Accuracy (std)=0.0969
5-fold Cross-validation
Naive Bayes: Accuracy (mean)=0.2623, Accuracy (std)=0.0344
K-Nearest Neighbors: Accuracy (mean)=0.2689, Accuracy (std)=0.0245
Decision Tree: Accuracy (mean)=0.2131, Accuracy (std)=0.0374
#QUESTION3
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
from [Link] import TransactionEncoder
workouts = gym_df.groupby('BMI')['Resting_BPM'].apply(list)
/usr/local/lib/python3.10/dist-packages/ipykernel/[Link]: DeprecationWarning: `should_run_async` will not call `transform_c
and should_run_async(code)
te = TransactionEncoder()
te_ary = [Link](transactions).transform(transactions)
workouts_df = [Link](te_ary, columns=te.columns_)
/usr/local/lib/python3.10/dist-packages/ipykernel/[Link]: DeprecationWarning: `should_run_async` will not call `transform_c
and should_run_async(code)
# Function to run Apriori and extract association rules
def run_apriori(min_support, min_confidence):
# Find frequent itemsets
frequent_itemsets = apriori(workouts_df, min_support=min_support, use_colnames=True)
# Check if frequent_itemsets is empty
if frequent_itemsets.empty:
print(f"No frequent itemsets found for min_support = {min_support}")
return None, None
# Generate association rules
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=min_confidence, num_itemsets=len(frequent_itemsets))
return frequent_itemsets, rules
# Test with a lower minimum support if needed
frequent_itemsets_1, rules_1 = run_apriori(min_support=0.1, min_confidence=0.5)
if frequent_itemsets_1 is not None:
print("Frequent Itemsets:")
[Link] 8/9
03/12/2024, 23:43 [Link] - Colab
print(frequent_itemsets_1)
print("\nAssociation Rules:")
print(rules_1)
else:
print("No frequent itemsets found.")
No frequent itemsets found for min_support = 0.1
No frequent itemsets found.
/usr/local/lib/python3.10/dist-packages/ipykernel/[Link]: DeprecationWarning: `should_run_async` will not call `transform_c
and should_run_async(code)
# Case a: Minimum support = 50%, Minimum confidence = 75%
frequent_itemsets_1, rules_1 = run_apriori(min_support=0.5, min_confidence=0.75)
# Case b: Minimum support = 60%, Minimum confidence = 60%
frequent_itemsets_2, rules_2 = run_apriori(min_support=0.6, min_confidence=0.6)
# Display the results
print("Frequent Itemsets (Case a):")
print(frequent_itemsets_1)
print("\nAssociation Rules (Case a):")
print(rules_1)
print("\nFrequent Itemsets (Case b):")
print(frequent_itemsets_2)
print("\nAssociation Rules (Case b):")
print(rules_2)
No frequent itemsets found for min_support = 0.5
No frequent itemsets found for min_support = 0.6
Frequent Itemsets (Case a):
None
Association Rules (Case a):
None
Frequent Itemsets (Case b):
None
Association Rules (Case b):
None
/usr/local/lib/python3.10/dist-packages/ipykernel/[Link]: DeprecationWarning: `should_run_async` will not call `transform_c
and should_run_async(code)
[Link] 9/9