daup 0.1.0__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.
- daup-0.1.0/LICENSE +17 -0
- daup-0.1.0/PKG-INFO +48 -0
- daup-0.1.0/README.md +33 -0
- daup-0.1.0/daup/__init__.py +68 -0
- daup-0.1.0/daup/_scripts/api.py +64 -0
- daup-0.1.0/daup/_scripts/bcan.py +148 -0
- daup-0.1.0/daup/_scripts/cgi.py +108 -0
- daup-0.1.0/daup/_scripts/data.py +54 -0
- daup-0.1.0/daup/_scripts/db.py +73 -0
- daup-0.1.0/daup/_scripts/dirty.py +79 -0
- daup-0.1.0/daup/_scripts/iris.py +99 -0
- daup-0.1.0/daup/_scripts/pand.py +172 -0
- daup-0.1.0/daup/_scripts/wine.py +155 -0
- daup-0.1.0/daup.egg-info/PKG-INFO +48 -0
- daup-0.1.0/daup.egg-info/SOURCES.txt +17 -0
- daup-0.1.0/daup.egg-info/dependency_links.txt +1 -0
- daup-0.1.0/daup.egg-info/top_level.txt +1 -0
- daup-0.1.0/pyproject.toml +21 -0
- daup-0.1.0/setup.cfg +4 -0
daup-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daup maintainer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
daup-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: daup
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Download bundled Python practical scripts with simple functions
|
|
5
|
+
Author: Daup maintainer
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: python,scripts,education,download
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# daup
|
|
17
|
+
|
|
18
|
+
A small Python package that copies bundled example scripts into your current directory.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install daup
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import daup
|
|
30
|
+
|
|
31
|
+
daup.api() # creates api.py in the current directory
|
|
32
|
+
daup.bcan() # creates bcan.py
|
|
33
|
+
daup.cgi() # creates cgi.py
|
|
34
|
+
daup.data() # creates data.py
|
|
35
|
+
daup.db() # creates db.py
|
|
36
|
+
daup.dirty() # creates dirty.py
|
|
37
|
+
daup.iris() # creates iris.py
|
|
38
|
+
daup.pand() # creates pand.py
|
|
39
|
+
daup.wine() # creates wine.py
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Pass an optional destination directory:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
daup.api("downloads")
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Each function returns the absolute path to the copied file. Existing files with the same name are overwritten.
|
daup-0.1.0/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# daup
|
|
2
|
+
|
|
3
|
+
A small Python package that copies bundled example scripts into your current directory.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install daup
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import daup
|
|
15
|
+
|
|
16
|
+
daup.api() # creates api.py in the current directory
|
|
17
|
+
daup.bcan() # creates bcan.py
|
|
18
|
+
daup.cgi() # creates cgi.py
|
|
19
|
+
daup.data() # creates data.py
|
|
20
|
+
daup.db() # creates db.py
|
|
21
|
+
daup.dirty() # creates dirty.py
|
|
22
|
+
daup.iris() # creates iris.py
|
|
23
|
+
daup.pand() # creates pand.py
|
|
24
|
+
daup.wine() # creates wine.py
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Pass an optional destination directory:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
daup.api("downloads")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Each function returns the absolute path to the copied file. Existing files with the same name are overwritten.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Daup: download bundled Python example scripts to the current directory."""
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from importlib.resources import files
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
_SCRIPT_NAMES = ("api", "bcan", "cgi", "data", "db", "dirty", "iris", "pand", "wine")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _download_script(name, destination="."):
|
|
11
|
+
"""Copy a bundled script to destination and return its path."""
|
|
12
|
+
if name not in _SCRIPT_NAMES:
|
|
13
|
+
raise ValueError(f"Unknown script: {name}")
|
|
14
|
+
destination = Path(destination).expanduser()
|
|
15
|
+
destination.mkdir(parents=True, exist_ok=True)
|
|
16
|
+
output = destination / f"{name}.py"
|
|
17
|
+
source = files("daup").joinpath("_scripts", f"{name}.py")
|
|
18
|
+
output.write_bytes(source.read_bytes())
|
|
19
|
+
print(f"Downloaded {name}.py to {output.resolve()}")
|
|
20
|
+
return output.resolve()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def api(destination="."):
|
|
24
|
+
"""Download api.py to destination."""
|
|
25
|
+
return _download_script("api", destination)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def bcan(destination="."):
|
|
29
|
+
"""Download bcan.py to destination."""
|
|
30
|
+
return _download_script("bcan", destination)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def cgi(destination="."):
|
|
34
|
+
"""Download cgi.py to destination."""
|
|
35
|
+
return _download_script("cgi", destination)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def data(destination="."):
|
|
39
|
+
"""Download data.py to destination."""
|
|
40
|
+
return _download_script("data", destination)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def db(destination="."):
|
|
44
|
+
"""Download db.py to destination."""
|
|
45
|
+
return _download_script("db", destination)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def dirty(destination="."):
|
|
49
|
+
"""Download dirty.py to destination."""
|
|
50
|
+
return _download_script("dirty", destination)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def iris(destination="."):
|
|
54
|
+
"""Download iris.py to destination."""
|
|
55
|
+
return _download_script("iris", destination)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def pand(destination="."):
|
|
59
|
+
"""Download pand.py to destination."""
|
|
60
|
+
return _download_script("pand", destination)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def wine(destination="."):
|
|
64
|
+
"""Download wine.py to destination."""
|
|
65
|
+
return _download_script("wine", destination)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
__all__ = [*_SCRIPT_NAMES, "__version__"]
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
#Source Code
|
|
3
|
+
import requests
|
|
4
|
+
|
|
5
|
+
BASE_URL = "https://jsonplaceholder.typicode.com/posts" # Mock REST API for reliable testing
|
|
6
|
+
|
|
7
|
+
def check_status(response):
|
|
8
|
+
if response.status_code in [200, 201, 204]:
|
|
9
|
+
print(f"Status Code: {response.status_code} - Success")
|
|
10
|
+
return True
|
|
11
|
+
else:
|
|
12
|
+
print(f"Status Code: {response.status_code} - Failed")
|
|
13
|
+
return False
|
|
14
|
+
|
|
15
|
+
def create_student(name, age, course):
|
|
16
|
+
payload = {"name": name, "age": age, "course": course}
|
|
17
|
+
res = requests.post(BASE_URL, json=payload)
|
|
18
|
+
print("Creating Student:", name)
|
|
19
|
+
if check_status(res):
|
|
20
|
+
data = res.json()
|
|
21
|
+
return data.get("id")
|
|
22
|
+
return None
|
|
23
|
+
|
|
24
|
+
def get_all_students():
|
|
25
|
+
res = requests.get(BASE_URL)
|
|
26
|
+
print("Fetching All Students...")
|
|
27
|
+
if check_status(res):
|
|
28
|
+
return res.json()[:5] # Limit output for display
|
|
29
|
+
return []
|
|
30
|
+
|
|
31
|
+
def update_student(student_id, name, age, course):
|
|
32
|
+
url = f"{BASE_URL}/{student_id}"
|
|
33
|
+
payload = {"name": name, "age": age, "course": course}
|
|
34
|
+
res = requests.put(url, json=payload)
|
|
35
|
+
print(f"Updating Student ID {student_id}...")
|
|
36
|
+
check_status(res)
|
|
37
|
+
|
|
38
|
+
def delete_student(student_id):
|
|
39
|
+
url = f"{BASE_URL}/{student_id}"
|
|
40
|
+
res = requests.delete(url)
|
|
41
|
+
print(f"Deleting Student ID {student_id}...")
|
|
42
|
+
check_status(res)
|
|
43
|
+
|
|
44
|
+
def main():
|
|
45
|
+
id1 = create_student("Alice", 20, "CS")
|
|
46
|
+
id2 = create_student("Bob", 22, "IT")
|
|
47
|
+
id3 = create_student("Charlie", 21, "ECE")
|
|
48
|
+
|
|
49
|
+
print("\n--- All Students ---")
|
|
50
|
+
all_st = get_all_students()
|
|
51
|
+
print("Students Count:", len(all_st))
|
|
52
|
+
|
|
53
|
+
if id2:
|
|
54
|
+
update_student(id2, "Bob", 22, "Data Science")
|
|
55
|
+
|
|
56
|
+
if id1:
|
|
57
|
+
delete_student(id1)
|
|
58
|
+
|
|
59
|
+
print("\n--- Final Students List ---")
|
|
60
|
+
final_st = get_all_students()
|
|
61
|
+
print("Final List Count:", len(final_st))
|
|
62
|
+
|
|
63
|
+
if __name__ == "__main__":
|
|
64
|
+
main()
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import seaborn as sns
|
|
4
|
+
|
|
5
|
+
from sklearn.datasets import load_breast_cancer
|
|
6
|
+
from sklearn.model_selection import train_test_split
|
|
7
|
+
from sklearn.preprocessing import StandardScaler
|
|
8
|
+
from sklearn.ensemble import RandomForestClassifier
|
|
9
|
+
from sklearn.tree import DecisionTreeClassifier
|
|
10
|
+
from sklearn.linear_model import LogisticRegression
|
|
11
|
+
from sklearn.metrics import accuracy_score, recall_score, confusion_matrix
|
|
12
|
+
|
|
13
|
+
data = load_breast_cancer(as_frame=True)
|
|
14
|
+
|
|
15
|
+
X = data.data
|
|
16
|
+
y = data.target
|
|
17
|
+
|
|
18
|
+
print("Dataset Shape:", X.shape)
|
|
19
|
+
print("\nClass Distribution:")
|
|
20
|
+
print(y.value_counts().rename(index={0: "Malignant", 1: "Benign"}))
|
|
21
|
+
|
|
22
|
+
sns.countplot(x=y.map({0: "Malignant", 1: "Benign"}))
|
|
23
|
+
plt.title("Class Distribution")
|
|
24
|
+
plt.xlabel("Tumor Type")
|
|
25
|
+
plt.show()
|
|
26
|
+
|
|
27
|
+
plt.figure(figsize=(14, 10))
|
|
28
|
+
sns.heatmap(X.corr(), cmap="coolwarm", center=0)
|
|
29
|
+
plt.title("Feature Correlation Heatmap")
|
|
30
|
+
plt.show()
|
|
31
|
+
|
|
32
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
33
|
+
X, y, test_size=0.2, random_state=42, stratify=y
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
scaler = StandardScaler()
|
|
37
|
+
X_train_scaled = scaler.fit_transform(X_train)
|
|
38
|
+
X_test_scaled = scaler.transform(X_test)
|
|
39
|
+
|
|
40
|
+
rf_ranker = RandomForestClassifier(n_estimators=100, random_state=42)
|
|
41
|
+
rf_ranker.fit(X_train, y_train)
|
|
42
|
+
|
|
43
|
+
importance = pd.Series(
|
|
44
|
+
rf_ranker.feature_importances_,
|
|
45
|
+
index=X.columns
|
|
46
|
+
).sort_values(ascending=False)
|
|
47
|
+
|
|
48
|
+
print("\nFeature Ranking:")
|
|
49
|
+
print(importance)
|
|
50
|
+
|
|
51
|
+
plt.figure(figsize=(10, 6))
|
|
52
|
+
importance.head(10).sort_values().plot(kind="barh")
|
|
53
|
+
plt.title("Top 10 Features by Random Forest Importance")
|
|
54
|
+
plt.xlabel("Importance")
|
|
55
|
+
plt.show()
|
|
56
|
+
|
|
57
|
+
top_features = importance.head(10).index.tolist()
|
|
58
|
+
|
|
59
|
+
X_train_top = X_train[top_features]
|
|
60
|
+
X_test_top = X_test[top_features]
|
|
61
|
+
|
|
62
|
+
scaler_top = StandardScaler()
|
|
63
|
+
X_train_top_scaled = scaler_top.fit_transform(X_train_top)
|
|
64
|
+
X_test_top_scaled = scaler_top.transform(X_test_top)
|
|
65
|
+
|
|
66
|
+
models = {
|
|
67
|
+
"Decision Tree": DecisionTreeClassifier(random_state=42),
|
|
68
|
+
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
|
|
69
|
+
"Logistic Regression": LogisticRegression(max_iter=1000)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
results = []
|
|
73
|
+
|
|
74
|
+
for name, model in models.items():
|
|
75
|
+
model.fit(X_train_scaled, y_train)
|
|
76
|
+
y_pred = model.predict(X_test_scaled)
|
|
77
|
+
|
|
78
|
+
results.append({
|
|
79
|
+
"Model": name,
|
|
80
|
+
"Features": "All 30",
|
|
81
|
+
"Accuracy": accuracy_score(y_test, y_pred),
|
|
82
|
+
"Malignant Recall": recall_score(y_test, y_pred, pos_label=0),
|
|
83
|
+
"False Negatives": confusion_matrix(y_test, y_pred, labels=[0, 1])[0, 1]
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
for name, model in models.items():
|
|
87
|
+
model.fit(X_train_top_scaled, y_train)
|
|
88
|
+
y_pred = model.predict(X_test_top_scaled)
|
|
89
|
+
|
|
90
|
+
results.append({
|
|
91
|
+
"Model": name,
|
|
92
|
+
"Features": "Top 10",
|
|
93
|
+
"Accuracy": accuracy_score(y_test, y_pred),
|
|
94
|
+
"Malignant Recall": recall_score(y_test, y_pred, pos_label=0),
|
|
95
|
+
"False Negatives": confusion_matrix(y_test, y_pred, labels=[0, 1])[0, 1]
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
results_df = pd.DataFrame(results)
|
|
99
|
+
print("\nModel Comparison:")
|
|
100
|
+
print(results_df)
|
|
101
|
+
|
|
102
|
+
plt.figure(figsize=(10, 6))
|
|
103
|
+
sns.barplot(data=results_df, x="Model", y="Accuracy", hue="Features")
|
|
104
|
+
plt.title("Accuracy Before and After Feature Reduction")
|
|
105
|
+
plt.ylim(0, 1)
|
|
106
|
+
plt.show()
|
|
107
|
+
|
|
108
|
+
plt.figure(figsize=(10, 6))
|
|
109
|
+
sns.barplot(data=results_df, x="Model", y="Malignant Recall", hue="Features")
|
|
110
|
+
plt.title("Malignant Recall Before and After Feature Reduction")
|
|
111
|
+
plt.ylim(0, 1)
|
|
112
|
+
plt.show()
|
|
113
|
+
|
|
114
|
+
best_model_name = results_df.sort_values(
|
|
115
|
+
["False Negatives", "Malignant Recall", "Accuracy"],
|
|
116
|
+
ascending=[True, False, False]
|
|
117
|
+
).iloc[0]["Model"]
|
|
118
|
+
|
|
119
|
+
print("\nModel with the fewest false negatives:")
|
|
120
|
+
print(best_model_name)
|
|
121
|
+
|
|
122
|
+
final_model = LogisticRegression(max_iter=1000)
|
|
123
|
+
final_model.fit(X_train_scaled, y_train)
|
|
124
|
+
|
|
125
|
+
y_pred = final_model.predict(X_test_scaled)
|
|
126
|
+
|
|
127
|
+
cm = confusion_matrix(y_test, y_pred, labels=[0, 1])
|
|
128
|
+
|
|
129
|
+
sns.heatmap(
|
|
130
|
+
cm,
|
|
131
|
+
annot=True,
|
|
132
|
+
fmt="d",
|
|
133
|
+
cmap="Blues",
|
|
134
|
+
xticklabels=["Malignant", "Benign"],
|
|
135
|
+
yticklabels=["Malignant", "Benign"]
|
|
136
|
+
)
|
|
137
|
+
plt.xlabel("Predicted")
|
|
138
|
+
plt.ylabel("Actual")
|
|
139
|
+
plt.title("Logistic Regression Confusion Matrix")
|
|
140
|
+
plt.show()
|
|
141
|
+
|
|
142
|
+
new_samples = X_test.iloc[:3]
|
|
143
|
+
new_samples_scaled = scaler.transform(new_samples)
|
|
144
|
+
predictions = final_model.predict(new_samples_scaled)
|
|
145
|
+
|
|
146
|
+
print("\nPredictions for New Samples:")
|
|
147
|
+
for i, prediction in enumerate(predictions):
|
|
148
|
+
print("Sample", i + 1, ":", data.target_names[prediction])
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#sql + cgi
|
|
2
|
+
#!/usr/bin/env python3
|
|
3
|
+
import cgi
|
|
4
|
+
import html
|
|
5
|
+
import sqlite3
|
|
6
|
+
|
|
7
|
+
print("Content-Type: text/html\n")
|
|
8
|
+
conn = sqlite3.connect("cgi_demo.db")
|
|
9
|
+
cur = conn.cursor()
|
|
10
|
+
cur.execute("CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
|
|
11
|
+
conn.commit()
|
|
12
|
+
|
|
13
|
+
form = cgi.FieldStorage()
|
|
14
|
+
action = form.getvalue("action")
|
|
15
|
+
if action == "insert":
|
|
16
|
+
name = form.getvalue("name")
|
|
17
|
+
if name:
|
|
18
|
+
cur.execute("INSERT INTO Users (name) VALUES (?)", (name,))
|
|
19
|
+
conn.commit()
|
|
20
|
+
elif action == "update":
|
|
21
|
+
uid = form.getvalue("id")
|
|
22
|
+
name = form.getvalue("name")
|
|
23
|
+
if uid and name:
|
|
24
|
+
cur.execute("UPDATE Users SET name=? WHERE id=?", (name, uid))
|
|
25
|
+
conn.commit()
|
|
26
|
+
elif action == "destroy":
|
|
27
|
+
uid = form.getvalue("id")
|
|
28
|
+
if uid:
|
|
29
|
+
cur.execute("DELETE FROM Users WHERE id=?", (uid,))
|
|
30
|
+
conn.commit()
|
|
31
|
+
cur.execute("SELECT * FROM Users")
|
|
32
|
+
rows = cur.fetchall()
|
|
33
|
+
conn.close()
|
|
34
|
+
|
|
35
|
+
print("""
|
|
36
|
+
<html><body>
|
|
37
|
+
<h2>CGI Database CRUD</h2>
|
|
38
|
+
<form method="post">
|
|
39
|
+
Name: <input type="text" name="name"><br><br>
|
|
40
|
+
<button type="submit" name="action" value="insert">Insert</button>
|
|
41
|
+
</form>
|
|
42
|
+
<h3>Database Records:</h3>
|
|
43
|
+
<table border="1" cellpadding="6">
|
|
44
|
+
<tr><th>ID</th><th>Name</th><th>Actions</th></tr>
|
|
45
|
+
""")
|
|
46
|
+
for r in rows:
|
|
47
|
+
print(f"""
|
|
48
|
+
<tr>
|
|
49
|
+
<td>{r[0]}</td>
|
|
50
|
+
<td>{html.escape(r[1])}</td>
|
|
51
|
+
<td>
|
|
52
|
+
<form method="post" style="display:inline">
|
|
53
|
+
<input type="hidden" name="id" value="{r[0]}">
|
|
54
|
+
<input type="text" name="name" value="{html.escape(r[1], quote=True)}">
|
|
55
|
+
<button type="submit" name="action" value="update">Edit</button>
|
|
56
|
+
</form>
|
|
57
|
+
<form method="post" style="display:inline">
|
|
58
|
+
<input type="hidden" name="id" value="{r[0]}">
|
|
59
|
+
<button type="submit" name="action" value="destroy">Delete</button>
|
|
60
|
+
</form>
|
|
61
|
+
</td>
|
|
62
|
+
</tr>
|
|
63
|
+
""")
|
|
64
|
+
print("</table></body></html>")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
#MSC reg
|
|
69
|
+
#!/usr/bin/env python3
|
|
70
|
+
import cgi
|
|
71
|
+
import html
|
|
72
|
+
|
|
73
|
+
print("Content-Type: text/html\n")
|
|
74
|
+
|
|
75
|
+
form = cgi.FieldStorage()
|
|
76
|
+
|
|
77
|
+
name = form.getvalue("name")
|
|
78
|
+
email = form.getvalue("email")
|
|
79
|
+
degree = form.getvalue("degree")
|
|
80
|
+
|
|
81
|
+
if name and email and degree:
|
|
82
|
+
print(f"""
|
|
83
|
+
<html>
|
|
84
|
+
<head><title>Registration Summary</title></head>
|
|
85
|
+
<body>
|
|
86
|
+
<h2>MSc Admission Registration Details</h2>
|
|
87
|
+
<p><b>Name:</b> {html.escape(name)}</p>
|
|
88
|
+
<p><b>Email:</b> {html.escape(email)}</p>
|
|
89
|
+
<p><b>Previous Degree:</b> {html.escape(degree)}</p>
|
|
90
|
+
</body>
|
|
91
|
+
</html>
|
|
92
|
+
""")
|
|
93
|
+
else:
|
|
94
|
+
print("""
|
|
95
|
+
<html>
|
|
96
|
+
<head><title>MSc Admission Form</title></head>
|
|
97
|
+
<body>
|
|
98
|
+
<h2>MSc Admission Registration</h2>
|
|
99
|
+
<form method="post" action="q29.py">
|
|
100
|
+
Name: <input type="text" name="name"><br><br>
|
|
101
|
+
Email: <input type="email" name="email"><br><br>
|
|
102
|
+
Previous Degree: <input type="text" name="degree"><br><br>
|
|
103
|
+
<input type="submit" value="Register">
|
|
104
|
+
</form>
|
|
105
|
+
</body>
|
|
106
|
+
</html>
|
|
107
|
+
""")
|
|
108
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import seaborn as sns
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
from sklearn.preprocessing import StandardScaler
|
|
5
|
+
|
|
6
|
+
df = pd.read_csv("Data.csv")
|
|
7
|
+
|
|
8
|
+
print("Original Dataset:")
|
|
9
|
+
print(df.head())
|
|
10
|
+
print("\nDataset Shape:", df.shape)
|
|
11
|
+
print("\nMissing Values:")
|
|
12
|
+
print(df.isnull().sum())
|
|
13
|
+
|
|
14
|
+
sns.heatmap(df.isnull(), cbar=False, yticklabels=False)
|
|
15
|
+
plt.title("Missing Values")
|
|
16
|
+
plt.show()
|
|
17
|
+
|
|
18
|
+
num_cols = df.select_dtypes(include="number").columns
|
|
19
|
+
cat_cols = df.select_dtypes(exclude="number").columns
|
|
20
|
+
|
|
21
|
+
for col in num_cols:
|
|
22
|
+
df[col] = df[col].fillna(df[col].median())
|
|
23
|
+
|
|
24
|
+
for col in cat_cols:
|
|
25
|
+
df[col] = df[col].fillna(df[col].mode()[0])
|
|
26
|
+
|
|
27
|
+
print("\nMissing Values After Handling:")
|
|
28
|
+
print(df.isnull().sum())
|
|
29
|
+
|
|
30
|
+
for col in num_cols:
|
|
31
|
+
sns.histplot(df[col], kde=True)
|
|
32
|
+
plt.title("Distribution of " + col)
|
|
33
|
+
plt.show()
|
|
34
|
+
|
|
35
|
+
for col in cat_cols:
|
|
36
|
+
sns.countplot(data=df, x=col)
|
|
37
|
+
plt.title("Count of " + col)
|
|
38
|
+
plt.xticks(rotation=45)
|
|
39
|
+
plt.show()
|
|
40
|
+
|
|
41
|
+
df = pd.get_dummies(df, columns=cat_cols, drop_first=True, dtype=int)
|
|
42
|
+
|
|
43
|
+
scaler = StandardScaler()
|
|
44
|
+
df[num_cols] = scaler.fit_transform(df[num_cols])
|
|
45
|
+
|
|
46
|
+
print("\nPreprocessed Dataset:")
|
|
47
|
+
print(df.head())
|
|
48
|
+
|
|
49
|
+
plt.figure(figsize=(10, 6))
|
|
50
|
+
sns.heatmap(df.corr(), annot=True, cmap="coolwarm")
|
|
51
|
+
plt.title("Correlation Heatmap")
|
|
52
|
+
plt.show()
|
|
53
|
+
|
|
54
|
+
df.to_csv("Preprocessed_Data.csv", index=False)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import sqlite3
|
|
2
|
+
|
|
3
|
+
conn = sqlite3.connect("employee.db")
|
|
4
|
+
cursor = conn.cursor()
|
|
5
|
+
|
|
6
|
+
cursor.execute("""
|
|
7
|
+
CREATE TABLE IF NOT EXISTS Employee (
|
|
8
|
+
Emp_ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
9
|
+
Name VARCHAR(100) NOT NULL,
|
|
10
|
+
Department VARCHAR(100) NOT NULL,
|
|
11
|
+
Salary REAL NOT NULL,
|
|
12
|
+
Designation VARCHAR(100) NOT NULL
|
|
13
|
+
)
|
|
14
|
+
""")
|
|
15
|
+
conn.commit()
|
|
16
|
+
|
|
17
|
+
cursor.execute("""
|
|
18
|
+
INSERT INTO Employee (Name, Department, Salary, Designation)
|
|
19
|
+
VALUES (?, ?, ?, ?)
|
|
20
|
+
""", ("Anu", "HR", 45000, "Assistant Manager"))
|
|
21
|
+
conn.commit()
|
|
22
|
+
|
|
23
|
+
employees = [
|
|
24
|
+
("Rahul", "IT", 60000, "Software Engineer"),
|
|
25
|
+
("Meera", "Finance", 55000, "Accountant"),
|
|
26
|
+
("Arun", "Production", 48000, "Supervisor")
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
cursor.executemany("""
|
|
30
|
+
INSERT INTO Employee (Name, Department, Salary, Designation)
|
|
31
|
+
VALUES (?, ?, ?, ?)
|
|
32
|
+
""", employees)
|
|
33
|
+
conn.commit()
|
|
34
|
+
|
|
35
|
+
cursor.execute("SELECT * FROM Employee")
|
|
36
|
+
print("All Employee Records:")
|
|
37
|
+
for row in cursor.fetchall():
|
|
38
|
+
print(row)
|
|
39
|
+
|
|
40
|
+
emp_id = 1
|
|
41
|
+
cursor.execute("SELECT * FROM Employee WHERE Emp_ID = ?", (emp_id,))
|
|
42
|
+
print("\nEmployee Search Result:")
|
|
43
|
+
print(cursor.fetchone())
|
|
44
|
+
|
|
45
|
+
cursor.execute("""
|
|
46
|
+
UPDATE Employee
|
|
47
|
+
SET Salary = ?
|
|
48
|
+
WHERE Emp_ID = ?
|
|
49
|
+
""", (65000, emp_id))
|
|
50
|
+
conn.commit()
|
|
51
|
+
|
|
52
|
+
cursor.execute("""
|
|
53
|
+
UPDATE Employee
|
|
54
|
+
SET Designation = ?
|
|
55
|
+
WHERE Emp_ID = ?
|
|
56
|
+
""", ("Senior Manager", emp_id))
|
|
57
|
+
conn.commit()
|
|
58
|
+
|
|
59
|
+
cursor.execute("""
|
|
60
|
+
DELETE FROM Employee
|
|
61
|
+
WHERE Emp_ID = ?
|
|
62
|
+
""", (3,))
|
|
63
|
+
conn.commit()
|
|
64
|
+
|
|
65
|
+
cursor.execute("""
|
|
66
|
+
SELECT * FROM Employee
|
|
67
|
+
WHERE Salary > ?
|
|
68
|
+
""", (50000,))
|
|
69
|
+
print("\nEmployees earning more than ₹50,000:")
|
|
70
|
+
for row in cursor.fetchall():
|
|
71
|
+
print(row)
|
|
72
|
+
|
|
73
|
+
conn.close()
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
df = pd.read_csv("dirtydata.csv")
|
|
5
|
+
|
|
6
|
+
print("Original Dataset:")
|
|
7
|
+
print(df)
|
|
8
|
+
|
|
9
|
+
print("\nMissing Values:")
|
|
10
|
+
print(df.isnull().sum())
|
|
11
|
+
|
|
12
|
+
df_drop = df.dropna()
|
|
13
|
+
print("\nRows after dropna():")
|
|
14
|
+
print(df_drop)
|
|
15
|
+
|
|
16
|
+
df_fill = df.copy()
|
|
17
|
+
|
|
18
|
+
num_cols = df_fill.select_dtypes(include="number").columns
|
|
19
|
+
cat_cols = df_fill.select_dtypes(include="object").columns
|
|
20
|
+
|
|
21
|
+
for col in num_cols:
|
|
22
|
+
df_fill[col] = df_fill[col].fillna(df_fill[col].mean())
|
|
23
|
+
|
|
24
|
+
for col in cat_cols:
|
|
25
|
+
df_fill[col] = df_fill[col].fillna(df_fill[col].mode()[0])
|
|
26
|
+
|
|
27
|
+
print("\nMissing Values Filled:")
|
|
28
|
+
print(df_fill)
|
|
29
|
+
|
|
30
|
+
df_mean = df.copy()
|
|
31
|
+
df_median = df.copy()
|
|
32
|
+
df_mode = df.copy()
|
|
33
|
+
|
|
34
|
+
for col in num_cols:
|
|
35
|
+
df_mean[col] = df_mean[col].fillna(df_mean[col].mean())
|
|
36
|
+
df_median[col] = df_median[col].fillna(df_median[col].median())
|
|
37
|
+
df_mode[col] = df_mode[col].fillna(df_mode[col].mode()[0])
|
|
38
|
+
|
|
39
|
+
for col in cat_cols:
|
|
40
|
+
df_mean[col] = df_mean[col].fillna(df_mean[col].mode()[0])
|
|
41
|
+
df_median[col] = df_median[col].fillna(df_median[col].mode()[0])
|
|
42
|
+
df_mode[col] = df_mode[col].fillna(df_mode[col].mode()[0])
|
|
43
|
+
|
|
44
|
+
print("\nFilled using Mean:")
|
|
45
|
+
print(df_mean)
|
|
46
|
+
|
|
47
|
+
print("\nFilled using Median:")
|
|
48
|
+
print(df_median)
|
|
49
|
+
|
|
50
|
+
print("\nFilled using Mode:")
|
|
51
|
+
print(df_mode)
|
|
52
|
+
|
|
53
|
+
print("\nData Types:")
|
|
54
|
+
print(df.dtypes)
|
|
55
|
+
|
|
56
|
+
for col in num_cols:
|
|
57
|
+
df[col] = pd.to_numeric(df[col], errors="coerce")
|
|
58
|
+
|
|
59
|
+
for col in cat_cols:
|
|
60
|
+
df[col] = df[col].astype(str).str.strip()
|
|
61
|
+
|
|
62
|
+
print("\nCorrected Data Types:")
|
|
63
|
+
print(df.dtypes)
|
|
64
|
+
|
|
65
|
+
print("\nRemove Invalid Data:")
|
|
66
|
+
for col in df.select_dtypes(include="number").columns:
|
|
67
|
+
df = df[df[col] >= 0]
|
|
68
|
+
|
|
69
|
+
print(df)
|
|
70
|
+
|
|
71
|
+
print("\nDuplicate Rows:")
|
|
72
|
+
print(df[df.duplicated()])
|
|
73
|
+
|
|
74
|
+
df = df.drop_duplicates()
|
|
75
|
+
|
|
76
|
+
print("\nDataset after Removing Duplicates:")
|
|
77
|
+
print(df)
|
|
78
|
+
|
|
79
|
+
df.to_csv("cleaned_dirtydata.csv", index=False)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import seaborn as sns
|
|
5
|
+
|
|
6
|
+
from sklearn.datasets import load_iris
|
|
7
|
+
from sklearn.preprocessing import StandardScaler
|
|
8
|
+
from sklearn.cluster import KMeans
|
|
9
|
+
from sklearn.decomposition import PCA
|
|
10
|
+
|
|
11
|
+
iris=load_iris()
|
|
12
|
+
|
|
13
|
+
X=pd.DataFrame(
|
|
14
|
+
iris.data,
|
|
15
|
+
columns=iris.feature_names
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
print(X.head())
|
|
19
|
+
|
|
20
|
+
X.hist(figsize=(10,7),bins=20,edgecolor="black")
|
|
21
|
+
plt.suptitle("Distributions ->")
|
|
22
|
+
plt.tight_layout()
|
|
23
|
+
plt.show()
|
|
24
|
+
|
|
25
|
+
scalar=StandardScaler()
|
|
26
|
+
X_sc=scalar.fit_transform(X)
|
|
27
|
+
print(X_sc.shape)
|
|
28
|
+
|
|
29
|
+
print("Feature mean : ",X_sc.mean(axis=0).round(2))
|
|
30
|
+
|
|
31
|
+
print("Standard deviation : ",X_sc.std(axis=0).round(2))
|
|
32
|
+
|
|
33
|
+
models={}
|
|
34
|
+
cluster_lab={}
|
|
35
|
+
|
|
36
|
+
for k in [2,3,4]:
|
|
37
|
+
|
|
38
|
+
kmeans=KMeans(
|
|
39
|
+
n_clusters=k,
|
|
40
|
+
random_state=42,
|
|
41
|
+
n_init=10
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
labels=kmeans.fit_predict(X_sc)
|
|
45
|
+
|
|
46
|
+
models[k]=kmeans
|
|
47
|
+
cluster_lab[k]=labels
|
|
48
|
+
|
|
49
|
+
print(f"k={k}")
|
|
50
|
+
print("Cluster sizes : ",np.bincount(labels))
|
|
51
|
+
print("Inertia : ",round(kmeans.inertia_,2))
|
|
52
|
+
|
|
53
|
+
inertias=[]
|
|
54
|
+
for k in range(1,11):
|
|
55
|
+
kmeans=KMeans(
|
|
56
|
+
n_clusters=k,
|
|
57
|
+
random_state=42,
|
|
58
|
+
n_init=10
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
kmeans.fit(X_sc)
|
|
62
|
+
|
|
63
|
+
inertias.append(round(kmeans.inertia_,2))
|
|
64
|
+
|
|
65
|
+
print(inertias)
|
|
66
|
+
|
|
67
|
+
plt.figure(figsize=(8,5))
|
|
68
|
+
plt.plot(range(1,11),inertias,marker="x")
|
|
69
|
+
|
|
70
|
+
plt.xlabel("No. of clusters(k)--->")
|
|
71
|
+
plt.ylabel("Inertias--->")
|
|
72
|
+
plt.title("Elbow method for optimal k")
|
|
73
|
+
plt.xticks(list(range(1,11)))
|
|
74
|
+
plt.grid(True)
|
|
75
|
+
plt.show()
|
|
76
|
+
|
|
77
|
+
pca=PCA(n_components=2)
|
|
78
|
+
|
|
79
|
+
X_pca=pca.fit_transform(X_sc)
|
|
80
|
+
print("Variance explained : ",pca.explained_variance_ratio_.round(3))
|
|
81
|
+
|
|
82
|
+
fig,axes=plt.subplots(1,3,figsize=(18,5))
|
|
83
|
+
|
|
84
|
+
for ax, k in zip(axes,[2,3,4]):
|
|
85
|
+
ax.scatter(
|
|
86
|
+
X_pca[:, 0],
|
|
87
|
+
X_pca[:, 1],
|
|
88
|
+
c=cluster_lab[k],
|
|
89
|
+
cmap="viridis",
|
|
90
|
+
s=50
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
ax.set_title(f"K-Means Clusters (k = {k})")
|
|
94
|
+
ax.set_xlabel("Principal Component 1")
|
|
95
|
+
ax.set_ylabel("Principal Component 2")
|
|
96
|
+
|
|
97
|
+
plt.tight_layout()
|
|
98
|
+
plt.show()
|
|
99
|
+
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import seaborn as sns
|
|
4
|
+
|
|
5
|
+
# Q33: Student Series, DataFrame, Pass/Fail
|
|
6
|
+
name = pd.Series(["Anu", "Rahul", "Meera", "Arun", "Sneha"])
|
|
7
|
+
age = pd.Series([20, 21, 19, 22, 20])
|
|
8
|
+
course = pd.Series(["B.Tech", "B.Sc", "B.Com", "B.Tech", "BCA"])
|
|
9
|
+
marks = pd.Series([85, 35, 72, 40, 28])
|
|
10
|
+
|
|
11
|
+
df = pd.DataFrame({"Name": name, "Age": age, "Course": course, "Marks": marks})
|
|
12
|
+
df["Result"] = ["Pass" if m >= 40 else "Fail" for m in df["Marks"]]
|
|
13
|
+
print(df[df["Result"] == "Pass"])
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Q34: Department average salary and highest-paid employee
|
|
17
|
+
df = pd.DataFrame({
|
|
18
|
+
"EmployeeID": [101, 102, 103, 104, 105],
|
|
19
|
+
"Name": ["Anu", "Rahul", "Meera", "Arun", "Sneha"],
|
|
20
|
+
"Department": ["HR", "IT", "HR", "IT", "Sales"],
|
|
21
|
+
"Salary": [40000, 60000, 45000, 75000, 50000]
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
print(df.groupby("Department")["Salary"].mean())
|
|
25
|
+
print(df[df["Salary"] == df["Salary"].max()])
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Q35: Monthly sales and grouped bar chart
|
|
29
|
+
df = pd.DataFrame({
|
|
30
|
+
"Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
|
|
31
|
+
"Product A": [150, 200, 180, 220, 250, 210],
|
|
32
|
+
"Product B": [180, 160, 210, 240, 200, 230],
|
|
33
|
+
"Product C": [120, 140, 160, 180, 210, 190]
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
df["Total"] = df[["Product A", "Product B", "Product C"]].sum(axis=1)
|
|
37
|
+
print(df.loc[df["Total"].idxmax(), "Month"])
|
|
38
|
+
|
|
39
|
+
df.plot(x="Month", y=["Product A", "Product B", "Product C"], kind="bar")
|
|
40
|
+
plt.show()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Q36: Weather averages and temperature line plot
|
|
44
|
+
df = pd.DataFrame({
|
|
45
|
+
"Day": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
|
|
46
|
+
"Temperature": [28, 30, 29, 31, 27, 26, 28],
|
|
47
|
+
"Humidity": [70, 65, 75, 60, 80, 85, 72],
|
|
48
|
+
"Rainfall": [0, 2, 5, 0, 10, 8, 1]
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
print(df["Temperature"].mean())
|
|
52
|
+
print(df["Rainfall"].sum())
|
|
53
|
+
|
|
54
|
+
plt.plot(df["Day"], df["Temperature"], marker="o")
|
|
55
|
+
plt.xlabel("Day")
|
|
56
|
+
plt.ylabel("Temperature (°C)")
|
|
57
|
+
plt.show()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Q37: Convert continuous age values into categories
|
|
61
|
+
df = pd.DataFrame({
|
|
62
|
+
"Name": ["Alberto Franco", "Gino Mcneill", "Ryan Parkes",
|
|
63
|
+
"Eesha Hinton", "Syed Wharton"],
|
|
64
|
+
"Age": [18, 22, 40, 50, 80]
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
df["Age_Group"] = pd.cut(
|
|
68
|
+
df["Age"], bins=[0, 18, 40, 100],
|
|
69
|
+
labels=["Young", "Adult", "Senior"], include_lowest=True
|
|
70
|
+
)
|
|
71
|
+
print(df)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# Q38: Concatenate DataFrames along rows
|
|
75
|
+
student_data1 = pd.DataFrame({
|
|
76
|
+
"student_id": ["s1", "s2", "s3", "s4", "s5"],
|
|
77
|
+
"name": ["Danniella Fenton", "Ryder Storey", "Bryce Jensen",
|
|
78
|
+
"Ed Bernal", "Kwame Morin"],
|
|
79
|
+
"marks": [200, 210, 190, 222, 199]
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
student_data2 = pd.DataFrame({
|
|
83
|
+
"student_id": ["s4", "s5", "s6", "s7", "s8"],
|
|
84
|
+
"name": ["Scarlette Fisher", "Carla Williamson", "Dante Morse",
|
|
85
|
+
"Kaiser William", "Madeeha Preston"],
|
|
86
|
+
"marks": [201, 200, 198, 219, 201]
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
print(pd.concat([student_data1, student_data2], ignore_index=True))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# Q39: Concatenate DataFrames along columns
|
|
93
|
+
print(pd.concat([student_data1, student_data2], axis=1))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# Q40: Concatenate student data and merge with exam data
|
|
97
|
+
students = pd.concat([student_data1, student_data2], ignore_index=True)
|
|
98
|
+
|
|
99
|
+
exam_data = pd.DataFrame({
|
|
100
|
+
"student_id": ["s1", "s2", "s3", "s4", "s5", "s7", "s8",
|
|
101
|
+
"s9", "s10", "s11", "s12", "s13"],
|
|
102
|
+
"exam_id": [23, 45, 12, 67, 21, 55, 33, 14, 56, 83, 88, 12]
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
print(pd.merge(students, exam_data, on="student_id", how="inner"))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# Q41: Inner join matching student IDs
|
|
109
|
+
print(pd.merge(student_data1, student_data2, on="student_id", how="inner"))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# Q42: Group by school and calculate age statistics
|
|
113
|
+
df = pd.DataFrame({
|
|
114
|
+
"school": ["S1", "S2", "S3", "S4", "S5", "S6"],
|
|
115
|
+
"class": ["s001", "s002", "s003", "s001", "s002", "s004"],
|
|
116
|
+
"name": ["Alberto Franco", "Gino Mcneill", "Ryan Parkes",
|
|
117
|
+
"Eesha Hinton", "Gino Mcneill", "David Parkes"],
|
|
118
|
+
"date_Of_Birth": ["15/05/2002", "17/05/2002", "16/02/1999",
|
|
119
|
+
"25/09/1998", "11/05/2002", "15/09/1997"],
|
|
120
|
+
"age": [12, 12, 13, 13, 14, 12],
|
|
121
|
+
"height": [173, 192, 186, 167, 151, 159],
|
|
122
|
+
"weight": [35, 32, 33, 30, 31, 32],
|
|
123
|
+
"address": ["street1", "street2", "street3", "street1", "street2", "street4"]
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
groups = df.groupby("school")
|
|
127
|
+
print(type(groups))
|
|
128
|
+
print(groups["age"].agg(["mean", "min", "max"]))
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# Q43: Count occurrences of each unique row
|
|
132
|
+
df = pd.DataFrame({
|
|
133
|
+
"Id": [1, 2, 1, 1, 2, 1, 2],
|
|
134
|
+
"type": [10, 15, 11, 20, 21, 12, 14],
|
|
135
|
+
"book": ["Math", "English", "Physics", "Math",
|
|
136
|
+
"English", "Physics", "English"]
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
print(df.value_counts())
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# Q44: Alphabet stock data visualization
|
|
143
|
+
df = pd.read_csv("alphabet_stock_data.csv")
|
|
144
|
+
df["Date"] = pd.to_datetime(df["Date"])
|
|
145
|
+
|
|
146
|
+
start_date = "2020-01-01"
|
|
147
|
+
end_date = "2020-12-31"
|
|
148
|
+
data = df[(df["Date"] >= start_date) & (df["Date"] <= end_date)]
|
|
149
|
+
|
|
150
|
+
# Line plot of historical stock prices
|
|
151
|
+
data.plot(x="Date", y=["Open", "Close", "High", "Low"])
|
|
152
|
+
plt.title("Alphabet Stock Prices")
|
|
153
|
+
plt.show()
|
|
154
|
+
|
|
155
|
+
# Bar plot of trading volume
|
|
156
|
+
data.plot(x="Date", y="Volume", kind="bar")
|
|
157
|
+
plt.title("Trading Volume")
|
|
158
|
+
plt.show()
|
|
159
|
+
|
|
160
|
+
# Stacked histogram of stock prices
|
|
161
|
+
data[["Open", "Close", "High", "Low"]].plot(
|
|
162
|
+
kind="hist", bins=30, stacked=True
|
|
163
|
+
)
|
|
164
|
+
plt.title("Stock Price Distribution")
|
|
165
|
+
plt.show()
|
|
166
|
+
|
|
167
|
+
# Scatter plot of volume vs closing price
|
|
168
|
+
plt.scatter(data["Volume"], data["Close"])
|
|
169
|
+
plt.xlabel("Trading Volume")
|
|
170
|
+
plt.ylabel("Closing Price")
|
|
171
|
+
plt.title("Volume vs Stock Price")
|
|
172
|
+
plt.show()
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import numpy as np
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import seaborn as sns
|
|
6
|
+
|
|
7
|
+
from sklearn.model_selection import train_test_split
|
|
8
|
+
from sklearn.preprocessing import StandardScaler
|
|
9
|
+
from sklearn.linear_model import LinearRegression
|
|
10
|
+
from sklearn.tree import DecisionTreeRegressor
|
|
11
|
+
from sklearn.ensemble import RandomForestRegressor
|
|
12
|
+
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
|
|
13
|
+
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
|
|
14
|
+
|
|
15
|
+
red_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
|
|
16
|
+
white_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv"
|
|
17
|
+
|
|
18
|
+
red = pd.read_csv(red_url, sep=";")
|
|
19
|
+
white = pd.read_csv(white_url, sep=";")
|
|
20
|
+
|
|
21
|
+
print("Red wine shape:", red.shape)
|
|
22
|
+
print("White wine shape:", white.shape)
|
|
23
|
+
print("Red wine columns:", red.columns.tolist())
|
|
24
|
+
print("White wine columns:", white.columns.tolist())
|
|
25
|
+
print("\nRed wine:\n", red.head())
|
|
26
|
+
print("\nWhite wine:\n", white.head())
|
|
27
|
+
|
|
28
|
+
print("\nSame columns:", red.columns.equals(white.columns))
|
|
29
|
+
|
|
30
|
+
red["wine_type"] = "red"
|
|
31
|
+
white["wine_type"] = "White"
|
|
32
|
+
|
|
33
|
+
wine = pd.concat([red, white], ignore_index=True)
|
|
34
|
+
|
|
35
|
+
print("\nIntegrated shape:", wine.shape)
|
|
36
|
+
print("Unique wine types:", wine["wine_type"].unique())
|
|
37
|
+
print("Wine type counts:\n", wine["wine_type"].value_counts())
|
|
38
|
+
|
|
39
|
+
wine.to_csv("integrated_wine_quality.csv", index=False)
|
|
40
|
+
|
|
41
|
+
print("\nMissing values:\n", wine.isnull().sum())
|
|
42
|
+
print("Duplicate rows:", wine.duplicated().sum())
|
|
43
|
+
|
|
44
|
+
wine = wine.drop_duplicates()
|
|
45
|
+
|
|
46
|
+
print("Shape after removing duplicates:", wine.shape)
|
|
47
|
+
|
|
48
|
+
y = wine["quality"]
|
|
49
|
+
X = wine.drop("quality", axis=1)
|
|
50
|
+
|
|
51
|
+
X = pd.get_dummies(X, columns=["wine_type"], drop_first=True, dtype=int)
|
|
52
|
+
|
|
53
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
54
|
+
X, y, test_size=0.2, random_state=42
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
scaler = StandardScaler()
|
|
58
|
+
|
|
59
|
+
X_train_scaled = scaler.fit_transform(X_train)
|
|
60
|
+
X_test_scaled = scaler.transform(X_test)
|
|
61
|
+
|
|
62
|
+
plt.figure(figsize=(8, 5))
|
|
63
|
+
sns.countplot(data=wine, x="quality")
|
|
64
|
+
plt.title("Distribution of Wine Quality Scores")
|
|
65
|
+
plt.xlabel("Quality")
|
|
66
|
+
plt.ylabel("Number of wines")
|
|
67
|
+
plt.show()
|
|
68
|
+
|
|
69
|
+
plt.figure(figsize=(8, 5))
|
|
70
|
+
sns.boxplot(data=wine, x="wine_type", y="quality")
|
|
71
|
+
plt.title("Wine Quality by Wine Type")
|
|
72
|
+
plt.xlabel("Wine Type")
|
|
73
|
+
plt.ylabel("Quality Score")
|
|
74
|
+
plt.show()
|
|
75
|
+
|
|
76
|
+
average_quality = wine.groupby("wine_type")["quality"].mean()
|
|
77
|
+
print("\nAverage quality:\n", average_quality)
|
|
78
|
+
|
|
79
|
+
correlation = wine.drop(columns="wine_type").corr()
|
|
80
|
+
|
|
81
|
+
plt.figure(figsize=(12, 8))
|
|
82
|
+
sns.heatmap(correlation, annot=True, cmap="coolwarm", fmt=".2f")
|
|
83
|
+
plt.title("Correlation Heatmap")
|
|
84
|
+
plt.show()
|
|
85
|
+
|
|
86
|
+
quality_corr = correlation["quality"].drop("quality")
|
|
87
|
+
|
|
88
|
+
top_3 = quality_corr.reindex(
|
|
89
|
+
quality_corr.abs().sort_values(ascending=False).index
|
|
90
|
+
).head(3)
|
|
91
|
+
|
|
92
|
+
print("\nTop 3 features correlated with quality:\n", top_3)
|
|
93
|
+
|
|
94
|
+
plt.figure(figsize=(8, 5))
|
|
95
|
+
sns.scatterplot(data=wine, x="alcohol", y="quality", hue="wine_type")
|
|
96
|
+
plt.title("Alcohol vs Wine Quality")
|
|
97
|
+
plt.xlabel("Alcohol")
|
|
98
|
+
plt.ylabel("Quality")
|
|
99
|
+
plt.show()
|
|
100
|
+
|
|
101
|
+
models = {
|
|
102
|
+
"Linear Regression": LinearRegression(),
|
|
103
|
+
"Decision Tree": DecisionTreeRegressor(random_state=42),
|
|
104
|
+
"Random Forest": RandomForestRegressor(
|
|
105
|
+
n_estimators=100, random_state=42
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
results = []
|
|
110
|
+
|
|
111
|
+
for name, model in models.items():
|
|
112
|
+
model.fit(X_train_scaled, y_train)
|
|
113
|
+
y_pred = model.predict(X_test_scaled)
|
|
114
|
+
|
|
115
|
+
r2 = r2_score(y_test, y_pred)
|
|
116
|
+
mae = mean_absolute_error(y_test, y_pred)
|
|
117
|
+
mse = mean_squared_error(y_test, y_pred)
|
|
118
|
+
rmse = np.sqrt(mse)
|
|
119
|
+
|
|
120
|
+
results.append({
|
|
121
|
+
"Model": name,
|
|
122
|
+
"R2": r2,
|
|
123
|
+
"MAE": mae,
|
|
124
|
+
"MSE": mse,
|
|
125
|
+
"RMSE": rmse
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
results_df = pd.DataFrame(results)
|
|
129
|
+
|
|
130
|
+
print("\nModel Evaluation Results:")
|
|
131
|
+
print(results_df.round(4))
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
for name, model in models.items():
|
|
135
|
+
y_pred = model.predict(X_test_scaled)
|
|
136
|
+
|
|
137
|
+
y_pred_rounded = np.clip(np.rint(y_pred), 0, 10).astype(int)
|
|
138
|
+
y_test_int = y_test.astype(int)
|
|
139
|
+
|
|
140
|
+
labels = sorted(set(y_test_int) | set(y_pred_rounded))
|
|
141
|
+
|
|
142
|
+
cm = confusion_matrix(
|
|
143
|
+
y_test_int,
|
|
144
|
+
y_pred_rounded,
|
|
145
|
+
labels=labels
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
disp = ConfusionMatrixDisplay(
|
|
149
|
+
confusion_matrix=cm,
|
|
150
|
+
display_labels=labels
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
disp.plot(cmap="Blues", values_format="d")
|
|
154
|
+
plt.title(f"Confusion Matrix - {name}")
|
|
155
|
+
plt.show()
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: daup
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Download bundled Python practical scripts with simple functions
|
|
5
|
+
Author: Daup maintainer
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: python,scripts,education,download
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# daup
|
|
17
|
+
|
|
18
|
+
A small Python package that copies bundled example scripts into your current directory.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install daup
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import daup
|
|
30
|
+
|
|
31
|
+
daup.api() # creates api.py in the current directory
|
|
32
|
+
daup.bcan() # creates bcan.py
|
|
33
|
+
daup.cgi() # creates cgi.py
|
|
34
|
+
daup.data() # creates data.py
|
|
35
|
+
daup.db() # creates db.py
|
|
36
|
+
daup.dirty() # creates dirty.py
|
|
37
|
+
daup.iris() # creates iris.py
|
|
38
|
+
daup.pand() # creates pand.py
|
|
39
|
+
daup.wine() # creates wine.py
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Pass an optional destination directory:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
daup.api("downloads")
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Each function returns the absolute path to the copied file. Existing files with the same name are overwritten.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
daup/__init__.py
|
|
5
|
+
daup.egg-info/PKG-INFO
|
|
6
|
+
daup.egg-info/SOURCES.txt
|
|
7
|
+
daup.egg-info/dependency_links.txt
|
|
8
|
+
daup.egg-info/top_level.txt
|
|
9
|
+
daup/_scripts/api.py
|
|
10
|
+
daup/_scripts/bcan.py
|
|
11
|
+
daup/_scripts/cgi.py
|
|
12
|
+
daup/_scripts/data.py
|
|
13
|
+
daup/_scripts/db.py
|
|
14
|
+
daup/_scripts/dirty.py
|
|
15
|
+
daup/_scripts/iris.py
|
|
16
|
+
daup/_scripts/pand.py
|
|
17
|
+
daup/_scripts/wine.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
daup
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "daup"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Download bundled Python practical scripts with simple functions"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [{name = "Daup maintainer"}]
|
|
13
|
+
keywords = ["python", "scripts", "education", "download"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.package-data]
|
|
21
|
+
daup = ["_scripts/*.py"]
|
daup-0.1.0/setup.cfg
ADDED