numpi-isr 1.0.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.
- numpi_isr-1.0.0/PKG-INFO +14 -0
- numpi_isr-1.0.0/numpi/__init__.py +6 -0
- numpi_isr-1.0.0/numpi/pr1.py +157 -0
- numpi_isr-1.0.0/numpi/pr2.py +192 -0
- numpi_isr-1.0.0/numpi/pr3.py +259 -0
- numpi_isr-1.0.0/numpi/pr4.py +62 -0
- numpi_isr-1.0.0/numpi/pr5.py +66 -0
- numpi_isr-1.0.0/numpi/pr6.py +45 -0
- numpi_isr-1.0.0/numpi_isr.egg-info/PKG-INFO +14 -0
- numpi_isr-1.0.0/numpi_isr.egg-info/SOURCES.txt +13 -0
- numpi_isr-1.0.0/numpi_isr.egg-info/dependency_links.txt +1 -0
- numpi_isr-1.0.0/numpi_isr.egg-info/top_level.txt +1 -0
- numpi_isr-1.0.0/pyproject.toml +3 -0
- numpi_isr-1.0.0/setup.cfg +4 -0
- numpi_isr-1.0.0/setup.py +12 -0
numpi_isr-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: numpi-isr
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: My Personal ISR Practical Python Library
|
|
5
|
+
Author: Senorita
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/plain
|
|
8
|
+
Dynamic: author
|
|
9
|
+
Dynamic: description
|
|
10
|
+
Dynamic: description-content-type
|
|
11
|
+
Dynamic: requires-python
|
|
12
|
+
Dynamic: summary
|
|
13
|
+
|
|
14
|
+
A collection of six ISR practical programs.
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
CODE = """
|
|
2
|
+
!pip install nltk scikit-learn numpy
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from nltk.stem import PorterStemmer
|
|
8
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
9
|
+
from sklearn.metrics.pairwise import cosine_similarity
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
documents = [
|
|
13
|
+
"Electric vehicles are becoming popular. Electric cars reduce pollution.",
|
|
14
|
+
|
|
15
|
+
"Electric cars use rechargeable batteries for transportation.",
|
|
16
|
+
|
|
17
|
+
"Cricket is a popular sport. People enjoy playing cricket.",
|
|
18
|
+
|
|
19
|
+
"Football is a famous sport played worldwide.",
|
|
20
|
+
|
|
21
|
+
"Electric vehicles help reduce air pollution and save fuel."
|
|
22
|
+
]
|
|
23
|
+
filenames = [
|
|
24
|
+
"doc1.txt",
|
|
25
|
+
"doc2.txt",
|
|
26
|
+
"doc3.txt",
|
|
27
|
+
"doc4.txt",
|
|
28
|
+
"doc5.txt"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
print("Total Documents:",len(documents))
|
|
32
|
+
for name, doc in zip(filenames, documents):
|
|
33
|
+
print(name, ":", doc)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Step 1: Create Porter Stemmer
|
|
38
|
+
stemmer = PorterStemmer()
|
|
39
|
+
|
|
40
|
+
# Step 2: Function for conflation
|
|
41
|
+
def conflation(text):
|
|
42
|
+
words = re.findall(r'\b[a-zA-Z]+\b', text.lower())
|
|
43
|
+
|
|
44
|
+
stemmed_words = []
|
|
45
|
+
|
|
46
|
+
for word in words:
|
|
47
|
+
root_word = stemmer.stem(word)
|
|
48
|
+
stemmed_words.append(root_word)
|
|
49
|
+
|
|
50
|
+
return stemmed_words
|
|
51
|
+
|
|
52
|
+
# Step 3: Apply conflation to all documents
|
|
53
|
+
print("CONFLATION RESULTS\n")
|
|
54
|
+
|
|
55
|
+
for i, doc in enumerate(documents):
|
|
56
|
+
print(filenames[i], ":", conflation(doc))
|
|
57
|
+
|
|
58
|
+
words = ["playing", "played", "plays", "play"]
|
|
59
|
+
|
|
60
|
+
for word in words:
|
|
61
|
+
print(word, "→", stemmer.stem(word))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Create TF-IDF vectorizer
|
|
66
|
+
vectorizer = TfidfVectorizer(
|
|
67
|
+
tokenizer = conflation,
|
|
68
|
+
token_pattern = None
|
|
69
|
+
)
|
|
70
|
+
# Convert documents into numerical vectors
|
|
71
|
+
tfidf_matrix = vectorizer.fit_transform(documents)
|
|
72
|
+
|
|
73
|
+
print("Document representations generated successfully!")
|
|
74
|
+
print("\nTF-IDF Matrix Shape:")
|
|
75
|
+
print(tfidf_matrix.shape)
|
|
76
|
+
|
|
77
|
+
print("\nDocument Vectors:")
|
|
78
|
+
print(tfidf_matrix.toarray())
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
feature_names = vectorizer.get_feature_names_out()
|
|
83
|
+
|
|
84
|
+
print("Words used in TF-IDF:\n")
|
|
85
|
+
print(feature_names)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# Calculate similarity between all documents
|
|
91
|
+
|
|
92
|
+
similarity_matrix = cosine_similarity(tfidf_matrix)
|
|
93
|
+
|
|
94
|
+
print("COSINE SIMILARITY MATRIX\n")
|
|
95
|
+
|
|
96
|
+
print(np.round(similarity_matrix, 2))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# Single-Pass Clustering
|
|
101
|
+
|
|
102
|
+
clusters = []
|
|
103
|
+
|
|
104
|
+
threshold = 0.20
|
|
105
|
+
|
|
106
|
+
for i in range(len(documents)):
|
|
107
|
+
|
|
108
|
+
current_document = tfidf_matrix[i]
|
|
109
|
+
|
|
110
|
+
if len(clusters) == 0:
|
|
111
|
+
clusters.append([i])
|
|
112
|
+
continue
|
|
113
|
+
|
|
114
|
+
best_similarity = 0
|
|
115
|
+
best_cluster = -1
|
|
116
|
+
|
|
117
|
+
for j, cluster in enumerate(clusters):
|
|
118
|
+
|
|
119
|
+
# Calculate cluster centroid
|
|
120
|
+
centroid = np.asarray(
|
|
121
|
+
tfidf_matrix[cluster].mean(axis=0)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# Calculate cosine similarity
|
|
125
|
+
similarity = cosine_similarity(
|
|
126
|
+
current_document, centroid
|
|
127
|
+
)[0][0]
|
|
128
|
+
|
|
129
|
+
if similarity > best_similarity:
|
|
130
|
+
best_similarity = similarity
|
|
131
|
+
best_cluster = j
|
|
132
|
+
|
|
133
|
+
# Assign document to cluster
|
|
134
|
+
if best_similarity >= threshold:
|
|
135
|
+
clusters[best_cluster].append(i)
|
|
136
|
+
|
|
137
|
+
else:
|
|
138
|
+
clusters.append([i])
|
|
139
|
+
|
|
140
|
+
print("Clustering completed!")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
print("FINAL CLUSTERING RESULTS\n")
|
|
144
|
+
|
|
145
|
+
for i, cluster in enumerate(clusters):
|
|
146
|
+
|
|
147
|
+
print("Cluster", i + 1)
|
|
148
|
+
|
|
149
|
+
for doc_index in cluster:
|
|
150
|
+
print(" ", filenames[doc_index])
|
|
151
|
+
|
|
152
|
+
print()
|
|
153
|
+
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
def show():
|
|
157
|
+
print(CODE)
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
|
|
2
|
+
CODE = """
|
|
3
|
+
# ISR PRACTICAL 2
|
|
4
|
+
# Boolean Search and Vector Space Model
|
|
5
|
+
|
|
6
|
+
%pip install Whoosh scikit-learn numpy
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import shutil
|
|
10
|
+
import numpy as np
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
from whoosh import index
|
|
14
|
+
from whoosh.fields import Schema, TEXT, ID
|
|
15
|
+
from whoosh.qparser import QueryParser
|
|
16
|
+
|
|
17
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
18
|
+
from sklearn.metrics.pairwise import cosine_similarity
|
|
19
|
+
|
|
20
|
+
from collections import defaultdict
|
|
21
|
+
|
|
22
|
+
print("All libraries imported successfully!")
|
|
23
|
+
|
|
24
|
+
# STEP 1: CREATE DOCUMENTS
|
|
25
|
+
|
|
26
|
+
documents = [
|
|
27
|
+
"Python is a popular programming language used for web development and data science.",
|
|
28
|
+
|
|
29
|
+
"Java is an object oriented programming language used to develop software applications.",
|
|
30
|
+
|
|
31
|
+
"Machine learning is a branch of artificial intelligence that helps computers learn from data.",
|
|
32
|
+
|
|
33
|
+
"Electric vehicles use rechargeable batteries and help reduce air pollution.",
|
|
34
|
+
|
|
35
|
+
"Artificial intelligence and machine learning are used in modern technology."
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
titles = [
|
|
39
|
+
"Python Programming",
|
|
40
|
+
"Java Programming",
|
|
41
|
+
"Machine Learning",
|
|
42
|
+
"Electric Vehicles",
|
|
43
|
+
"Artificial Intelligence"
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
print("Total documents:", len(documents))
|
|
47
|
+
|
|
48
|
+
for i in range(len(documents)):
|
|
49
|
+
print("\\nDocument", i + 1)
|
|
50
|
+
print("Title:", titles[i])
|
|
51
|
+
print("Content:", documents[i])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# STEP 2: CREATE WHOOSH INDEX
|
|
55
|
+
|
|
56
|
+
schema = Schema(
|
|
57
|
+
doc_id=ID(stored=True, unique=True),
|
|
58
|
+
title=TEXT(stored=True),
|
|
59
|
+
content=TEXT(stored=True)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
index_folder = "search_index"
|
|
63
|
+
|
|
64
|
+
if os.path.exists(index_folder):
|
|
65
|
+
shutil.rmtree(index_folder)
|
|
66
|
+
|
|
67
|
+
os.mkdir(index_folder)
|
|
68
|
+
|
|
69
|
+
ix = index.create_in(index_folder, schema)
|
|
70
|
+
|
|
71
|
+
writer = ix.writer()
|
|
72
|
+
|
|
73
|
+
for i in range(len(documents)):
|
|
74
|
+
writer.add_document(
|
|
75
|
+
doc_id=str(i + 1),
|
|
76
|
+
title=titles[i],
|
|
77
|
+
content=documents[i]
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
writer.commit()
|
|
81
|
+
|
|
82
|
+
print("\\nAll documents indexed successfully!")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# STEP 3: BOOLEAN SEARCH FUNCTION
|
|
86
|
+
|
|
87
|
+
def boolean_search(search_text):
|
|
88
|
+
|
|
89
|
+
with ix.searcher() as searcher:
|
|
90
|
+
|
|
91
|
+
parser = QueryParser("content", schema=ix.schema)
|
|
92
|
+
|
|
93
|
+
query = parser.parse(search_text)
|
|
94
|
+
|
|
95
|
+
results = searcher.search(query)
|
|
96
|
+
|
|
97
|
+
print("\\nSearch Query:", search_text)
|
|
98
|
+
print("Results Found:", len(results))
|
|
99
|
+
|
|
100
|
+
if len(results) == 0:
|
|
101
|
+
print("No matching documents found.")
|
|
102
|
+
|
|
103
|
+
for result in results:
|
|
104
|
+
print("\\nDocument ID:", result["doc_id"])
|
|
105
|
+
print("Title:", result["title"])
|
|
106
|
+
print("Content:", result["content"])
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# STEP 4: USER INPUT FOR BOOLEAN SEARCH
|
|
110
|
+
|
|
111
|
+
print("\\n========== BOOLEAN SEARCH ==========")
|
|
112
|
+
|
|
113
|
+
print("Use AND, OR, NOT operators")
|
|
114
|
+
|
|
115
|
+
search_text = input("Enter Boolean search query: ")
|
|
116
|
+
|
|
117
|
+
boolean_search(search_text)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# STEP 5: TF-IDF VECTOR GENERATION
|
|
121
|
+
|
|
122
|
+
vectorizer = TfidfVectorizer()
|
|
123
|
+
|
|
124
|
+
tfidf_matrix = vectorizer.fit_transform(documents)
|
|
125
|
+
|
|
126
|
+
print("\\nTF-IDF vectors generated successfully!")
|
|
127
|
+
print("Matrix Shape:", tfidf_matrix.shape)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# STEP 6: VECTOR SEARCH FUNCTION
|
|
131
|
+
|
|
132
|
+
def vector_search(query):
|
|
133
|
+
|
|
134
|
+
query_vector = vectorizer.transform([query])
|
|
135
|
+
|
|
136
|
+
scores = cosine_similarity(query_vector, tfidf_matrix)[0]
|
|
137
|
+
|
|
138
|
+
ranked_indices = np.argsort(scores)[::-1]
|
|
139
|
+
|
|
140
|
+
print("\\nSearch Query:", query)
|
|
141
|
+
print("\\nRelevant Documents:\\n")
|
|
142
|
+
|
|
143
|
+
found = False
|
|
144
|
+
|
|
145
|
+
for i in ranked_indices:
|
|
146
|
+
|
|
147
|
+
if scores[i] > 0:
|
|
148
|
+
|
|
149
|
+
found = True
|
|
150
|
+
|
|
151
|
+
print("Title:", titles[i])
|
|
152
|
+
print("Similarity Score:", round(scores[i], 3))
|
|
153
|
+
print("Content:", documents[i])
|
|
154
|
+
print("-" * 50)
|
|
155
|
+
|
|
156
|
+
if not found:
|
|
157
|
+
print("No matching documents found.")
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
# STEP 7: USER INPUT FOR VECTOR SEARCH
|
|
161
|
+
|
|
162
|
+
print("\\n========== VECTOR SEARCH ==========")
|
|
163
|
+
|
|
164
|
+
vector_query = input("Enter vector search query: ")
|
|
165
|
+
|
|
166
|
+
vector_search(vector_query)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# STEP 8: INVERTED INDEX
|
|
170
|
+
|
|
171
|
+
inverted_index = defaultdict(list)
|
|
172
|
+
|
|
173
|
+
for i, doc in enumerate(documents):
|
|
174
|
+
|
|
175
|
+
words = set(re.findall(r'\\b\\w+\\b', doc.lower()))
|
|
176
|
+
|
|
177
|
+
for word in words:
|
|
178
|
+
inverted_index[word].append(titles[i])
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
print("\\n========== INVERTED INDEX ==========\\n")
|
|
182
|
+
|
|
183
|
+
for word, doc_list in sorted(inverted_index.items()):
|
|
184
|
+
|
|
185
|
+
print(word, ":", doc_list)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
print("\\nISR PRACTICAL 2 COMPLETED!")
|
|
189
|
+
"""
|
|
190
|
+
|
|
191
|
+
def show():
|
|
192
|
+
print(CODE)
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
CODE = """
|
|
2
|
+
# ISR PRACTICAL 3
|
|
3
|
+
|
|
4
|
+
import os, math, random
|
|
5
|
+
|
|
6
|
+
# 1. Create documents
|
|
7
|
+
os.makedirs("documents", exist_ok=True)
|
|
8
|
+
|
|
9
|
+
docs = {
|
|
10
|
+
"D1.txt": "Python programming and coding",
|
|
11
|
+
"D2.txt": "Python machine learning",
|
|
12
|
+
"D3.txt": "Java programming language",
|
|
13
|
+
"D4.txt": "Python data science",
|
|
14
|
+
"D5.txt": "Web development using HTML"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for name, text in docs.items():
|
|
18
|
+
with open("documents/" + name, "w") as f:
|
|
19
|
+
f.write(text)
|
|
20
|
+
|
|
21
|
+
# 2. Search documents
|
|
22
|
+
query = "Python programming"
|
|
23
|
+
relevant = ["D1.txt", "D2.txt", "D4.txt"]
|
|
24
|
+
|
|
25
|
+
results = []
|
|
26
|
+
for name in docs:
|
|
27
|
+
text = open("documents/" + name).read().lower().split()
|
|
28
|
+
if any(w in text for w in query.lower().split()):
|
|
29
|
+
results.append(name)
|
|
30
|
+
|
|
31
|
+
# 3. Calculate evaluation metrics
|
|
32
|
+
TP = len(set(results) & set(relevant))
|
|
33
|
+
P = TP / len(results) if results else 0
|
|
34
|
+
R = TP / len(relevant) if relevant else 0
|
|
35
|
+
F = 2 * P * R / (P + R) if P + R else 0
|
|
36
|
+
E = 1 - F
|
|
37
|
+
|
|
38
|
+
scores = [1 if d in relevant else 0 for d in results]
|
|
39
|
+
dcg = sum(s / math.log2(i + 2) for i, s in enumerate(scores))
|
|
40
|
+
idcg = sum(s / math.log2(i + 2) for i, s in enumerate(sorted(scores, reverse=True)))
|
|
41
|
+
ndcg = dcg / idcg if idcg else 0
|
|
42
|
+
|
|
43
|
+
print("Retrieved:", results)
|
|
44
|
+
print("Relevant:", relevant)
|
|
45
|
+
print("Precision:", round(P, 2))
|
|
46
|
+
print("Recall:", round(R, 2))
|
|
47
|
+
print("F-measure:", round(F, 2))
|
|
48
|
+
print("E-measure:", round(E, 2))
|
|
49
|
+
print("NDCG:", round(ndcg, 2))
|
|
50
|
+
|
|
51
|
+
# 4. A/B Testing Simulation
|
|
52
|
+
random.seed(1)
|
|
53
|
+
n = 2000
|
|
54
|
+
|
|
55
|
+
cA = sum(random.random() < 0.10 for _ in range(n))
|
|
56
|
+
cB = sum(random.random() < 0.12 for _ in range(n))
|
|
57
|
+
|
|
58
|
+
pA, pB = cA/n, cB/n
|
|
59
|
+
pool = (cA + cB) / (2*n)
|
|
60
|
+
z = (pB-pA) / math.sqrt(pool*(1-pool)*(2/n))
|
|
61
|
+
pval = math.erfc(abs(z)/math.sqrt(2))
|
|
62
|
+
|
|
63
|
+
print("\nA/B TESTING")
|
|
64
|
+
print("CTR A:", round(pA, 3), "CTR B:", round(pB, 3))
|
|
65
|
+
print("Z-score:", round(z, 2), "P-value:", round(pval, 4))
|
|
66
|
+
print("Version B is better!" if pval < 0.05 and pB > pA else "No significant improvement")
|
|
67
|
+
|
|
68
|
+
# 5. Overview and feedback
|
|
69
|
+
print("""
|
|
70
|
+
A/B Testing: Compare original (A) and modified (B) interfaces
|
|
71
|
+
using CTR, search time and user satisfaction.
|
|
72
|
+
|
|
73
|
+
Amazon: Search filters and product relevance.
|
|
74
|
+
Netflix: Thumbnails and personalized recommendations.
|
|
75
|
+
""")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
#my
|
|
127
|
+
|
|
128
|
+
import numpy as np
|
|
129
|
+
|
|
130
|
+
print("Libraries imported successfully!")
|
|
131
|
+
documents = {
|
|
132
|
+
"D1": "Python programming tutorial",
|
|
133
|
+
"D2": "Python applications in data science",
|
|
134
|
+
"D3": "Python programming examples",
|
|
135
|
+
"D4": "Electric vehicles and batteries",
|
|
136
|
+
"D5": "Cricket and sports news"
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
# Query
|
|
140
|
+
q1 = "Python programming"
|
|
141
|
+
|
|
142
|
+
# Actual relevant documents (ground truth)
|
|
143
|
+
relevant_documents = ["D1", "D2", "D3"]
|
|
144
|
+
|
|
145
|
+
# Documents returned by search engine (ranked)
|
|
146
|
+
retrieved_documents = ["D1", "D2", "D4", "D5"]
|
|
147
|
+
|
|
148
|
+
print("Query:", q1)
|
|
149
|
+
print("Relevant documents:", relevant_documents)
|
|
150
|
+
print("Retrieved documents:", retrieved_documents)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# Find correct and incorrect retrieved documents
|
|
157
|
+
|
|
158
|
+
TP = len(set(retrieved_documents) & set(relevant_documents))
|
|
159
|
+
|
|
160
|
+
FP = len(set(retrieved_documents) - set(relevant_documents))
|
|
161
|
+
|
|
162
|
+
FN = len(set(relevant_documents) - set(retrieved_documents))
|
|
163
|
+
|
|
164
|
+
# Calculate Precision
|
|
165
|
+
precision = TP / (TP + FP)
|
|
166
|
+
|
|
167
|
+
# Calculate Recall
|
|
168
|
+
recall = TP / (TP + FN)
|
|
169
|
+
|
|
170
|
+
print("True Positive:", TP)
|
|
171
|
+
print("False Positive:", FP)
|
|
172
|
+
print("False Negative:", FN)
|
|
173
|
+
|
|
174
|
+
print("\nPrecision:", round(precision, 2))
|
|
175
|
+
print("Recall:", round(recall, 2))
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
# F1 Measure
|
|
182
|
+
f_measure = (2 * precision * recall) / (precision + recall)
|
|
183
|
+
|
|
184
|
+
# E Measure
|
|
185
|
+
e_measure = 1 - f_measure
|
|
186
|
+
|
|
187
|
+
print("F-measure:", round(f_measure, 2))
|
|
188
|
+
print("E-measure:", round(e_measure, 2))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
# Calculate DCG
|
|
194
|
+
def calculate_dcg(scores):
|
|
195
|
+
dcg = 0
|
|
196
|
+
|
|
197
|
+
for i, score in enumerate(scores):
|
|
198
|
+
dcg += score / np.log2(i + 2)
|
|
199
|
+
|
|
200
|
+
return dcg
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# Relevance scores
|
|
204
|
+
relevance_scores = []
|
|
205
|
+
|
|
206
|
+
for doc in retrieved_documents:
|
|
207
|
+
if doc in relevant_documents:
|
|
208
|
+
relevance_scores.append(1)
|
|
209
|
+
else:
|
|
210
|
+
relevance_scores.append(0)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# Calculate DCG
|
|
214
|
+
dcg = calculate_dcg(relevance_scores)
|
|
215
|
+
|
|
216
|
+
# Calculate ideal DCG
|
|
217
|
+
k = len(retrieved_documents)
|
|
218
|
+
|
|
219
|
+
ideal_scores = [1] * min(len(relevant_documents), k)
|
|
220
|
+
ideal_scores += [0] * (k - len(ideal_scores))
|
|
221
|
+
|
|
222
|
+
idcg = calculate_dcg(ideal_scores)
|
|
223
|
+
|
|
224
|
+
# Calculate NDCG
|
|
225
|
+
ndcg = dcg / idcg if idcg > 0 else 0
|
|
226
|
+
|
|
227
|
+
print("Relevance Scores:", relevance_scores)
|
|
228
|
+
print("DCG:", round(dcg, 2))
|
|
229
|
+
print("IDCG:", round(idcg, 2))
|
|
230
|
+
print("NDCG:", round(ndcg, 2))
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
# User click data
|
|
236
|
+
|
|
237
|
+
version_A = [1, 0, 1, 0, 1, 0, 1, 0, 0, 1]
|
|
238
|
+
|
|
239
|
+
version_B = [1, 1, 1, 1, 0, 1, 1, 0, 1, 1]
|
|
240
|
+
|
|
241
|
+
# Calculate click-through rate
|
|
242
|
+
|
|
243
|
+
ctr_A = sum(version_A) / len(version_A)
|
|
244
|
+
|
|
245
|
+
ctr_B = sum(version_B) / len(version_B)
|
|
246
|
+
|
|
247
|
+
print("Version A CTR:", round(ctr_A * 100, 2), "%")
|
|
248
|
+
|
|
249
|
+
print("Version B CTR:", round(ctr_B * 100, 2), "%")
|
|
250
|
+
|
|
251
|
+
if ctr_B > ctr_A:
|
|
252
|
+
print("Version B performs better!")
|
|
253
|
+
else:
|
|
254
|
+
print("Version A performs better!")
|
|
255
|
+
|
|
256
|
+
"""
|
|
257
|
+
|
|
258
|
+
def show():
|
|
259
|
+
print(CODE)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
CODE = """
|
|
2
|
+
# ISR PRACTICAL 4
|
|
3
|
+
|
|
4
|
+
\n===== ISR PRACTICAL 4 =====\n
|
|
5
|
+
import cv2
|
|
6
|
+
import numpy as np
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
import os
|
|
9
|
+
import imagehash
|
|
10
|
+
from PIL import Image
|
|
11
|
+
|
|
12
|
+
os.makedirs("images", exist_ok=True)
|
|
13
|
+
|
|
14
|
+
# Create 5 sample images
|
|
15
|
+
colors = [(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]
|
|
16
|
+
|
|
17
|
+
for i, color in enumerate(colors):
|
|
18
|
+
img = np.zeros((200,200,3), dtype=np.uint8)
|
|
19
|
+
img[:] = color
|
|
20
|
+
cv2.circle(img, (100,100), 50, (255,255,255), -1)
|
|
21
|
+
cv2.imwrite(f"images/image{i+1}.jpg", img)
|
|
22
|
+
|
|
23
|
+
print("Images created successfully!")
|
|
24
|
+
|
|
25
|
+
# Display images in tile format
|
|
26
|
+
plt.figure(figsize=(10,6))
|
|
27
|
+
|
|
28
|
+
for i in range(5):
|
|
29
|
+
img = cv2.imread(f"images/image{i+1}.jpg")
|
|
30
|
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
31
|
+
plt.subplot(2,3,i+1)
|
|
32
|
+
plt.imshow(img)
|
|
33
|
+
plt.title(f"Image {i+1}")
|
|
34
|
+
plt.axis("off")
|
|
35
|
+
|
|
36
|
+
plt.tight_layout()
|
|
37
|
+
plt.show()
|
|
38
|
+
|
|
39
|
+
# Color histogram
|
|
40
|
+
img = cv2.imread("images/image1.jpg")
|
|
41
|
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
|
42
|
+
hist = cv2.calcHist([hsv], [0], None, [256], [0,256])
|
|
43
|
+
|
|
44
|
+
plt.plot(hist)
|
|
45
|
+
plt.title("Color Histogram")
|
|
46
|
+
plt.show()
|
|
47
|
+
|
|
48
|
+
# CBIR using ImageHash
|
|
49
|
+
query = imagehash.average_hash(Image.open("images/image1.jpg"))
|
|
50
|
+
|
|
51
|
+
print("Similar Images:")
|
|
52
|
+
|
|
53
|
+
for i in range(5):
|
|
54
|
+
path = f"images/image{i+1}.jpg"
|
|
55
|
+
h = imagehash.average_hash(Image.open(path))
|
|
56
|
+
|
|
57
|
+
if query - h < 15:
|
|
58
|
+
print(f"Image {i+1} is similar")
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def show():
|
|
62
|
+
print(CODE)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
CODE = """
|
|
2
|
+
# ISR PRACTICAL 5
|
|
3
|
+
|
|
4
|
+
import requests
|
|
5
|
+
city = input("Enter city name: ")
|
|
6
|
+
# Get city coordinates
|
|
7
|
+
url = "https://geocoding-api.open-meteo.com/v1/search"
|
|
8
|
+
data = requests.get(url, params={"name": city, "count": 1}).json()
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
if "results" in data:
|
|
13
|
+
place = data["results"][0]
|
|
14
|
+
# Get weather
|
|
15
|
+
url = "https://api.open-meteo.com/v1/forecast"
|
|
16
|
+
params = { "latitude": place["latitude"],
|
|
17
|
+
"longitude": place["longitude"],
|
|
18
|
+
"current": "temperature_2m,wind_speed_10m,weather_code" }
|
|
19
|
+
weather = requests.get(url, params=params).json()["current"]
|
|
20
|
+
codes = { 0: "Clear sky",
|
|
21
|
+
1: "Mainly clear",
|
|
22
|
+
2: "Partly cloudy",
|
|
23
|
+
3: "Cloudy",
|
|
24
|
+
45: "Foggy",
|
|
25
|
+
61: "Rain" }
|
|
26
|
+
print("City:", place["name"])
|
|
27
|
+
print("Temperature:", weather["temperature_2m"], "°C")
|
|
28
|
+
print("Wind Speed:", weather["wind_speed_10m"], "km/h")
|
|
29
|
+
print("Description:", codes.get(weather["weather_code"], "Other conditions"))
|
|
30
|
+
else: print("City not found!")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
!pip install requests beautifulsoup4
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
import requests
|
|
40
|
+
from bs4 import BeautifulSoup
|
|
41
|
+
from urllib.parse import urljoin
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
url = "https://books.toscrape.com/"
|
|
45
|
+
# Fetch website
|
|
46
|
+
response = requests.get(url)
|
|
47
|
+
|
|
48
|
+
soup = BeautifulSoup(response.text, "html.parser")
|
|
49
|
+
|
|
50
|
+
# Extract product details
|
|
51
|
+
products = soup.find_all("article", class_="product_pod")
|
|
52
|
+
print("Product Details:\n")
|
|
53
|
+
|
|
54
|
+
for product in products:
|
|
55
|
+
name = product.h3.a["title"]
|
|
56
|
+
price = product.find("p", class_="price_color").text
|
|
57
|
+
link = urljoin(url, product.h3.a["href"])
|
|
58
|
+
print("Product:", name)
|
|
59
|
+
print("Price:", price)
|
|
60
|
+
print("Link:", link)
|
|
61
|
+
print("--------------------")
|
|
62
|
+
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def show():
|
|
66
|
+
print(CODE)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
CODE = """
|
|
2
|
+
# ISR PRACTICAL 6
|
|
3
|
+
|
|
4
|
+
\n===== ISR PRACTICAL 6 =====\n
|
|
5
|
+
import os
|
|
6
|
+
from bs4 import BeautifulSoup
|
|
7
|
+
|
|
8
|
+
# Create website folder
|
|
9
|
+
os.makedirs("website", exist_ok=True)
|
|
10
|
+
|
|
11
|
+
# Create HTML pages
|
|
12
|
+
pages = {
|
|
13
|
+
"python.html": "<h1>Python</h1><p>Python is a programming language.</p>",
|
|
14
|
+
"java.html": "<h1>Java</h1><p>Java is used for application development.</p>",
|
|
15
|
+
"ai.html": "<h1>Artificial Intelligence</h1><p>AI uses machine learning.</p>"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for name, content in pages.items():
|
|
19
|
+
with open("website/" + name, "w") as f:
|
|
20
|
+
f.write(content)
|
|
21
|
+
|
|
22
|
+
# Search engine
|
|
23
|
+
keyword = input("Enter search keyword: ").lower()
|
|
24
|
+
|
|
25
|
+
print("\nSearch Results:")
|
|
26
|
+
found = False
|
|
27
|
+
|
|
28
|
+
for file in os.listdir("website"):
|
|
29
|
+
with open("website/" + file) as f:
|
|
30
|
+
soup = BeautifulSoup(f, "html.parser")
|
|
31
|
+
text = soup.get_text().lower()
|
|
32
|
+
|
|
33
|
+
if keyword in text:
|
|
34
|
+
print("Page:", file)
|
|
35
|
+
print("Title:", soup.h1.text)
|
|
36
|
+
print("Link:", "website/" + file)
|
|
37
|
+
print("----------------")
|
|
38
|
+
found = True
|
|
39
|
+
|
|
40
|
+
if not found:
|
|
41
|
+
print("No results found!")
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def show():
|
|
45
|
+
print(CODE)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: numpi-isr
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: My Personal ISR Practical Python Library
|
|
5
|
+
Author: Senorita
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/plain
|
|
8
|
+
Dynamic: author
|
|
9
|
+
Dynamic: description
|
|
10
|
+
Dynamic: description-content-type
|
|
11
|
+
Dynamic: requires-python
|
|
12
|
+
Dynamic: summary
|
|
13
|
+
|
|
14
|
+
A collection of six ISR practical programs.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
setup.py
|
|
3
|
+
numpi/__init__.py
|
|
4
|
+
numpi/pr1.py
|
|
5
|
+
numpi/pr2.py
|
|
6
|
+
numpi/pr3.py
|
|
7
|
+
numpi/pr4.py
|
|
8
|
+
numpi/pr5.py
|
|
9
|
+
numpi/pr6.py
|
|
10
|
+
numpi_isr.egg-info/PKG-INFO
|
|
11
|
+
numpi_isr.egg-info/SOURCES.txt
|
|
12
|
+
numpi_isr.egg-info/dependency_links.txt
|
|
13
|
+
numpi_isr.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numpi
|
numpi_isr-1.0.0/setup.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="numpi-isr",
|
|
5
|
+
version="1.0.0",
|
|
6
|
+
description="My Personal ISR Practical Python Library",
|
|
7
|
+
long_description="A collection of six ISR practical programs.",
|
|
8
|
+
long_description_content_type="text/plain",
|
|
9
|
+
author="Senorita",
|
|
10
|
+
packages=find_packages(),
|
|
11
|
+
python_requires=">=3.8"
|
|
12
|
+
)
|