scikit-tools 0.1.0__tar.gz → 0.1.2__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.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scikit-tools
3
- Version: 0.1.0
4
- Summary: Clean, streamlined Data Science and Machine Learning toolkit with direct topic names
3
+ Version: 0.1.2
4
+ Summary: Clean, dependency-managed toolkit with raw script segments and dataset handling options
5
5
  Author-email: Your Name <your.email@example.com>
6
6
  Classifier: Programming Language :: Python :: 3
7
7
  Classifier: License :: OSI Approved :: MIT License
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scikit-tools"
7
- version = "0.1.0"
8
- description = "Clean, streamlined Data Science and Machine Learning toolkit with direct topic names"
7
+ version = "0.1.2"
8
+ description = "Clean, dependency-managed toolkit with raw script segments and dataset handling options"
9
9
  authors = [
10
10
  { name="Your Name", email="your.email@example.com" }
11
11
  ]
@@ -0,0 +1,341 @@
1
+ # scikit-tools solutions library containing raw topic scripts
2
+
3
+ DATASET_SETUP_CODE = """
4
+ import os
5
+ import pandas as pd
6
+
7
+ # --- METHOD 1: Direct URL Download & Format (Default) ---
8
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
9
+ print("Fetching dataset from URL...")
10
+ df = pd.read_csv(url)
11
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
12
+ df['Species'] = df['Species'].map(lambda x: f\"Iris-{x}\")
13
+ df.to_csv('iris.csv', index=False)
14
+
15
+ # --- METHOD 2: Colab File Upload (Alternative for custom/manual uploads) ---
16
+ # from google.colab import files
17
+ # uploaded = files.upload()
18
+ # df = pd.read_csv('iris.csv')
19
+
20
+ # --- METHOD 3: Standard Local Check ---
21
+ # if os.path.exists('iris.csv'):
22
+ # df = pd.read_csv('iris.csv')
23
+ """
24
+ """
25
+ This library contains the raw code scripts for all 13 Data Science topics.
26
+ """
27
+
28
+ # Topic 1: Basic numerical operations and statistics using NumPy.
29
+ numpy_stats_script = """
30
+ import os
31
+ import pandas as pd
32
+ import numpy as np
33
+
34
+ # Dataset Download and Setup
35
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
36
+ df = pd.read_csv(url)
37
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
38
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
39
+ df.to_csv('iris.csv', index=False)
40
+
41
+ # Alternatively using Colab uploads:
42
+ # from google.colab import files
43
+ # uploaded = files.upload()
44
+
45
+ # Load formatted data
46
+ df = pd.read_csv('iris.csv')
47
+ sepal_length = df['SepalLengthCm'].to_numpy()
48
+
49
+ total_sum = np.sum(sepal_length)
50
+ mean_val = np.mean(sepal_length)
51
+ max_val = np.max(sepal_length)
52
+ min_val = np.min(sepal_length)
53
+ first_five = sepal_length[:5]
54
+
55
+ print(f"Sum: {total_sum:.2f}")
56
+ print(f"Mean: {mean_val:.2f}")
57
+ print(f"Maximum: {max_val:.2f}")
58
+ print(f"Minimum: {min_val:.2f}")
59
+ print(f"First five values: {first_five}")
60
+ """
61
+
62
+ # Topic 2: Load DataFrame and inspect its structure, columns, and shape.
63
+ inspect_dataset_script = """
64
+ import os
65
+ import pandas as pd
66
+
67
+ # Dataset Download and Setup
68
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
69
+ df = pd.read_csv(url)
70
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
71
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
72
+ df.to_csv('iris.csv', index=False)
73
+
74
+ # Load formatted data
75
+ df = pd.read_csv('iris.csv')
76
+
77
+ print("--- First 5 Records ---")
78
+ print(df.head())
79
+ print(f"\nNumber of rows: {df.shape[0]}, Number of columns: {df.shape[1]}")
80
+ print("\n--- Column Names & Data Types ---")
81
+ print(df.dtypes)
82
+ print("\n--- Summary Statistics ---")
83
+ print(df.describe())
84
+ """
85
+
86
+ # Topic 3: Introduce mock missing values and handle them using median and mode.
87
+ handle_missing_values_script = """
88
+ import os
89
+ import pandas as pd
90
+ import numpy as np
91
+
92
+ # Dataset Download and Setup
93
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
94
+ df = pd.read_csv(url)
95
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
96
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
97
+ df.to_csv('iris.csv', index=False)
98
+
99
+ df_copy = pd.read_csv('iris.csv').copy()
100
+ df_copy.loc[[5, 10, 15], 'SepalLengthCm'] = np.nan
101
+ df_copy.loc[[20, 25], 'Species'] = np.nan
102
+
103
+ median_sepal = df_copy['SepalLengthCm'].median()
104
+ df_copy['SepalLengthCm'] = df_copy['SepalLengthCm'].fillna(median_sepal)
105
+ mode_species = df_copy['Species'].mode()[0]
106
+ df_copy['Species'] = df_copy['Species'].fillna(mode_species)
107
+ print(df_copy.isnull().sum())
108
+ """
109
+
110
+ # Topic 4: Perform one-hot encoding and standardization (scaling).
111
+ data_transformation_script = """
112
+ import os
113
+ import pandas as pd
114
+ from sklearn.preprocessing import StandardScaler
115
+
116
+ # Dataset Download and Setup
117
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
118
+ df = pd.read_csv(url)
119
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
120
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
121
+ df.to_csv('iris.csv', index=False)
122
+
123
+ df = pd.read_csv('iris.csv')
124
+ df_encoded = pd.get_dummies(df, columns=['Species'])
125
+ num_cols = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
126
+ scaler = StandardScaler()
127
+ df_encoded[num_cols] = scaler.fit_transform(df_encoded[num_cols])
128
+ print(df_encoded.head())
129
+ """
130
+
131
+ # Topic 5: Generate and render Histogram and Scatter Plots for EDA.
132
+ eda_plots_script = """
133
+ import os
134
+ import pandas as pd
135
+ import matplotlib.pyplot as plt
136
+ import seaborn as sns
137
+
138
+ # Dataset Download and Setup
139
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
140
+ df = pd.read_csv(url)
141
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
142
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
143
+ df.to_csv('iris.csv', index=False)
144
+
145
+ df = pd.read_csv('iris.csv')
146
+ fig, axes = plt.subplots(1, 2, figsize=(14, 5))
147
+ sns.histplot(data=df, x='PetalLengthCm', kde=True, color='skyblue', ax=axes[0])
148
+ axes[0].set_title('Histogram of PetalLengthCm')
149
+ sns.scatterplot(data=df, x='SepalLengthCm', y='PetalLengthCm', hue='Species', palette='Set1', ax=axes[1])
150
+ axes[1].set_title('SepalLengthCm vs PetalLengthCm')
151
+ plt.tight_layout()
152
+ plt.show()
153
+ """
154
+
155
+ # Topic 6: Quantify outliers using Interquartile Range (IQR) bounds.
156
+ outlier_detection_script = """
157
+ import os
158
+ import pandas as pd
159
+
160
+ # Dataset Download and Setup
161
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
162
+ df = pd.read_csv(url)
163
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
164
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
165
+ df.to_csv('iris.csv', index=False)
166
+
167
+ df = pd.read_csv('iris.csv')
168
+ Q1 = df['SepalWidthCm'].quantile(0.25)
169
+ Q3 = df['SepalWidthCm'].quantile(0.75)
170
+ IQR = Q3 - Q1
171
+ lower_bound = Q1 - 1.5 * IQR
172
+ upper_bound = Q3 + 1.5 * IQR
173
+ outliers = df[(df['SepalWidthCm'] < lower_bound) | (df['SepalWidthCm'] > upper_bound)]
174
+ print(f"IQR: {IQR:.2f}")
175
+ print(f"Limits: {lower_bound:.2f} to {upper_bound:.2f}")
176
+ print(outliers[['SepalWidthCm', 'Species']])
177
+ """
178
+
179
+ # Topic 7: Calculate detailed descriptive metrics.
180
+ descriptive_stats_script = """
181
+ import os
182
+ import pandas as pd
183
+
184
+ # Dataset Download and Setup
185
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
186
+ df = pd.read_csv(url)
187
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
188
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
189
+ df.to_csv('iris.csv', index=False)
190
+
191
+ df = pd.read_csv('iris.csv')
192
+ attributes = ['SepalLengthCm', 'PetalLengthCm']
193
+ stats_summary = pd.DataFrame({
194
+ 'Mean': df[attributes].mean(),
195
+ 'Median': df[attributes].median(),
196
+ 'Sample Variance': df[attributes].var(),
197
+ 'Standard Deviation': df[attributes].std()
198
+ })
199
+ print(stats_summary)
200
+ """
201
+
202
+ # Topic 8: Evaluate covariance and Pearson correlation coefficient.
203
+ correlation_covariance_script = """
204
+ import os
205
+ import pandas as pd
206
+
207
+ # Dataset Download and Setup
208
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
209
+ df = pd.read_csv(url)
210
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
211
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
212
+ df.to_csv('iris.csv', index=False)
213
+
214
+ df = pd.read_csv('iris.csv')
215
+ covariance_val = df['PetalLengthCm'].cov(df['PetalWidthCm'])
216
+ correlation_val = df['PetalLengthCm'].corr(df['PetalWidthCm'])
217
+ print(f"Covariance: {covariance_val:.4f}")
218
+ print(f"Correlation Coefficient: {correlation_val:.4f}")
219
+ """
220
+
221
+ # Topic 9: Run an independent T-test on SepalLengthCm between Setosa and Versicolor.
222
+ hypothesis_testing_script = """
223
+ import os
224
+ import pandas as pd
225
+ from scipy import stats
226
+
227
+ # Dataset Download and Setup
228
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
229
+ df = pd.read_csv(url)
230
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
231
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
232
+ df.to_csv('iris.csv', index=False)
233
+
234
+ df = pd.read_csv('iris.csv')
235
+ setosa_sepal = df[df['Species'] == 'Iris-setosa']['SepalLengthCm']
236
+ versicolor_sepal = df[df['Species'] == 'Iris-versicolor']['SepalLengthCm']
237
+ t_stat, p_val = stats.ttest_ind(setosa_sepal, versicolor_sepal, equal_var=False)
238
+ print(f"T-statistic: {t_stat:.4f}")
239
+ print(f"P-value: {p_val:.4e}")
240
+ """
241
+
242
+ # Topic 10: Linear Regression predicting PetalLengthCm.
243
+ linear_regression_script = """
244
+ import os
245
+ import pandas as pd
246
+ from sklearn.model_selection import train_test_split
247
+ from sklearn.linear_model import LinearRegression
248
+
249
+ # Dataset Download and Setup
250
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
251
+ df = pd.read_csv(url)
252
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
253
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
254
+ df.to_csv('iris.csv', index=False)
255
+
256
+ df = pd.read_csv('iris.csv')
257
+ X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalWidthCm']]
258
+ y = df['PetalLengthCm']
259
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
260
+ model = LinearRegression()
261
+ model.fit(X_train, y_train)
262
+ predictions = model.predict(X_test)
263
+ print("Model Coefficients:", model.coef_)
264
+ print("Intercept:", model.intercept_)
265
+ """
266
+
267
+ # Topic 11: Multi-class Logistic Regression for classification.
268
+ logistic_regression_script = """
269
+ import os
270
+ import pandas as pd
271
+ from sklearn.model_selection import train_test_split
272
+ from sklearn.linear_model import LogisticRegression
273
+ from sklearn.metrics import accuracy_score, confusion_matrix
274
+
275
+ # Dataset Download and Setup
276
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
277
+ df = pd.read_csv(url)
278
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
279
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
280
+ df.to_csv('iris.csv', index=False)
281
+
282
+ df = pd.read_csv('iris.csv')
283
+ X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
284
+ y = df['Species']
285
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
286
+ clf = LogisticRegression(max_iter=200)
287
+ clf.fit(X_train, y_train)
288
+ y_pred = clf.predict(X_test)
289
+ print(f"Accuracy: {accuracy_score(y_test, y_pred):.4f}")
290
+ print(confusion_matrix(y_test, y_pred))
291
+ """
292
+
293
+ # Topic 12: Cluster attributes unsupervised using K-Means.
294
+ kmeans_clustering_script = """
295
+ import os
296
+ import pandas as pd
297
+ from sklearn.cluster import KMeans
298
+
299
+ # Dataset Download and Setup
300
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
301
+ df = pd.read_csv(url)
302
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
303
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
304
+ df.to_csv('iris.csv', index=False)
305
+
306
+ df = pd.read_csv('iris.csv')
307
+ X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
308
+ kmeans = KMeans(n_clusters=3, random_state=42, n_init='auto')
309
+ df['Cluster'] = kmeans.fit_predict(X)
310
+ print(df.head())
311
+ """
312
+
313
+ # Topic 13: Standardize, compress with 2-Component PCA, and classify.
314
+ pca_analysis_script = """
315
+ import os
316
+ import pandas as pd
317
+ from sklearn.preprocessing import StandardScaler
318
+ from sklearn.decomposition import PCA
319
+ from sklearn.model_selection import train_test_split
320
+ from sklearn.linear_model import LogisticRegression
321
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
322
+
323
+ # Dataset Download and Setup
324
+ url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
325
+ df = pd.read_csv(url)
326
+ df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
327
+ df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
328
+ df.to_csv('iris.csv', index=False)
329
+
330
+ df = pd.read_csv('iris.csv')
331
+ X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
332
+ y = df['Species']
333
+ X_scaled = StandardScaler().fit_transform(X)
334
+ pca = PCA(n_components=2)
335
+ X_pca = pca.fit_transform(X_scaled)
336
+ X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
337
+ clf = LogisticRegression()
338
+ clf.fit(X_train, y_train)
339
+ y_pred = clf.predict(X_test)
340
+ print(f"Accuracy: {accuracy_score(y_test, y_pred):.4f}")
341
+ """
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scikit-tools
3
- Version: 0.1.0
4
- Summary: Clean, streamlined Data Science and Machine Learning toolkit with direct topic names
3
+ Version: 0.1.2
4
+ Summary: Clean, dependency-managed toolkit with raw script segments and dataset handling options
5
5
  Author-email: Your Name <your.email@example.com>
6
6
  Classifier: Programming Language :: Python :: 3
7
7
  Classifier: License :: OSI Approved :: MIT License
@@ -1,186 +0,0 @@
1
- import pandas as pd
2
- import numpy as np
3
- import matplotlib.pyplot as plt
4
- import seaborn as sns
5
- from scipy import stats
6
- from sklearn.preprocessing import StandardScaler
7
- from sklearn.decomposition import PCA
8
- from sklearn.model_selection import train_test_split
9
- from sklearn.linear_model import LinearRegression, LogisticRegression
10
- from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
11
- from sklearn.cluster import KMeans
12
- import os
13
-
14
- def ensure_dataset(filepath='iris.csv'):
15
- """Ensures that the local dataset exists. If not, automatically downloads and formats it."""
16
- if not os.path.exists(filepath):
17
- url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
18
- df = pd.read_csv(url)
19
- df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
20
- df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
21
- df.to_csv(filepath, index=False)
22
-
23
- def numpy_stats(filepath='iris.csv'):
24
- """Topic 1: Basic numerical operations and statistics using NumPy."""
25
- ensure_dataset(filepath)
26
- df = pd.read_csv(filepath)
27
- sepal_length = df['SepalLengthCm'].to_numpy()
28
- return {
29
- 'sum': np.sum(sepal_length),
30
- 'mean': np.mean(sepal_length),
31
- 'max': np.max(sepal_length),
32
- 'min': np.min(sepal_length),
33
- 'first_five': sepal_length[:5]
34
- }
35
-
36
- def inspect_dataset(filepath='iris.csv'):
37
- """Topic 2: Load DataFrame and inspect its structure, columns, and shape."""
38
- ensure_dataset(filepath)
39
- df = pd.read_csv(filepath)
40
- return {
41
- 'head': df.head(),
42
- 'shape': df.shape,
43
- 'dtypes': df.dtypes,
44
- 'description': df.describe()
45
- }
46
-
47
- def handle_missing_values(filepath='iris.csv'):
48
- """Topic 3: Introduce mock missing values and handle them using median and mode."""
49
- ensure_dataset(filepath)
50
- df_copy = pd.read_csv(filepath).copy()
51
- df_copy.loc[[5, 10, 15], 'SepalLengthCm'] = np.nan
52
- df_copy.loc[[20, 25], 'Species'] = np.nan
53
-
54
- median_sepal = df_copy['SepalLengthCm'].median()
55
- df_copy['SepalLengthCm'] = df_copy['SepalLengthCm'].fillna(median_sepal)
56
- mode_species = df_copy['Species'].mode()[0]
57
- df_copy['Species'] = df_copy['Species'].fillna(mode_species)
58
- return df_copy
59
-
60
- def data_transformation(filepath='iris.csv'):
61
- """Topic 4: Perform one-hot encoding and standardization (scaling)."""
62
- ensure_dataset(filepath)
63
- df = pd.read_csv(filepath)
64
- df_encoded = pd.get_dummies(df, columns=['Species'])
65
- num_cols = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
66
- scaler = StandardScaler()
67
- df_encoded[num_cols] = scaler.fit_transform(df_encoded[num_cols])
68
- return df_encoded
69
-
70
- def eda_plots(filepath='iris.csv'):
71
- """Topic 5: Generate and render Histogram and Scatter Plots for EDA."""
72
- ensure_dataset(filepath)
73
- df = pd.read_csv(filepath)
74
- fig, axes = plt.subplots(1, 2, figsize=(14, 5))
75
- sns.histplot(data=df, x='PetalLengthCm', kde=True, color='skyblue', ax=axes[0])
76
- axes[0].set_title('Histogram of PetalLengthCm')
77
- sns.scatterplot(data=df, x='SepalLengthCm', y='PetalLengthCm', hue='Species', palette='Set1', ax=axes[1])
78
- axes[1].set_title('SepalLengthCm vs PetalLengthCm')
79
- plt.tight_layout()
80
- plt.show()
81
-
82
- def outlier_detection(filepath='iris.csv'):
83
- """Topic 6: Quantify outliers using Interquartile Range (IQR) bounds."""
84
- ensure_dataset(filepath)
85
- df = pd.read_csv(filepath)
86
- Q1 = df['SepalWidthCm'].quantile(0.25)
87
- Q3 = df['SepalWidthCm'].quantile(0.75)
88
- IQR = Q3 - Q1
89
- lower_bound = Q1 - 1.5 * IQR
90
- upper_bound = Q3 + 1.5 * IQR
91
- outliers = df[(df['SepalWidthCm'] < lower_bound) | (df['SepalWidthCm'] > upper_bound)]
92
- return {
93
- 'IQR': IQR,
94
- 'limits': (lower_bound, upper_bound),
95
- 'outliers': outliers
96
- }
97
-
98
- def descriptive_stats(filepath='iris.csv'):
99
- """Topic 7: Calculate detailed descriptive metrics."""
100
- ensure_dataset(filepath)
101
- df = pd.read_csv(filepath)
102
- attributes = ['SepalLengthCm', 'PetalLengthCm']
103
- return pd.DataFrame({
104
- 'Mean': df[attributes].mean(),
105
- 'Median': df[attributes].median(),
106
- 'Sample Variance': df[attributes].var(),
107
- 'Standard Deviation': df[attributes].std()
108
- })
109
-
110
- def correlation_covariance(filepath='iris.csv'):
111
- """Topic 8: Evaluate covariance and Pearson correlation coefficient."""
112
- ensure_dataset(filepath)
113
- df = pd.read_csv(filepath)
114
- return {
115
- 'covariance': df['PetalLengthCm'].cov(df['PetalWidthCm']),
116
- 'correlation': df['PetalLengthCm'].corr(df['PetalWidthCm'])
117
- }
118
-
119
- def hypothesis_testing(filepath='iris.csv'):
120
- """Topic 9: Run an independent T-test on SepalLengthCm between Setosa and Versicolor."""
121
- ensure_dataset(filepath)
122
- df = pd.read_csv(filepath)
123
- setosa_sepal = df[df['Species'] == 'Iris-setosa']['SepalLengthCm']
124
- versicolor_sepal = df[df['Species'] == 'Iris-versicolor']['SepalLengthCm']
125
- t_stat, p_val = stats.ttest_ind(setosa_sepal, versicolor_sepal, equal_var=False)
126
- return {'t_statistic': t_stat, 'p_value': p_val}
127
-
128
- def linear_regression(filepath='iris.csv'):
129
- """Topic 10: Linear Regression predicting PetalLengthCm."""
130
- ensure_dataset(filepath)
131
- df = pd.read_csv(filepath)
132
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalWidthCm']]
133
- y = df['PetalLengthCm']
134
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
135
- model = LinearRegression()
136
- model.fit(X_train, y_train)
137
- predictions = model.predict(X_test)
138
- return {
139
- 'coefficients': {col: coef for col, coef in zip(X.columns, model.coef_)},
140
- 'intercept': model.intercept_,
141
- 'predictions': pd.DataFrame({'Actual': y_test, 'Predicted': predictions})
142
- }
143
-
144
- def logistic_regression(filepath='iris.csv'):
145
- """Topic 11: Multi-class Logistic Regression for classification."""
146
- ensure_dataset(filepath)
147
- df = pd.read_csv(filepath)
148
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
149
- y = df['Species']
150
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
151
- clf = LogisticRegression(max_iter=200)
152
- clf.fit(X_train, y_train)
153
- y_pred = clf.predict(X_test)
154
- return {
155
- 'accuracy': accuracy_score(y_test, y_pred),
156
- 'confusion_matrix': confusion_matrix(y_test, y_pred)
157
- }
158
-
159
- def kmeans_clustering(filepath='iris.csv'):
160
- """Topic 12: Cluster attributes unsupervised using K-Means."""
161
- ensure_dataset(filepath)
162
- df = pd.read_csv(filepath)
163
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
164
- kmeans = KMeans(n_clusters=3, random_state=42, n_init='auto')
165
- df['Cluster'] = kmeans.fit_predict(X)
166
- return df
167
-
168
- def pca_analysis(filepath='iris.csv'):
169
- """Topic 13: Standardize, compress with 2-Component PCA, and classify."""
170
- ensure_dataset(filepath)
171
- df = pd.read_csv(filepath)
172
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
173
- y = df['Species']
174
- X_scaled = StandardScaler().fit_transform(X)
175
- pca = PCA(n_components=2)
176
- X_pca = pca.fit_transform(X_scaled)
177
- X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
178
- clf = LogisticRegression()
179
- clf.fit(X_train, y_train)
180
- y_pred = clf.predict(X_test)
181
- return {
182
- 'accuracy': accuracy_score(y_test, y_pred),
183
- 'precision': precision_score(y_test, y_pred, average='macro'),
184
- 'recall': recall_score(y_test, y_pred, average='macro'),
185
- 'f1_score': f1_score(y_test, y_pred, average='macro')
186
- }
File without changes
File without changes