scikit-lab-kit 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/PKG-INFO +1 -1
  2. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/pyproject.toml +1 -1
  3. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/01_data_cleaning_missing_values.txt +0 -1
  4. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/03_data_cleaning_on_dataset.txt +2 -3
  5. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/05_eda.txt +2 -15
  6. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/06_decision_tree.txt +0 -9
  7. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/07_compare_classification_models.txt +1 -48
  8. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/08_classification_improvement.txt +0 -18
  9. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/09_clustering.txt +0 -9
  10. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/11_association_apriori_own_dataset.txt +0 -3
  11. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab_kit.egg-info/PKG-INFO +1 -1
  12. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/README.md +0 -0
  13. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/__init__.py +0 -0
  14. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/__main__.py +0 -0
  15. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/02_data_cleaning_noisy_data.txt +0 -0
  16. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/04_preprocessing_contingency.txt +0 -0
  17. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/10_association_apriori_groceries.txt +0 -0
  18. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/12_fp_tree_association.txt +0 -0
  19. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/13_classifiers_explanation.txt +0 -0
  20. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/practicals/14_pca.txt +0 -0
  21. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab/viewer.py +0 -0
  22. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab_kit.egg-info/SOURCES.txt +0 -0
  23. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab_kit.egg-info/dependency_links.txt +0 -0
  24. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab_kit.egg-info/entry_points.txt +0 -0
  25. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/scikit_lab_kit.egg-info/top_level.txt +0 -0
  26. {scikit_lab_kit-0.1.0 → scikit_lab_kit-0.1.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scikit-lab-kit
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Numbered DAML (scikit-learn) practicals: steps, code and short explanation
5
5
  Requires-Python: >=3.9
6
6
  Description-Content-Type: text/markdown
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scikit-lab-kit"
7
- version = "0.1.0"
7
+ version = "0.1.1"
8
8
  description = "Numbered DAML (scikit-learn) practicals: steps, code and short explanation"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -40,7 +40,6 @@ if 'Marks' in df.columns:
40
40
  print("\nDataFrame After Filling Missing Values:")
41
41
  print(df)
42
42
  if 'Marks' in df.columns and not df['Marks'].isna().all():
43
- # Min-Max Scaling
44
43
  scaler = MinMaxScaler()
45
44
  df['Marks_MINMAX'] = scaler.fit_transform(df[['Marks']])
46
45
  zscaler = StandardScaler()
@@ -18,10 +18,9 @@ df=pd.read_csv('/content/sample_data/loan_credit_dataset_240_rows_with_outliers.
18
18
  print(df.head())
19
19
  print(df.info())
20
20
  print(df.describe())
21
- print(df.isnull().sum()) #missing value
22
- print("Duplicate rows:",df.duplicated().sum()) #duplicate values
21
+ print(df.isnull().sum())
22
+ print("Duplicate rows:",df.duplicated().sum())
23
23
  print(df.dtypes)
24
- #finding outliers in data
25
24
  print("Before removing outliers:")
26
25
  columns = [
27
26
  "AnnualIncome",
@@ -43,7 +43,6 @@ venue_wins = (
43
43
  .size()
44
44
  .reset_index(name='Wins')
45
45
  )
46
- # Select top 10 venues by number of matches
47
46
  top_venues = df['Venue_Name'].value_counts().head(10).index
48
47
  venue_wins = venue_wins[venue_wins['Venue_Name'].isin(top_venues)]
49
48
  plt.figure(figsize=(14,7))
@@ -78,9 +77,7 @@ team_id_name_pairs = pd.concat([
78
77
  df[['Match_Winner_Id', 'OpponentTeam name']].rename(columns={'Match_Winner_Id': 'Team_ID', 'OpponentTeam name': 'Team_Name'})
79
78
  ]).drop_duplicates().dropna(subset=['Team_ID', 'Team_Name'])
80
79
  team_map = team_id_name_pairs.groupby('Team_ID')['Team_Name'].first()
81
- # Add winner name column
82
80
  df['Match_Winner_Id'] = df['Match_Winner_Id'].map(team_map)
83
- # Home wins
84
81
  home_wins = df[df['OpponentTeam name'] == df['Match_Winner_Id']]
85
82
  home_win_counts = home_wins['OpponentTeam name'].value_counts()
86
83
  plt.figure(figsize=(8,8))
@@ -98,21 +95,17 @@ venue_wins = (
98
95
  .size()
99
96
  .reset_index(name='Wins')
100
97
  )
101
- # Top 10 venues
102
98
  top_venues = df['Venue_Name'].value_counts().head(10).index
103
99
  venue_wins = venue_wins[venue_wins['Venue_Name'].isin(top_venues)]
104
- # Convert venue names to numeric positions
105
100
  venue_positions = {venue: i for i, venue in enumerate(top_venues)}
106
101
  venue_wins['Venue_Pos'] = venue_wins['Venue_Name'].map(venue_positions)
107
- # Convert 'Match_Winner_Id' (team names) to numerical categories for plotting
108
102
  venue_wins['Team_Numeric_ID'], unique_teams = pd.factorize(venue_wins['Match_Winner_Id'])
109
- # Scatter Plot
110
103
  plt.figure(figsize=(14,7))
111
104
  scatter = plt.scatter(
112
105
  venue_wins['Venue_Pos'],
113
106
  venue_wins['Wins'],
114
107
  s=120,
115
- c=venue_wins['Team_Numeric_ID'], # Use numerical IDs for coloring
108
+ c=venue_wins['Team_Numeric_ID'],
116
109
  cmap='tab20',
117
110
  alpha=0.8
118
111
  )
@@ -125,7 +118,6 @@ plt.xticks(
125
118
  plt.xlabel('Venue')
126
119
  plt.ylabel('Number of Wins')
127
120
  plt.title('Venue-wise Wins by Teams (Scatter Plot)')
128
- # Create a custom colorbar to show team names
129
121
  cbar = plt.colorbar(scatter, ticks=range(len(unique_teams)))
130
122
  cbar.set_ticklabels(unique_teams)
131
123
  cbar.set_label('Match Winner Team')
@@ -184,9 +176,7 @@ team_id_name_pairs = pd.concat([
184
176
  df[['Match_Winner_Id', 'OpponentTeam name']].rename(columns={'Match_Winner_Id': 'Team_ID', 'OpponentTeam name': 'Team_Name'})
185
177
  ]).drop_duplicates().dropna(subset=['Team_ID', 'Team_Name'])
186
178
  team_map = team_id_name_pairs.groupby('Team_ID')['Team_Name'].first()
187
- # Map winner IDs to team names
188
179
  df['Match_Winner_Name'] = df['Match_Winner_Id'].map(team_map)
189
- # Home wins
190
180
  home_wins = df[df['OpponentTeam name'] == df['Match_Winner_Name']]
191
181
  home_win_counts = home_wins['OpponentTeam name'].value_counts()
192
182
  plt.figure(figsize=(9,9))
@@ -213,14 +203,13 @@ plt.show()
213
203
  df['Venue_Code'] = df['Venue_Name'].astype('category').cat.codes
214
204
  df['Toss_Code'] = df['Toss_Decision'].astype('category').cat.codes
215
205
  df['WinType_Code'] = df['Win_Type'].astype('category').cat.codes
216
- # Create 3D Scatter Plot
217
206
  fig = plt.figure(figsize=(12,8))
218
207
  ax = fig.add_subplot(111, projection='3d')
219
208
  scatter = ax.scatter(
220
209
  df['Venue_Code'],
221
210
  df['Toss_Code'],
222
211
  df['WinType_Code'],
223
- c=df['WinType_Code'], # Color by Win Type
212
+ c=df['WinType_Code'],
224
213
  cmap='viridis',
225
214
  s=40,
226
215
  alpha=0.7
@@ -229,11 +218,9 @@ ax.set_title('3D Scatter Plot of Venue, Toss Decision and Win Type')
229
218
  ax.set_xlabel('Venue')
230
219
  ax.set_ylabel('Toss Decision')
231
220
  ax.set_zlabel('Win Type')
232
- # Show original category names on axes
233
221
  venue_labels = df['Venue_Name'].astype('category').cat.categories
234
222
  toss_labels = df['Toss_Decision'].astype('category').cat.categories
235
223
  win_labels = df['Win_Type'].astype('category').cat.categories
236
- # Limit x-axis labels to avoid clutter (top 10 venues)
237
224
  ax.set_xticks(range(min(10, len(venue_labels))))
238
225
  ax.set_xticklabels(venue_labels[:10], rotation=45, ha='right')
239
226
  ax.set_yticks(range(len(toss_labels)))
@@ -19,19 +19,14 @@ import matplotlib.pyplot as plt
19
19
  import numpy as np
20
20
  filepath = "C:/Users/Admin/Downloads/diabetes.csv"
21
21
  data = pd.read_csv(filepath)
22
- # Binarize the outcome
23
22
  data['Outcome'] = data['Outcome'].apply(lambda x: 1 if x >= 0.5 else 0)
24
23
  X = data.drop(columns=['Outcome'])
25
24
  y = data['Outcome']
26
- # Split the dataset
27
25
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
28
- # Initialize and train the model
29
26
  clf = DecisionTreeClassifier(random_state=42)
30
27
  clf.fit(X_train, y_train)
31
- # Get predictions and probabilities
32
28
  y_pred = clf.predict(X_test)
33
29
  y_pred_prob = clf.predict_proba(X_test)[:, 1]
34
- # Calculate metrics
35
30
  accuracy = accuracy_score(y_test, y_pred)
36
31
  conf_matrix = confusion_matrix(y_test, y_pred)
37
32
  precision = precision_score(y_test, y_pred)
@@ -40,10 +35,8 @@ TN = conf_matrix[0, 0]
40
35
  FP = conf_matrix[0, 1]
41
36
  specificity = TN / (TN + FP)
42
37
  standard_error = np.sqrt((accuracy * (1 - accuracy)) / len(y_test))
43
- # ROC and AUC
44
38
  fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
45
39
  roc_auc = auc(fpr, tpr)
46
- # Plot ROC Curve
47
40
  plt.figure()
48
41
  plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
49
42
  plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
@@ -52,12 +45,10 @@ plt.ylabel('True Positive Rate')
52
45
  plt.title('Receiver Operating Characteristic')
53
46
  plt.legend(loc="lower right")
54
47
  plt.show()
55
- # Plot Decision Tree
56
48
  plt.figure(figsize=(12, 8))
57
49
  plot_tree(clf, feature_names=X.columns, class_names=['0', '1'], filled=True, max_depth=3)
58
50
  plt.title("Decision Tree (Max Depth = 3)")
59
51
  plt.show()
60
- # Print Results
61
52
  print("Predictions:", y_pred)
62
53
  print("Accuracy:", accuracy)
63
54
  print("Confusion Matrix:\n", conf_matrix)
@@ -76,20 +76,15 @@ import matplotlib.pyplot as plt
76
76
  import numpy as np
77
77
  filepath = "C:/Users/Admin/Downloads/pulsar_stars.csv"
78
78
  data = pd.read_csv(filepath)
79
- # Binarize the target_class
80
79
  data['target_class'] = data['target_class'].apply(lambda x: 1 if x >= 0.5 else 0)
81
80
  X = data.drop(columns=['target_class'])
82
81
  y = data['target_class']
83
- # Split the dataset
84
82
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
85
83
  random_state=42)
86
- # Initialize and train the model
87
84
  clf = DecisionTreeClassifier(random_state=42)
88
85
  clf.fit(X_train, y_train)
89
- # Get predictions and probabilities
90
86
  y_pred = clf.predict(X_test)
91
87
  y_pred_prob = clf.predict_proba(X_test)[:, 1]
92
- # Calculate metrics
93
88
  accuracy = accuracy_score(y_test, y_pred)
94
89
  conf_matrix = confusion_matrix(y_test, y_pred)
95
90
  precision = precision_score(y_test, y_pred)
@@ -98,10 +93,8 @@ TN = conf_matrix[0, 0]
98
93
  FP = conf_matrix[0, 1]
99
94
  specificity = TN / (TN + FP)
100
95
  standard_error = np.sqrt((accuracy * (1 - accuracy)) / len(y_test))
101
- # ROC and AUC
102
96
  fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
103
97
  roc_auc = auc(fpr, tpr)
104
- # Plot ROC Curve
105
98
  plt.figure()
106
99
  plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)'% roc_auc)
107
100
  plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
@@ -110,13 +103,11 @@ plt.ylabel('True Positive Rate')
110
103
  plt.title('Receiver Operating Characteristic')
111
104
  plt.legend(loc="lower right")
112
105
  plt.show()
113
- # Plot Decision Tree
114
106
  plt.figure(figsize=(12, 8))
115
107
  plot_tree(clf, feature_names=X.columns, class_names=['0', '1'], filled=True,
116
108
  max_depth=3)
117
109
  plt.title("Decision Tree (Max Depth = 3)")
118
110
  plt.show()
119
- # Print Results
120
111
  print("Predictions:", y_pred)
121
112
  print("Accuracy:", accuracy)
122
113
  print("Confusion Matrix:\n", conf_matrix)
@@ -137,7 +128,6 @@ EXPLANATION:
137
128
  3. NAIVE BAYES
138
129
 
139
130
  CODE:
140
- #Naive Bayes
141
131
  import pandas as pd
142
132
  from sklearn.model_selection import train_test_split
143
133
  from sklearn.preprocessing import StandardScaler
@@ -152,41 +142,28 @@ from sklearn.metrics import (
152
142
  )
153
143
  import matplotlib.pyplot as plt
154
144
  import numpy as np
155
- # Load the dataset
156
145
  file_path = "C:/Users/Admin/Downloads/pulsar_stars.csv"
157
146
  data = pd.read_csv(file_path)
158
- # Convert target_class to binary if it has continuous values
159
147
  data['target_class'] = data['target_class'].apply(lambda x: 1 if x >= 0.5 else 0)
160
- # Define features and target variable
161
148
  X = data.drop(columns=['target_class'])
162
149
  y = data['target_class']
163
- # Standardize the features
164
150
  scaler = StandardScaler()
165
151
  X_scaled = scaler.fit_transform(X)
166
- # Split the dataset (20% for test and 80% for train)
167
152
  X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
168
- # Create a Naive Bayes Classifier
169
153
  nb_model = GaussianNB()
170
- # Train the model
171
154
  nb_model.fit(X_train, y_train)
172
- # Make predictions
173
155
  y_pred = nb_model.predict(X_test)
174
156
  y_pred_prob = nb_model.predict_proba(X_test)[:, 1]
175
- # Calculate metrics
176
157
  accuracy = accuracy_score(y_test, y_pred)
177
158
  conf_matrix = confusion_matrix(y_test, y_pred)
178
159
  precision = precision_score(y_test, y_pred)
179
160
  sensitivity = recall_score(y_test, y_pred)
180
- # Specificity calculation
181
161
  TN = conf_matrix[0, 0]
182
162
  FP = conf_matrix[0, 1]
183
163
  specificity = TN / (TN + FP)
184
- # Standard error
185
164
  standard_error = np.sqrt((accuracy * (1 - accuracy)) / len(y_test))
186
- # Calculate ROC curve and AUC
187
165
  fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
188
166
  roc_auc = auc(fpr, tpr)
189
- # Plot ROC curve
190
167
  plt.figure()
191
168
  plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
192
169
  plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
@@ -195,7 +172,6 @@ plt.ylabel('True Positive Rate')
195
172
  plt.title('Receiver Operating Characteristic - Naive Bayes')
196
173
  plt.legend(loc="lower right")
197
174
  plt.show()
198
- # Print results
199
175
  print("Predictions:", y_pred)
200
176
  print("Accuracy:", accuracy)
201
177
  print("Confusion Matrix:\n", conf_matrix)
@@ -224,28 +200,20 @@ from sklearn.linear_model import LogisticRegression
224
200
  from sklearn.metrics import accuracy_score
225
201
  import matplotlib.pyplot as plt
226
202
  import numpy as np
227
- # Load the dataset
228
203
  file_path = "C:/Users/Admin/Downloads/pulsar_stars.csv"
229
204
  data = pd.read_csv(file_path)
230
- # Convert target_class to binary if it has continuous values
231
205
  data['target_class'] = data['target_class'].apply(lambda x: 1 if x >= 0.5 else 0)
232
- # Define features and target variable
233
206
  X = data.drop(columns=['target_class'])
234
207
  y = data['target_class']
235
- # Standardize the features
236
208
  scaler = StandardScaler()
237
209
  X_scaled = scaler.fit_transform(X)
238
- # Apply PCA to reduce dimensionality
239
210
  n_components = 7
240
211
  pca = PCA(n_components=n_components)
241
212
  X_pca = pca.fit_transform(X_scaled)
242
- # Explained variance ratio for PCA
243
213
  explained_variance_ratio = pca.explained_variance_ratio_
244
214
  total_variance_explained = np.sum(explained_variance_ratio)
245
- # Print PCA results
246
215
  print("Explained Variance Ratio:", explained_variance_ratio)
247
216
  print("Total Variance Explained:", total_variance_explained)
248
- # Visualize the explained variance ratio
249
217
  plt.figure(figsize=(8, 5))
250
218
  plt.bar(range(1, n_components + 1), explained_variance_ratio, alpha=0.7, align='center', label='Individual Explained Variance')
251
219
  plt.step(range(1, n_components + 1), np.cumsum(explained_variance_ratio), where='mid', label='Cumulative Explained Variance')
@@ -254,16 +222,11 @@ plt.ylabel('Explained Variance Ratio')
254
222
  plt.title('PCA Explained Variance')
255
223
  plt.legend(loc='best')
256
224
  plt.show()
257
- # Split the dataset using PCA features
258
225
  X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
259
- # Train a Logistic Regression model
260
226
  model = LogisticRegression()
261
227
  model.fit(X_train, y_train)
262
- # Make predictions
263
228
  y_pred = model.predict(X_test)
264
- # Calculate accuracy
265
229
  accuracy = accuracy_score(y_test, y_pred)
266
- # Print accuracy
267
230
  print("Accuracy:", accuracy)
268
231
 
269
232
  EXPLANATION:
@@ -301,14 +264,11 @@ svm_model = SVC(
301
264
  random_state=42
302
265
  )
303
266
  svm_model.fit(X_train_scaled, y_train)
304
- # Make Predictions
305
267
  y_pred = svm_model.predict(X_test_scaled)
306
268
  y_prob = svm_model.predict_proba(X_test_scaled)[:, 1]
307
- # Evaluate the Model
308
269
  print("--- Confusion Matrix ---")
309
270
  print(confusion_matrix(y_test, y_pred))
310
271
  print("\n--- Classification Report ---")
311
- # Focus heavily on Precision and Recall for Class 1 (Pulsars)
312
272
  print(classification_report(y_test, y_pred))
313
273
  print(f"ROC-AUC Score: {roc_auc_score(y_test, y_prob):.4f}")
314
274
 
@@ -333,16 +293,12 @@ df = pd.read_csv('C:/Users/Admin/Downloads/pulsar_stars.csv')
333
293
  df.columns = df.columns.str.strip()
334
294
  X = df.drop(columns=['target_class'])
335
295
  y = df['target_class']
336
- # Split into training and testing sets (Stratified to maintain class balance)
337
296
  X_train, X_test, y_train, y_test = train_test_split(
338
297
  X, y, test_size=0.3, random_state=42, stratify=y
339
298
  )
340
- # Feature Scaling
341
299
  scaler = StandardScaler()
342
300
  X_train_scaled = scaler.fit_transform(X_train)
343
301
  X_test_scaled = scaler.transform(X_test)
344
- # Initialize and Train Random Forest
345
- # class_weight='balanced' penalizes mistakes on the rare pulsar class heavily.
346
302
  rf_model = RandomForestClassifier(
347
303
  n_estimators=100,
348
304
  class_weight='balanced',
@@ -350,16 +306,13 @@ rf_model = RandomForestClassifier(
350
306
  n_jobs=-1
351
307
  )
352
308
  rf_model.fit(X_train_scaled, y_train)
353
- # Make Predictions
354
309
  y_pred = rf_model.predict(X_test_scaled)
355
- y_prob = rf_model.predict_proba(X_test_scaled)[:, 1] # Probability estimates for ROC-AUC
356
- # Evaluate the Model
310
+ y_prob = rf_model.predict_proba(X_test_scaled)[:, 1]
357
311
  print("--- Confusion Matrix ---")
358
312
  print(confusion_matrix(y_test, y_pred))
359
313
  print("\n--- Classification Report ---")
360
314
  print(classification_report(y_test, y_pred))
361
315
  print(f"ROC-AUC Score: {roc_auc_score(y_test, y_prob):.4f}")
362
- # Feature Importance
363
316
  importances = pd.Series(rf_model.feature_importances_, index=X.columns)
364
317
  print("\n--- Feature Importances ---")
365
318
  print(importances.sort_values(ascending=False))
@@ -48,42 +48,31 @@ def main():
48
48
  df[col] = pd.to_numeric(df[col], errors='coerce')
49
49
  df[col] = df[col].fillna(df[col].median())
50
50
  feature_cols.append(col)
51
- # Encode categorical features if available (e.g., 'brand')
52
51
  if 'brand' in df.columns:
53
52
  df = pd.get_dummies(df, columns=['brand'], drop_first=True)
54
- # Add encoded brand columns to features list
55
53
  brand_cols = [c for c in df.columns if c.startswith('brand_')]
56
54
  feature_cols.extend(brand_cols)
57
- # Target Encoding (Convert categorical classes to numeric labels)
58
55
  label_enc = LabelEncoder()
59
56
  y = label_enc.fit_transform(df[target_col].astype(str))
60
- # Feature matrix (Ensure strictly numeric data types)
61
57
  X = df[feature_cols].select_dtypes(include=[np.number])
62
- # Exploratory Data Analysis - Heatmap
63
58
  plt.figure(figsize=(12, 8))
64
59
  sns.heatmap(X.corr(numeric_only=True), annot=False, cmap='coolwarm')
65
60
  plt.title("EV Features Correlation Heatmap")
66
61
  plt.tight_layout()
67
62
  plt.show()
68
- # Scaling
69
63
  standard_scaler = StandardScaler()
70
64
  minmax_scaler = MinMaxScaler()
71
65
  X_std = standard_scaler.fit_transform(X)
72
66
  X_mm = minmax_scaler.fit_transform(X)
73
- # Train / Test Split
74
67
  X_train_std, X_test_std, y_train, y_test = train_test_split(
75
68
  X_std, y, test_size=0.2, random_state=69, stratify=y
76
69
  )
77
- # Evaluation Function
78
70
  def evaluate_model(y_true, y_pred, y_prob):
79
71
  acc = accuracy_score(y_true, y_pred)
80
72
  cm = confusion_matrix(y_true, y_pred)
81
- # Weighted precision and recall for multiclass/binary
82
73
  prec = precision_score(y_true, y_pred, average='weighted', zero_division=0)
83
74
  sens = recall_score(y_true, y_pred, average='weighted', zero_division=0)
84
- # Standard Error of Accuracy
85
75
  se = np.sqrt((acc * (1 - acc)) / len(y_true))
86
- # Multiclass/binary AUC calculation
87
76
  try:
88
77
  n_classes = len(np.unique(y_true))
89
78
  if n_classes == 2:
@@ -100,7 +89,6 @@ def main():
100
89
  'AUC': model_auc,
101
90
  'Confusion Matrix': cm
102
91
  }
103
- # Model Initialization
104
92
  dt_bag = BaggingClassifier(
105
93
  estimator=DecisionTreeClassifier(random_state=69),
106
94
  n_estimators=50,
@@ -117,27 +105,22 @@ def main():
117
105
  n_estimators=50,
118
106
  random_state=69
119
107
  )
120
- # Train Models
121
108
  dt_bag.fit(X_train_std, y_train)
122
109
  nb_bag.fit(X_train_std, y_train)
123
110
  log_reg.fit(X_train_std, y_train)
124
111
  dt_ada.fit(X_train_std, y_train)
125
- # Predictions
126
112
  pred_dt_bag = dt_bag.predict(X_test_std)
127
113
  pred_nb_bag = nb_bag.predict(X_test_std)
128
114
  pred_log_reg = log_reg.predict(X_test_std)
129
115
  pred_dt_ada = dt_ada.predict(X_test_std)
130
- # Results Compilation
131
116
  results = {
132
117
  'Bagging DT': evaluate_model(y_test, pred_dt_bag, dt_bag.predict_proba(X_test_std)),
133
118
  'Bagging NB': evaluate_model(y_test, pred_nb_bag, nb_bag.predict_proba(X_test_std)),
134
119
  'Logistic Regression': evaluate_model(y_test, pred_log_reg, log_reg.predict_proba(X_test_std)),
135
120
  'AdaBoost DT': evaluate_model(y_test, pred_dt_ada, dt_ada.predict_proba(X_test_std))
136
121
  }
137
- # Cross-Validation Score
138
122
  cv = KFold(n_splits=5, shuffle=True, random_state=69)
139
123
  dt_cv_std = np.mean(cross_val_score(dt_bag, X_std, y, cv=cv, scoring='accuracy'))
140
- # Print Summary Table
141
124
  summary_df = pd.DataFrame(results).T.drop(columns=['Confusion Matrix'])
142
125
  print("\n" + "="*65)
143
126
  print(" EV DATASET MODEL EVALUATION SUMMARY (STANDARDIZED)")
@@ -146,7 +129,6 @@ def main():
146
129
  print("-" * 65)
147
130
  print(f"Bagging DT 5-Fold Cross-Validation Accuracy: {dt_cv_std:.4f}")
148
131
  print("="*65 + "\n")
149
- # Display Confusion Matrices with Class Names
150
132
  print("--- Detailed Confusion Matrices ---")
151
133
  for name, res in results.items():
152
134
  print(f"\n{name} Confusion Matrix:")
@@ -22,14 +22,12 @@ from sklearn.datasets import load_wine
22
22
  from sklearn.decomposition import PCA
23
23
  from sklearn.metrics import silhouette_score
24
24
  from sklearn.preprocessing import StandardScaler
25
- # 1. Load the Wine Dataset
26
25
  wine = load_wine()
27
26
  wine_df = pd.DataFrame(data=wine.data, columns=wine.feature_names)
28
27
  print("Data Summary:")
29
28
  print(wine_df.describe())
30
29
  scaler = StandardScaler()
31
30
  X_scaled = scaler.fit_transform(wine_df)
32
- # 3. Elbow Method to Find Optimal K
33
31
  wss = []
34
32
  for k in range(1, 11):
35
33
  kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
@@ -41,15 +39,12 @@ plt.xlabel("Number of clusters k")
41
39
  plt.ylabel("Total within-cluster sum of squares (Inertia)")
42
40
  plt.title("Elbow Method for Optimal K")
43
41
  plt.show()
44
- # 4. K-Means Clustering (Using k=3 since Wine has 3 natural classes)
45
42
  kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
46
43
  wine_df["kmeans_cluster"] = kmeans.fit_predict(X_scaled)
47
- # Calculate Silhouette Score for K-Means
48
44
  silhouette_avg_kmeans = silhouette_score(X_scaled, wine_df["kmeans_cluster"])
49
45
  print(
50
46
  f"\nMean Silhouette Width for K-Means Clustering: {silhouette_avg_kmeans:.4f}"
51
47
  )
52
- # 5. Visualize K-Means with PCA (2D Projection)
53
48
  pca = PCA(n_components=2)
54
49
  X_pca = pca.fit_transform(X_scaled)
55
50
  plt.figure(figsize=(8, 5))
@@ -60,7 +55,6 @@ plt.title("K-Means Clustering (PCA Reduced)")
60
55
  plt.xlabel("Principal Component 1")
61
56
  plt.ylabel("Principal Component 2")
62
57
  plt.show()
63
- # 6. Hierarchical Clustering - Dendrogram
64
58
  linkage_matrix = linkage(X_scaled, method="ward")
65
59
  plt.figure(figsize=(10, 5))
66
60
  dendrogram(linkage_matrix)
@@ -68,17 +62,14 @@ plt.title("Hierarchical Clustering Dendrogram")
68
62
  plt.xlabel("Sample Index")
69
63
  plt.ylabel("Distance")
70
64
  plt.show()
71
- # 7. Agglomerative Clustering
72
65
  agglomerative = AgglomerativeClustering(n_clusters=3)
73
66
  wine_df["hierarchical_cluster"] = agglomerative.fit_predict(X_scaled)
74
- # Calculate Silhouette Score for Hierarchical Clustering
75
67
  silhouette_avg_hierarchical = silhouette_score(
76
68
  X_scaled, wine_df["hierarchical_cluster"]
77
69
  )
78
70
  print(
79
71
  f"Mean Silhouette Width for Hierarchical Clustering: {silhouette_avg_hierarchical:.4f}"
80
72
  )
81
- # 8. Visualize Hierarchical Clusters (e.g., Alcohol vs. Malic Acid)
82
73
  plt.figure(figsize=(8, 5))
83
74
  plt.scatter(
84
75
  wine_df["alcohol"],
@@ -56,7 +56,6 @@ df = pd.DataFrame(te_ary, columns=te.columns_)
56
56
  print("Encoded Dataset Shape:", df.shape)
57
57
  frequent_itemsets = apriori(df, min_support=0.05, use_colnames=True)
58
58
  print(f"Total Frequent Itemsets Generated: {len(frequent_itemsets)}")
59
- # Plot Top Itemsets
60
59
  plt.figure(figsize=(12, 6))
61
60
  top_itemsets = frequent_itemsets.nlargest(n=15, columns="support").copy()
62
61
  top_itemsets["itemsets_str"] = top_itemsets["itemsets"].apply(
@@ -74,12 +73,10 @@ plt.xticks(rotation=45, ha="right")
74
73
  plt.title("Top 15 Itemsets by Support")
75
74
  plt.tight_layout()
76
75
  plt.show()
77
- # Generate and Filter Association Rules
78
76
  rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.0)
79
77
  if not rules.empty:
80
78
  rules["antecedent_len"] = rules["antecedents"].apply(len)
81
79
  rules["consequent_len"] = rules["consequents"].apply(len)
82
- # Convert frozensets to readable strings for display
83
80
  display_rules = rules.copy()
84
81
  display_rules["antecedents"] = display_rules["antecedents"].apply(
85
82
  lambda x: list(x)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scikit-lab-kit
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Numbered DAML (scikit-learn) practicals: steps, code and short explanation
5
5
  Requires-Python: >=3.9
6
6
  Description-Content-Type: text/markdown
File without changes
File without changes