scikit-tools 0.1.1__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.1
4
- Summary: Clean, dependency-managed toolkit with fully self-contained topic functions
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.1"
8
- description = "Clean, dependency-managed toolkit with fully self-contained topic functions"
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.1
4
- Summary: Clean, dependency-managed toolkit with fully self-contained topic functions
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,205 +0,0 @@
1
- def ensure_dataset(filepath='iris.csv'):
2
- """Ensures that the local dataset exists. If not, automatically downloads and formats it."""
3
- import os
4
- import pandas as pd
5
- if not os.path.exists(filepath):
6
- url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
7
- df = pd.read_csv(url)
8
- df.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']
9
- df['Species'] = df['Species'].map(lambda x: f"Iris-{x}")
10
- df.to_csv(filepath, index=False)
11
-
12
- def numpy_stats(filepath='iris.csv'):
13
- """Topic 1: Basic numerical operations and statistics using NumPy."""
14
- import pandas as pd
15
- import numpy as np
16
- ensure_dataset(filepath)
17
- df = pd.read_csv(filepath)
18
- sepal_length = df['SepalLengthCm'].to_numpy()
19
- return {
20
- 'sum': np.sum(sepal_length),
21
- 'mean': np.mean(sepal_length),
22
- 'max': np.max(sepal_length),
23
- 'min': np.min(sepal_length),
24
- 'first_five': sepal_length[:5]
25
- }
26
-
27
- def inspect_dataset(filepath='iris.csv'):
28
- """Topic 2: Load DataFrame and inspect its structure, columns, and shape."""
29
- import pandas as pd
30
- ensure_dataset(filepath)
31
- df = pd.read_csv(filepath)
32
- return {
33
- 'head': df.head(),
34
- 'shape': df.shape,
35
- 'dtypes': df.dtypes,
36
- 'description': df.describe()
37
- }
38
-
39
- def handle_missing_values(filepath='iris.csv'):
40
- """Topic 3: Introduce mock missing values and handle them using median and mode."""
41
- import pandas as pd
42
- import numpy as np
43
- ensure_dataset(filepath)
44
- df_copy = pd.read_csv(filepath).copy()
45
- df_copy.loc[[5, 10, 15], 'SepalLengthCm'] = np.nan
46
- df_copy.loc[[20, 25], 'Species'] = np.nan
47
-
48
- median_sepal = df_copy['SepalLengthCm'].median()
49
- df_copy['SepalLengthCm'] = df_copy['SepalLengthCm'].fillna(median_sepal)
50
- mode_species = df_copy['Species'].mode()[0]
51
- df_copy['Species'] = df_copy['Species'].fillna(mode_species)
52
- return df_copy
53
-
54
- def data_transformation(filepath='iris.csv'):
55
- """Topic 4: Perform one-hot encoding and standardization (scaling)."""
56
- import pandas as pd
57
- from sklearn.preprocessing import StandardScaler
58
- ensure_dataset(filepath)
59
- df = pd.read_csv(filepath)
60
- df_encoded = pd.get_dummies(df, columns=['Species'])
61
- num_cols = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']
62
- scaler = StandardScaler()
63
- df_encoded[num_cols] = scaler.fit_transform(df_encoded[num_cols])
64
- return df_encoded
65
-
66
- def eda_plots(filepath='iris.csv'):
67
- """Topic 5: Generate and render Histogram and Scatter Plots for EDA."""
68
- import pandas as pd
69
- import matplotlib.pyplot as plt
70
- import seaborn as sns
71
- ensure_dataset(filepath)
72
- df = pd.read_csv(filepath)
73
- fig, axes = plt.subplots(1, 2, figsize=(14, 5))
74
- sns.histplot(data=df, x='PetalLengthCm', kde=True, color='skyblue', ax=axes[0])
75
- axes[0].set_title('Histogram of PetalLengthCm')
76
- sns.scatterplot(data=df, x='SepalLengthCm', y='PetalLengthCm', hue='Species', palette='Set1', ax=axes[1])
77
- axes[1].set_title('SepalLengthCm vs PetalLengthCm')
78
- plt.tight_layout()
79
- plt.show()
80
-
81
- def outlier_detection(filepath='iris.csv'):
82
- """Topic 6: Quantify outliers using Interquartile Range (IQR) bounds."""
83
- import pandas as pd
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
- import pandas as pd
101
- ensure_dataset(filepath)
102
- df = pd.read_csv(filepath)
103
- attributes = ['SepalLengthCm', 'PetalLengthCm']
104
- return pd.DataFrame({
105
- 'Mean': df[attributes].mean(),
106
- 'Median': df[attributes].median(),
107
- 'Sample Variance': df[attributes].var(),
108
- 'Standard Deviation': df[attributes].std()
109
- })
110
-
111
- def correlation_covariance(filepath='iris.csv'):
112
- """Topic 8: Evaluate covariance and Pearson correlation coefficient."""
113
- import pandas as pd
114
- ensure_dataset(filepath)
115
- df = pd.read_csv(filepath)
116
- return {
117
- 'covariance': df['PetalLengthCm'].cov(df['PetalWidthCm']),
118
- 'correlation': df['PetalLengthCm'].corr(df['PetalWidthCm'])
119
- }
120
-
121
- def hypothesis_testing(filepath='iris.csv'):
122
- """Topic 9: Run an independent T-test on SepalLengthCm between Setosa and Versicolor."""
123
- import pandas as pd
124
- from scipy import stats
125
- ensure_dataset(filepath)
126
- df = pd.read_csv(filepath)
127
- setosa_sepal = df[df['Species'] == 'Iris-setosa']['SepalLengthCm']
128
- versicolor_sepal = df[df['Species'] == 'Iris-versicolor']['SepalLengthCm']
129
- t_stat, p_val = stats.ttest_ind(setosa_sepal, versicolor_sepal, equal_var=False)
130
- return {'t_statistic': t_stat, 'p_value': p_val}
131
-
132
- def linear_regression(filepath='iris.csv'):
133
- """Topic 10: Linear Regression predicting PetalLengthCm."""
134
- import pandas as pd
135
- from sklearn.model_selection import train_test_split
136
- from sklearn.linear_model import LinearRegression
137
- ensure_dataset(filepath)
138
- df = pd.read_csv(filepath)
139
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalWidthCm']]
140
- y = df['PetalLengthCm']
141
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
142
- model = LinearRegression()
143
- model.fit(X_train, y_train)
144
- predictions = model.predict(X_test)
145
- return {
146
- 'coefficients': {col: coef for col, coef in zip(X.columns, model.coef_)},
147
- 'intercept': model.intercept_,
148
- 'predictions': pd.DataFrame({'Actual': y_test, 'Predicted': predictions})
149
- }
150
-
151
- def logistic_regression(filepath='iris.csv'):
152
- """Topic 11: Multi-class Logistic Regression for classification."""
153
- import pandas as pd
154
- from sklearn.model_selection import train_test_split
155
- from sklearn.linear_model import LogisticRegression
156
- from sklearn.metrics import accuracy_score, confusion_matrix
157
- ensure_dataset(filepath)
158
- df = pd.read_csv(filepath)
159
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
160
- y = df['Species']
161
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
162
- clf = LogisticRegression(max_iter=200)
163
- clf.fit(X_train, y_train)
164
- y_pred = clf.predict(X_test)
165
- return {
166
- 'accuracy': accuracy_score(y_test, y_pred),
167
- 'confusion_matrix': confusion_matrix(y_test, y_pred)
168
- }
169
-
170
- def kmeans_clustering(filepath='iris.csv'):
171
- """Topic 12: Cluster attributes unsupervised using K-Means."""
172
- import pandas as pd
173
- from sklearn.cluster import KMeans
174
- ensure_dataset(filepath)
175
- df = pd.read_csv(filepath)
176
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
177
- kmeans = KMeans(n_clusters=3, random_state=42, n_init='auto')
178
- df['Cluster'] = kmeans.fit_predict(X)
179
- return df
180
-
181
- def pca_analysis(filepath='iris.csv'):
182
- """Topic 13: Standardize, compress with 2-Component PCA, and classify."""
183
- import pandas as pd
184
- from sklearn.preprocessing import StandardScaler
185
- from sklearn.decomposition import PCA
186
- from sklearn.model_selection import train_test_split
187
- from sklearn.linear_model import LogisticRegression
188
- from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
189
- ensure_dataset(filepath)
190
- df = pd.read_csv(filepath)
191
- X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
192
- y = df['Species']
193
- X_scaled = StandardScaler().fit_transform(X)
194
- pca = PCA(n_components=2)
195
- X_pca = pca.fit_transform(X_scaled)
196
- X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
197
- clf = LogisticRegression()
198
- clf.fit(X_train, y_train)
199
- y_pred = clf.predict(X_test)
200
- return {
201
- 'accuracy': accuracy_score(y_test, y_pred),
202
- 'precision': precision_score(y_test, y_pred, average='macro'),
203
- 'recall': recall_score(y_test, y_pred, average='macro'),
204
- 'f1_score': f1_score(y_test, y_pred, average='macro')
205
- }
File without changes
File without changes