Complete Python Notes
Complete Python Notes
= Number Separate Strings and Integers from a List Check if a Sentence is Pangram
from math import gcd def is_sum_of_cubes_equal(number): def separate_strings_and_integers(lst): import string
num = int(input("Enter an integer: ")) num_str = str(number) strings = [item for item in lst if isinstance(item, str)] def is_pangram(sentence):
relative_primes = {i for i in range(1, num) if gcd(i, num) == 1} alternate_digits = [int(num_str[i]) for i in range(0, len(num_str), 2)] integers = [item for item in lst if isinstance(item, int)] return
print(f"Relative primes of {num} are: {relative_primes}") sum_of_cubes = sum(d**3 for d in alternate_digits) return strings, integers set(string.ascii_lowercase).issubset(set([Link]()))
return sum_of_cubes == number lst = ['hello', 42, 'world', 10, 5, 'Python'] print(is_pangram("The quick brown fox jumps over the lazy dog"))
Sum of Digits def find_special_numbers(start, end): strings, integers = separate_strings_and_integers(lst) # Output: True
def sum_of_digits(n): special_numbers = [num for num in range(start, end + 1) if print("Strings:", strings) # Output: ['hello', 'world', 'Python'] print(is_pangram("Hello world")) # Output: False
return sum(int(digit) for digit in str(abs(n))) is_sum_of_cubes_equal(num)] print("Integers:", integers) # Output: [42, 10, 5]
n = int(input("Enter an integer: ")) return special_numbers Numpy Array with Random Integers and Average of Diagonals
print("Sum of digits:", sum_of_digits(n)) List Processing (Integer Sum and String Concatenation) n = int(input("Enter the size of the matrix (n x n): "))
Class to Return Primes Between Two Numbers L1 = ["Aligarh", 10, "Muslim", 54, 90, "University"] Create n x n matrix with random integers between 1 and 100
Add Arbitrary Integers class Prime: integers = [x for x in L1 if isinstance(x, int)] matrix = [Link](1, 101, size=(n, n))
def add_integers(*args): @staticmethod strings = [x for x in L1 if isinstance(x, str)] print("Matrix:")
return sum(args) def get_primes(start, end): II = sum(integers) print(matrix)
nums = list(map(int, input("Enter integers separated by space: primes = [] S1 = "".join(strings) main_diag_avg = [Link]([Link](matrix))
").split())) for num in range(start, end + 1): print("Sum of integers (II):", II) anti_diag_avg = [Link]([Link]([Link](matrix)))
print("Sum:", add_integers(*nums)) if num > 1: print("Concatenated string (S1):", S1) print(f"Average of main diagonal: {main_diag_avg}")
for i in range(2, int(num**0.5) + 1): print(f"Average of anti-diagonal: {anti_diag_avg}")
Find Prime Numbers Between Two Numbers if num % i == 0: Merge and Sort Two Lists
def primes_between(start, end): break def merge_and_sort(list1, list2): Extract Even Rows and Odd Columns from a NumPy Array
return [x for x in range(start, end + 1) if is_prime(x)] else: return sorted(list1 + list2) import numpy as np
start = int(input("Enter start: ")) [Link](num) print(merge_and_sort([3, 1, 4], [2, 5, 0])) array = [Link](1, 26).reshape(5, 5)
end = int(input("Enter end: ")) return primes # Output: [0, 1, 2, 3, 4, 5] even_rows = array[1::2] # Rows with index 1, 3, etc. (even rows in
print("Primes:", primes_between(start, end)) print(Prime.get_primes(10, 30)) # Output: [11, 13, 17, 19, 23, 29] 0-based index)
Create 2 List Based on User-Supplied List of Distinct Integers result = even_rows[:, ::2] # Odd columns in 0-based index
Check if Integer is Prime Python Program for Subset Creation def create_two_lists(): print("Original Array:")
def is_prime(n): original_set = {2, 3, 4, 5, 6, 7, 8, 9} user_list = list(map(int, input("Enter an even number of distinct print(array)
if n <= 1: even_subset = {x for x in original_set if x % 2 == 0} integers separated by space: ").split())) print("\nArray of Even Rows and Odd Columns:")
return False odd_subset = {x for x in original_set if x % 2 != 0} if len(user_list) % 2 != 0: print(result)
for i in range(2, int(n**0.5) + 1): print("Even Subset:", even_subset) print("Error: List must have an even number of elements!")
if n % i == 0: print("Odd Subset:", odd_subset) return Average of the Second Column of a 2D Numpy Array
return False user_list.sort() import numpy as np
return True Splitting Even-Sized Set into Ordered Subsets mid_index = len(user_list) // 2 def average_second_column(array):
n = int(input("Enter an integer: ")) nums = list(map(int, input("Enter distinct integers separated by L1 = user_list[:mid_index] return [Link](array[:, 1])
print("Prime" if is_prime(n) else "Not Prime") spaces: ").split())) L2 = user_list[mid_index:] arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
if len(nums) % 2 != 0: print("First list (L1):", L1) print(average_second_column(arr))
Average and Standard Deviation print("Please enter an even number of integers.") print("Second list (L2):", L2) # Output: 5.0
import math else: create_two_lists()
def average_and_std(numbers): [Link]() Average of First 5 Rows in Third and Fourth Columns
avg = sum(numbers) / len(numbers) mid = len(nums) // 2 Remove Duplicate Items from a List def average_columns(array):
variance = sum((x - avg) ** 2 for x in numbers) / len(numbers) subset1 = nums[:mid] def remove_duplicates(lst): return [Link](array[:5, 2:4], axis=0)
std_dev = [Link](variance) subset2 = nums[mid:] return list(set(lst)) arr = [Link](1, 100, size=(10, 5))
return avg, std_dev print("First subset:", subset1) print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) print(average_columns(arr))
numbers = [float(input(f"Enter number {i+1}: ")) for i in range(5)] print("Second subset:", subset2) # Output: [1, 2, 3, 4, 5] # Output: [average of 3rd column, average of 4th column]
avg, std_dev = average_and_std(numbers)
print("Average:", avg) Process Integers and Strings Find Second Largest Number and Its Indices Row-wise Sum of a 2D Numpy Array
print("Standard Deviation:", std_dev) L1 = ["Aligarh", 10, " Muslim", 54, 90, " University"] def second_largest(lst): def row_wise_sum(array):
II = sum(x for x in L1 if isinstance(x, int)) unique_sorted = sorted(set(lst), reverse=True) return [Link](array, axis=1)
Compute Algebraic Expression SI = "".join(x for x in L1 if isinstance(x, str)) second = unique_sorted[1] if len(unique_sorted) > 1 else None arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
def compute_expression(x, y): print("II:", II) print("SI:", SI) indices = [i for i, x in enumerate(lst) if x == second] print(row_wise_sum(arr))
return (1 + 2 + x * y**2 + 2 + y * x) return second, indices # Output: [6, 15, 24]
x = float(input("Enter value for x: ")) String Processing lst = [4, 1, 5, 3, 5, 2, 5]
y = float(input("Enter value for y: ")) def process_string(input_string): second, indices = second_largest(lst) Maximum Value in Each Row of a 2D Numpy Array
print("Result:", compute_expression(x, y)) if len(input_string) < 2: print("Second Largest:", second) # Output: 4 def max_in_rows(array):
return "" print("Indices:", indices) # Output: [0] return [Link](array, axis=1)
3. Multiply an Integer by 2 Using Bitwise Operator else: arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
def multiply_by_2(n): return input_string[-2:] Sort a List by Length of Elements print(max_in_rows(arr)) # Output: [3, 6, 9]
return n << 1 sample = "Uttar Pradesh" def sort_by_length(lst):
n = int(input("Enter an integer: ")) result = process_string(sample) return sorted(lst, key=len) Maximum of Average Value in Each Row
print("Result after multiplying by 2:", multiply_by_2(n)) print("Expected Result:", result) print(sort_by_length(["apple", "kiwi", "banana", "cherry"])) def max_of_averages(array):
# Output: ['kiwi', 'apple', 'cherry', 'banana'] row_averages = [Link](array, axis=1)
Divide an Integer by 4 Using Bitwise Operator Count Digits and Alphabets in a String return [Link](row_averages)
def divide_by_4(n): def count_digits_and_alphabets(text): List of Tuples with Number and Its Square arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
return n >> 2 digits = sum([Link]() for c in text) def number_and_square(n): print(max_of_averages(arr)) # Output: 8.0
n = int(input("Enter an integer: ")) alphabets = sum([Link]() for c in text) return [(i, i**2) for i in range(1, n+1)]
print("Result after dividing by 4:", divide_by_4(n)) return digits, alphabets print(number_and_square(5)) Analysis of Cricket Data
print(count_digits_and_alphabets("Hello123")) # Output: (3, 5) # Output: [(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)] import pandas as pd
Separate Even and Odd Integers data = pd.read_csv("cric_data.csv")
def separate_even_odd(lst): Count Characters in Strings Find Element Occurring Odd Number of Times (i) Maximum matches won in 2012
evens = [x for x in lst if x % 2 == 0] def count_characters(*strings): from collections import Counter max_2012 = data[data['Year'] == 2012].iloc[:,
odds = [x for x in lst if x % 2 != 0] return sum(len(s) for s in strings) def find_odd_occurrence(lst): 1:].idxmax(axis=1).values[0]
return evens, odds strings = input("Enter strings separated by space: ").split() counts = Counter(lst) print("Team with maximum matches in 2012:", max_2012)
lst = [1, 2, 3, 4, 5, 6] print("Total characters:", count_characters(*strings)) return [x for x, count in [Link]() if count % 2 != 0] (ii) Maximum matches between 2015-2022
evens, odds = separate_even_odd(lst) print(find_odd_occurrence([1, 2, 3, 2, 3, 3, 4, 1, 1])) subset = data[(data['Year'] >= 2015) & (data['Year'] <= 2022)]
print("Evens:", evens) # Output: [2, 4, 6] Check if String is Palindrome # Output: [3, 4] total_wins = [Link][:, 1:].sum()
print("Odds:", odds) # Output: [1, 3, 5] def is_palindrome(s): team_max = total_wins.idxmax()
return s == s[::-1] Find Repeated Items in a Tuple print("Team with maximum matches between 2015-2022:",
Exchange Values Using Addition and Subtraction s = input("Enter a string: ") from collections import Counter team_max)
def exchange_values(a, b): print("Palindrome" if is_palindrome(s) else "Not Palindrome") def find_repeated_items(tup):
a=a+b return [item for item, count in Counter(tup).items() if count > 1] File Content Manipulation
b=a-b Extract First and Last 2 Characters print(find_repeated_items((1, 2, 2, 3, 4, 4, 5))) # Output: [2, 4] with open('[Link]', 'r') as f1, open('[Link]', 'r') as f2,
a=a-b def extract_string(s): open('file_main.txt', 'w') as main:
return a, b return s[:2] + s[-2:] if len(s) >= 4 else "" Find the Index of an Item in a Tuple content1 = [Link]()
a = int(input("Enter first integer: ")) s = input("Enter a string: ") def find_index(tup, item): content2 = [Link]()
b = int(input("Enter second integer: ")) result = extract_string(s) return [Link](item) if item in tup else -1 half_len = len(content1) // 2
a, b = exchange_values(a, b) print("Result:", result if result else "Empty String") print(find_index((1, 2, 3, 4), 3)) # Output: 2 [Link](content1[:half_len])
print("After exchange:", a, b) [Link](content2[len(content2)//2:])
Length of Longest Word Remove Empty Tuples from a List
Value of 3<<3^2&1+3 def longest_word_length(words): def remove_empty_tuples(lst): Count Lines, Words, Characters, Alphabets, & Digits in a File
result = (3 << 3) ^ (2 & 1) + 3 return max(len(word) for word in words) return [tup for tup in lst if tup] def analyze_file(file_path):
print("Result:", result) # Output: 27 print(longest_word_length(["apple", "banana", "cherry", "kiwi"])) print(remove_empty_tuples([(1, 2), (), (3, 4), ()])) with open(file_path, 'r') as file:
# Output: 6 # Output: [(1, 2), (3, 4)] text = [Link]()
Check Even or Odd Using Bitwise Operator lines = [Link]()
def is_even(n): Count Words in a Line of Text Character Count and Maximum Instances words = [Link]()
return n & 1 == 0 def count_words(line): import Counter characters = len(text)
n = int(input("Enter an integer: ")) return len([Link]()) text = input("Enter a line of text: ") alphabets = sum([Link]() for c in text)
print("Even" if is_even(n) else "Odd") print(count_words("This is a sample line of text.")) # Output: 7 char_count = Counter(text) digits = sum([Link]() for c in text)
max_char = max(char_count, key=char_count.get) return len(lines), len(words), characters, alphabets, digits
Find the Maximum of Three Numbers Splitting Words Based on Average Length print("Character count:", dict(char_count)) print(analyze_file('[Link]')
def max_of_three(a, b, c): words = input("Enter words separated by spaces: ").split() print(f"The character '{max_char}' has the maximum instances
return max(a, b, c) avg_length = sum(len(word) for word in words) / len(words) ({char_count[max_char]}).") Find Word with Maximum Instances & Replace with "Aligarh"
a, b, c = [int(input(f"Enter number {i+1}: ")) for i in range(3)] short_words = [word for word in words if len(word) <= avg_length] def replace_max_word(file_name):
print("Maximum:", max_of_three(a, b, c)) long_words = [word for word in words if len(word) > avg_length] Count Sentences, Words with Maximum Occurrence with open(file_name, 'r') as file:
print("Words with length <= average length:", short_words) from collections import Counter text = [Link]()
Distance Between Two Points print("Words with length > average length:", long_words) def analyze_text(text): words = [Link]()
import math sentences = [Link]('.') word_count = {}
def distance(x1, y1, x2, y2): Python Program for Vowel Counts words = [Link]() for word in words:
return [Link]((x2 - x1)**2 + (y2 - y1)**2) def vowel_analysis(text): wordcounts = Counter(words) word_count[word] = word_count.get(word, 0) + 1
x1, y1 = map(float, input("Enter coordinates of point 1 (x1, y1): vowels = "aeiouAEIOU" mostcommonword, frequency = [Link](1)[0] max_count = max(word_count.values())
").split()) count = {v: [Link](v) for v in vowels if v in text} return len(sentences)-1,len(words), mostcommonword, frequency max_words = [word for word, count in word_count.items() if
x2, y2 = map(float, input("Enter coordinates of point 2 (x2, y2): max_vowel = max(count, key=[Link]) text = "This is a sentence. This is another sentence. The word count == max_count]
").split()) return count, max_vowel sentence occurs frequently." for word in max_words:
print("Distance:", distance(x1, y1, x2, y2)) text = "This is an example text with some vowels." num_sentences, num_words, max_word, max_count = text = [Link](word, "Aligarh")
vowel_count, max_vowel = vowel_analysis(text) analyze_text(text) with open(file_name, 'w') as file:
print("Vowel Counts:", vowel_count) print("Number of Sentences:", num_sentences) # Output: 3 [Link](text)
Compute Factorial
print("Vowel with Maximum Instances:", max_vowel) print("Number of Words:", num_words) # Output: 12 print(f"Word(s) replaced: {max_words}")
def factorial(n):
print("Most Frequent Word:", max_word) # Output: "sentence" print(f"Maximum count: {max_count}")
return 1 if n == 0 else n * factorial(n - 1)
Modify a String print("Frequency:", max_count) # Output: 3 replace_max_word('[Link]')
n = int(input("Enter an integer: ")) print("Factorial:", factorial(n))
def modify_string(s):
if len(s) >= 3 and [Link]('e'): Frequency of Each Character in a Line of Text Find Alphabet(s) with Maximum Instances in a File
Exchange Values of Two Variables Using XOR
return s[:-1] + 'en' from collections import Counter def find_max_alphabet_instances(file_name):
def exchange_values(a, b):
return s def char_frequency(text): alphabet_count = {}
a=a^b
print(modify_string('Give')) # Output: 'Given' return dict(Counter(text)) with open(file_name, 'r') as file:
b=a^b
print(modify_string('take')) # Output: 'taken' print(char_frequency("hello world")) for line in file:
a=a^b
print(modify_string('ta')) # Output: 'ta' # Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1} for char in line:
return a, b
if [Link]():
a = int(input("Enter first integer: "))
Sort Words in Alphabetic Order Character with Maximum Count in a Line of Text alphabet_count[char] =alphabet_count.get(char, 0)+ 1
b = int(input("Enter second integer: "))
def sort_words(text): def max_char_count(text): max_count = max(alphabet_count.values())
a, b = exchange_values(a, b)
words = [Link]() char_counts = Counter(text) max_alphabets = [char for char, count in alphabet_count.items()
print("After exchange:", a, b)
[Link]() return max(char_counts, key=char_counts.get) if count == max_count]
return " ".join(words) print(max_char_count("hello world")) # Output: 'l' print("Alphabet(s) with maximum instances:", max_alphabets)
print(sort_words("this is a test of sorting words")) print("Maximum count:", max_count)
find_max_alphabet_instances('[Link]')
Split a String into Numbers and Alphabets INTO lists Swap Content Between Two Files Data Science Problems df = load_and_clean_data(file_name
df = add_x7_feature(df)
def split_string(input_string): This program swaps the middle line of the first file with the a. Correlation Heatmap and Model Training
X, y = scale_features(df, target_column='y')
L1 = [int(char) for char in input_string if [Link]()] last line of the second file: import pandas as pd
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
import seaborn as sns
L2 = [char for char in input_string if [Link]()] def swap_lines(file1, file2): random_state=42)
import [Link] as plt
print("List of numbers (L1):", L1) with open(file1, 'r') as f1: from sklearn.model_selection import train_test_split
results = train_and_evaluate(X_train, X_test, y_train, y_test)
for model_name, metrics in [Link]():
print("List of alphabets (L2):", L2) lines1 = [Link]() from [Link] import StandardScaler
print(f"{model_name} - Accuracy: {metrics['accuracy']:.2f}, F1 Score:
input_string = "a1b2c3d4e5" middle_line_index = len(lines1) // 2 from sklearn.linear_model import LogisticRegression
{metrics['f1_score']:.2f}")
from [Link] import KNeighborsClassifier
split_string(input_string) middle_line = lines1[middle_line_index] plot_confusion_matrices(results)
from [Link] import accuracy_score, f1_score
with open(file2, 'r') as f2: main()
# Load your dataset DS
Explanation
Finding a Word in Two Text Files lines2 = [Link]() # Example: DS = pd.read_csv("[Link]")
Step a: Clean Independent Features: The program performs one-hot encoding for
def find_word_in_file(word, file_name): last_line = lines2[-1] # Assuming DS has columns ['x1', 'x2', 'x3', 'x4', 'x5', 'y']
the nominal features x1 and x2, creating binary columns for each category.
# Correlation heatmap
try: with open(file_name, 'r') as file: lines1[middle_line_index] = last_line Step b: Add Feature x7: Adds a new column x7 with random values between 0
corr_matrix = DS[['x1', 'x2', 'x3', 'x4', 'x5']].corr()
contents = [Link]() lines2[-1] = middle_line [Link](corr_matrix, annot=True, cmap="coolwarm")
and 1 using [Link]().
Step c: Perform Scaling: Uses MinMaxScaler to scale all features to a range of [0,
if word in contents: with open(file1, 'w') as f1: [Link]("Correlation Heatmap")
1].
print(f"The word '{word}' is found in {file_name}.") [Link](lines1) [Link]()
Step d: Train and Evaluate Models: Trains the dataset using Logistic Regression,
# Scaling the features
else: with open(file2, 'w') as f2: Decision Tree, and Random Forest. Evaluates each model on accuracy and F1
X = DS[['x1', 'x2', 'x3', 'x4', 'x5']]
print(f"The word '{word}' is NOT found in {file_name}.") [Link](lines2) y = DS['y']
score.
Step e: Draw Confusion Matrices: Creates heatmaps of confusion matrices for
except FileNotFoundError: swap_lines('[Link]', '[Link]') scaler = StandardScaler()
each model using seaborn.
print(f"The file {file_name} does not exist.") X_scaled = scaler.fit_transform(X)
Sample Output (Hypothetical)
# Train-test split
find_word_in_file("ces", "file_a.txt") Find Number of Courses Against Each Program X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3,
Model Performance:
find_word_in_file("stance", "file_b.txt") def count_courses(file_name): Logistic Regression - Accuracy: 0.85, F1 Score: 0.83
random_state=42)
Decision Tree - Accuracy: 0.80, F1 Score: 0.78
program_courses = {} # Train models
Random Forest - Accuracy: 0.88, F1 Score: 0.86
with open(file_name, 'r') as file: # Logistic Regression
Find Vowels with Maximum Instances in a File lr = LogisticRegression()
Confusion Matrices: Displays heatmaps for all three models.
def find_max_vowel_instances(file_name): for line in file: [Link](X_train, y_train)
vowels = "aeiouAEIOU" program, course = [Link]().split(',') y_pred_lr = [Link](X_test) Consider two features x and y based on the following
vowel_count = {v: 0 for v in vowels} if program not in program_courses: # K-Nearest Neighbors function: y = x12 + 3x2 + c, where c can be prepared based on
program_courses[program] = 0 knn = KNeighborsClassifier()
with open(file_name, 'r') as file: 1000 random values between 0 and 1 Now generate 1000
[Link](X_train, y_train)
for line in file: program_courses[program] += 1 y_pred_knn = [Link](X_test) random values between 0 and 1 for x1 and x2. Calculate y
for char in line: for program, count in program_courses.items(): # Evaluate models based on above function. Now train Polynomial Regression
if char in vowels: print(f"Program: {program}, Number of courses: {count}") accuracy_lr = accuracy_score(y_test, y_pred_lr) model and check the score for the same.
f1_lr = f1_score(y_test, y_pred_lr)
vowel_count[char] += 1 import numpy as np
accuracy_knn = accuracy_score(y_test, y_pred_knn) import pandas as pd
max_count = max(vowel_count.values()) Add HRA Column to an Employee File f1_knn = f1_score(y_test, y_pred_knn) from sklearn.model_selection import train_test_split
max_vowels = [v for v, count in vowel_count.items() if count == import csv print(f"Logistic Regression: Accuracy = {accuracy_lr}, F1 Score = {f1_lr}") from [Link] import PolynomialFeatures
max_count] def add_hra_column(file_name): print(f"KNN: Accuracy = {accuracy_knn}, F1 Score = {f1_knn}") from sklearn.linear_model import LinearRegression
print("Vowels with maximum instances:", max_vowels) updated_data = [] b. MNIST Dataset Training from [Link] import r2_score
For the MNIST problem, you'd typically use a library like TensorFlow or PyTorch. [Link](42) # For reproducibility
print("Maximum count:", max_count) with open(file_name, 'r') as file:
Let me know if you want an example for that! x1 = [Link](1000)
find_max_vowel_instances('[Link]') csv_reader = [Link](file) x2 = [Link](1000)
headers = next(csv_reader) c = [Link](1000)
Implementing a Custom Random Forest Model
Read Text File with Alphabets & Numbers [Link]('HRA') # Add HRA column to headers import numpy as np
y = (x1**2) + (3 * x2) + c
updated_data.append(headers) X = np.column_stack((x1, x2))
import re from [Link] import make_classification
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
def process_text_file(file_name): for row in csv_reader: from [Link] import DecisionTreeClassifier
random_state=42)
salary = float(row[2]) # Assuming salary is in the 3rd column from sklearn.model_selection import train_test_split
with open(file_name, 'r') as file: from [Link] import accuracy_score
poly = PolynomialFeatures(degree=2) # Quadratic terms since x1^2 is involved
content = [Link]() hra = round(0.18 * salary, 2) X, y = make_classification(n_samples=1000, n_features=5, n_classes=2,
X_train_poly = poly.fit_transform(X_train)
[Link](hra) # Add HRA column X_test_poly = [Link](X_test)
numbers = list(map(int, [Link](r'\b\d+\b', content))) random_state=42)
# Train Polynomial Regression model
special_chars = [Link](r'[^\w\s]', content) updated_data.append(row) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
model = LinearRegression()
with open(file_name, 'w', newline='') as file: random_state=42)
def is_armstrong(num): [Link](X_train_poly, y_train)
def train_decision_trees(n_trees, voting_type="majority"):
digits = [int(d) for d in str(num)] csv_writer = [Link](file) predictions = []
# Predict and evaluate the model
csv_writer.writerows(updated_data) y_pred_train = [Link](X_train_poly)
return sum(d ** len(digits) for d in digits) == num for _ in range(n_trees):
y_pred_test = [Link](X_test_poly)
armstrong_numbers = [num for num in numbers if print("HRA column added successfully.") tree = DecisionTreeClassifier(max_depth=3,
train_score = r2_score(y_train, y_pred_train)
random_state=[Link](1000))
is_armstrong(num)] [Link](X_train, y_train)
test_score = r2_score(y_test, y_pred_test)
special_count = {} Program for Linear Regression print(f"R2 Score on Training Data: {train_score:.4f}")
pred = [Link](X_test)
print(f"R2 Score on Testing Data: {test_score:.4f}")
for char in special_chars: from sklearn.linear_model import LinearRegression [Link](pred)
Explanation of the Code
special_count[char] = special_count.get(char, 0) + 1 from [Link] import r2_score predictions = [Link](predictions).T
Generate Random Data: x1, x2, and c are generated as random values between
if voting_type == "majority":
print(f"Numbers: {numbers}") [Link](42) final_prediction = [Link]([[Link](row).argmax() for row in predictions])
0 and 1 using [Link](). y is computed based on the given function:
print(f"Armstrong Numbers: {armstrong_numbers}") x = [Link](1000).reshape(-1, 1) # 1000 random values y=x12+3⋅x2+cy = x1^2 + 3 \cdot x2 + cy=x12+3⋅x2+c
elif voting_type == "minority":
Prepare Feature Matrix (X) and Target (y): Combine x1 and x2 into a single matrix
print(f"Total Characters: {len(content)}") for x final_prediction = [Link]([[Link](row).argmin() for row in predictions])
X.
print(f"Frequency of Special Characters: {special_count}") k = [Link](1000) # Random values for k return final_prediction
Polynomial Features: Since the function involves x12x1^2x12, polynomial
majority_voting_output = train_decision_trees(5, voting_type="majority")
y = 3 * [Link]() + k # y = 3x + k minority_voting_output = train_decision_trees(5, voting_type="minority")
features are created using PolynomialFeatures with degree 2.
Find Vowels & Special Characters with Min. No. of Instances X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, Train-Test Split: The dataset is split into training (80%) and testing (20%) sets
final_predictions = [Link]([
using train_test_split.
import string random_state=42) [Link]([majority_voting_output[i], minority_voting_output[i]]).argmax()
Train Polynomial Regression Model: A LinearRegression model is trained using
lin_reg = LinearRegression() for i in range(len(y_test))])
def find_min_vowels_special(file_name): the polynomial features.
accuracy = accuracy_score(y_test, final_predictions)
vowels = set("aeiouAEIOU") lin_reg.fit(X_train, y_train) print(f"Final Random Forest Accuracy: {accuracy}").
Evaluate the Model: The R2R^2R2 score is calculated for both training and testing
y_pred_train = lin_reg.predict(X_train) sets to measure the model's performance.
special_chars = set([Link])
R2 Score on Training Data: 0.9998
vowels_count = {v: 0 for v in vowels} y_pred_test = lin_reg.predict(X_test)
MNIST Image Augmentation and Training R2 Score on Testing Data: 0.9997
special_count = {s: 0 for s in special_chars} train_score = r2_score(y_train, y_pred_train) from [Link] import mnist
with open(file_name, 'r') as file: test_score = r2_score(y_test, y_pred_test) from [Link] import shift
Consider MNIST dataset that contains 70,000 small square
for line in file: print(f"R2 Score on Training Data: {train_score:.4f}") from [Link] import RandomForestClassifier
from sklearn.linear_model import SGDClassifier 28×28-pixel grayscale images of handwritten single digits
for char in line: print(f"R2 Score on Testing Data: {test_score:.4f}")
from [Link] import accuracy_score between 0 and 9. Train MNIST dataset using any model that
if char in vowels: [Link](X_test, y_test, color="blue", label="Actual") (X_train, y_train), (X_test, y_test) = mnist.load_data() you studied to classify a given image into digit 8 (Binary
vowels_count[char] += 1 [Link](X_test, y_pred_test, color="red", label="Predicted") def shift_image(image, dx, dy):
classification problem). Discuss its performance also.
elif char in special_chars: [Link]("Linear Regression: Actual vs Predicted") return shift(image, [dx, dy], cval=0)
X_train_augmented = [] Logistic Regression as the model for simplicity and discuss
special_count[char] += 1 [Link]("x")
y_train_augmented = [] its performance.
min_vowel = min(vowels_count, key=vowels_count.get) [Link]("y") for image, label in zip(X_train, y_train): import numpy as np
min_special = min(special_count, key=special_count.get) [Link]() for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)): import [Link] as plt
print(f"Vowel with minimum instances: '{min_vowel}' [Link]() X_train_augmented.append(shift_image(image,dx, dy).flatten()) from sklearn.linear_model import LogisticRegression
y_train_augmented.append(label) from [Link] import accuracy_score, f1_score, confusion_matrix,
({vowels_count[min_vowel]})") rf = RandomForestClassifier() classification_report
print(f"Special character with minimum instances: Linear Regression Using Normal Equation [Link](X_train_augmented, y_train_augmented) from sklearn.model_selection import train_test_split
'{min_special}' ({special_count[min_special]})") import numpy as np y_pred_rf = [Link](X_test.reshape(len(X_test), -1)) from [Link] import fetch_openml
find_min_vowels_special("[Link]") from [Link] import r2_score print("Random Forest Accuracy:", accuracy_score(y_test, y_pred_rf)) import seaborn as sns
mnist = fetch_openml('mnist_784', version=1)
x = [Link](1000) X, y = [Link], [Link](int)
Merge Content of Employees and Departments Files c=3 You have been given a dataset [Link] having independent y_binary = (y == 8).astype(int) # 1 if digit is 8, else 0
def merge_employee_department(emp_file, dept_file, output_file): y = 3*x + c + [Link](1000) features as x1, x2, x3, x4, x5, x6, x7 and dependent feature as X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2,
departments = {} X = np.c_[[Link]([Link][0]), x] # Adding bias term y with value either 0 or 1. All independent features are random_state=42)
with open(dept_file, 'r') as dept_csv: theta = [Link](X.T @ X) @ X.T @ y continuous data except x1 and x2, which are having nominal log_reg = LogisticRegression(max_iter=1000, solver='lbfgs')
log_reg.fit(X_train, y_train)
csv_reader = [Link](dept_csv) # Predictions and score data. write python program for following:a. Clean independent y_pred = log_reg.predict(X_test)
next(csv_reader) # Skip header y_pred = X @ theta features b. Add one more feature x7 having values between 0 accuracy = accuracy_score(y_test, y_pred)
for row in csv_reader: print("R2 Score:", r2_score(y, y_pred)) and 1. c. Perform scaling d. Train this dataset using Logistic f1 = f1_score(y_test, y_pred)
departments[row[0]] = {'DName': row[1], 'DLocation': regression, Decision Tree and Random Forest. Compare the conf_matrix = confusion_matrix(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")
row[2]} # DID -> {DName, DLocation} Logistic Regression, Decision Tree, and Random Forest performance of all the models based on accuracy and F1 print(f"F1 Score: {f1:.4f}")
merged_data = [] import pandas as pd score. e. Draw confusion matrix of each model print("\nClassification Report:\n", classification_report(y_test, y_pred))
import pandas as pd [Link](figsize=(6, 6))
with open(emp_file, 'r') as emp_csv: import seaborn as sns import numpy as np [Link](conf_matrix, annot=True, fmt='d', cmap="Blues", cbar=False)
csv_reader = [Link](emp_csv) from [Link] import SimpleImputer from sklearn.model_selection import train_test_split [Link]("Confusion Matrix")
headers = next(csv_reader) + ['DName', 'DLocation'] # Add from [Link] import StandardScaler, from [Link] import OneHotEncoder, MinMaxScaler [Link]("Predicted")
department fields to headers OneHotEncoder from sklearn.linear_model import LogisticRegression [Link]("Actual")
from [Link] import DecisionTreeClassifier [Link]()
merged_data.append(headers) from sklearn.model_selection import train_test_split from [Link] import RandomForestClassifier def plot_digits(images, predictions, labels, n=5):
for row in csv_reader: from sklearn.linear_model import LogisticRegression from [Link] import accuracy_score, f1_score, confusion_matrix [Link](figsize=(12, 6))
did = row[3] # DID from [Link] import DecisionTreeClassifier import seaborn as sns for i in range(n):
if did in departments: from [Link] import RandomForestClassifier import [Link] as plt [Link](1, n, i + 1)
def load_and_clean_data(file_name): [Link](images[i].reshape(28, 28), cmap="gray")
merged_row = row + [departments[did]['DName'], from [Link] import accuracy_score, f1_score, df = pd.read_csv(file_name [Link](f"Pred: {predictions[i]} (True: {labels[i]})")
departments[did]['DLocation']] confusion_matrix encoder = OneHotEncoder(sparse=False, drop='first') # Drop first to avoid [Link]("off")
merged_data.append(merged_row) import [Link] as plt multicollinearity plt.tight_layout()
with open(output_file, 'w', newline='') as out_csv: data = pd.read_csv('[Link]') nominal_data = encoder.fit_transform(df[['x1', 'x2']]) [Link]()
nominal_columns = encoder.get_feature_names_out(['x1', 'x2']) sample_images = X_test[:5]
csv_writer = [Link](out_csv) data['x3'].fillna(data['x3'].mean(), inplace=True) nominal_df = [Link](nominal_data, columns=nominal_columns) sample_predictions = y_pred[:5]
csv_writer.writerows(merged_data) data['x4'].fillna(data['x4'].mean(), inplace=True) df = [Link](columns=['x1', 'x2']).reset_index(drop=True) sample_labels = y_test[:5]
print(f"Merged content written to {output_file}") data = pd.get_dummies(data, columns=['x5'], drop_first=True) df = [Link]([df, nominal_df], axis=1) plot_digits(sample_images, sample_predictions, sample_labels)
merge_employee_department('[Link]', '[Link]', scaler = StandardScaler() return df Loading the MNIST Dataset: We use fetch_openml from [Link] to load
def add_x7_feature(df): the MNIST dataset.
'merged_output.csv') scaled_features = scaler.fit_transform([Link]('y', axis=1)) [Link](42) # For reproducibility Binary Classification Problem: We create a binary target y_binary where 1
X_train, X_test, y_train, y_test = train_test_split(scaled_features, df['x7'] = [Link](len(df)) represents images of digit 8, and 0 represents all other digits.
Average Salary of Each Department(read data from 2 Files): data['y'], test_size=0.2) return df Splitting the Dataset: Split the data into training (80%) and testing (20%) sets.
models = { def scale_features(df, target_column): Model Selection: Logistic Regression is chosen as a simple linear model to
import csv
scaler = MinMaxScaler() distinguish between 8 and not 8.
def calculate_average_salary(employee_file, department_file): 'Logistic Regression': LogisticRegression(), X = [Link](columns=[target_column]) Model Evaluation: We calculate accuracy, F1 score, and display a confusion
departments = {} 'Decision Tree': DecisionTreeClassifier(), y = df[target_column] matrix along with a detailed classification report.
with open(department_file, 'r') as dept_file: 'Random Forest': RandomForestClassifier() } scaled_X = scaler.fit_transform(X) Visualizing Predictions: The program plots a few test images along with their
for name, model in [Link](): return [Link](scaled_X, columns=[Link]), y predicted labels.
csv_reader = [Link](dept_file)
def train_and_evaluate(X_train, X_test, y_train, y_test): Confusion Matrix: Rows: Actual, Columns: Predicted. The matrix confirms high
next(csv_reader) # Skip header [Link](X_train, y_train) models = { accuracy with a few false positives and false negatives.
for row in csv_reader: predictions = [Link](X_test) "Logistic Regression": LogisticRegression(), Visualization of Predictions: Displays a set of test images along with predicted
departments[row[0]] = row[1] # DID -> DName acc = accuracy_score(y_test, predictions) "Decision Tree": DecisionTreeClassifier(), and actual labels.
f1 = f1_score(y_test, predictions) "RandomForest": RandomForestClassifier(n_estimators=100, Performance Discussion
department_salaries = {}
random_state=42) } Accuracy: Logistic Regression achieves 98.6% accuracy, indicating excellent
department_counts = {} print(f"{name} - Accuracy: {acc}, F1 Score: {f1}") results = {} performance for this binary classification task.
with open(employee_file, 'r') as emp_file: cm = confusion_matrix(y_test, predictions) for model_name, model in [Link](): F1 Score: The F1 score for the minority class (1, digit 8) is 93.33%, balancing
csv_reader = [Link](emp_file) [Link](cm, annot=True, fmt='d') [Link](X_train, y_train) precision and recall effectively.
[Link](f"Confusion Matrix for {name}") y_pred = [Link](X_test) Challenges: The model may struggle slightly with some ambiguous images of 8 or
next(csv_reader) # Skip header
accuracy = accuracy_score(y_test, y_pred) images resembling 8 due to its simplicity.
for row in csv_reader: [Link]() f1 = f1_score(y_test, y_pred) Future Improvements: Consider using a more advanced model, such as a
did = row[3] # DID results[model_name] = {"accuracy": accuracy, "f1_score": f1, Convolutional Neural Network (CNN), to improve performance further.
salary = float(row[2]) "confusion_matrix": confusion_matrix(y_test, y_pred)}
return results
if did not in department_salaries:
def plot_confusion_matrices(results):
department_salaries[did] = 0 for model_name, metrics in [Link]():
department_counts[did] = 0 [Link](figsize=(6, 6))
department_salaries[did] += salary [Link](metrics["confusion_matrix"], annot=True, fmt='d', cmap="Blues",
cbar=False)
department_counts[did] += 1
[Link](f"Confusion Matrix: {model_name}")
for did, total_salary in department_salaries.items(): [Link]("Predicted")
avg_salary = total_salary / department_counts[did] [Link]("Actual")
print(f"Department: {departments[did]}, Average Salary: [Link]()
def main():
{avg_salary:.2f}")
file_name = "[Link]" # Replace with your dataset path
calculate_average_salary('[Link]', '[Link]')
You have been given a dataset [Link] having independent
features as x1,x2,x3,x4,x5 and dependent feature as y with Binary Classification Ensemble Learning Models Implementing a Random Forest Model with Majority and
value either 0 or 1. All independent features are continuous Logistic Regression, Decision Tree, and Random Forest are trained. Heatmap Combines predictions from multiple models to improve accuracy. Minority Voting:
reveals correlations among independent features. Confusion matrices give insights Hard Voting: Final prediction is based on majority class votes. Random Forest model implemented here demonstrates the idea of
data except x5, which is having nominal data. Now write
into the true positives, false positives, etc. Scaling improves Logistic Regression Soft Voting: Final prediction is based on the average of predicted probabilities. combining decision trees using both majority and minority voting to improve
python program for the following: a. Clean independent significantly while having minimal impact on tree-based models. from [Link] import VotingClassifier prediction accuracy.
features (if any) b. Draw heatmap to show correlations among Linear Regression The R2R^2R2 score (close to 1) indicates the model from sklearn.linear_model import LogisticRegression import numpy as np
independent features. c. Train this dataset using Logistic explains most of the variance in the data. Visualization of actual vs predicted values from [Link] import DecisionTreeClassifier from [Link] import DecisionTreeClassifier
confirms the fit of the model. from [Link] import SVC from collections import Counter
regression, Decision Tree and Random Forest. Compare the
model1 = LogisticRegression() def __init__(self, n_trees=10):
performance of all the models based on accuracy and F1 model2 = DecisionTreeClassifier() self.n_trees = n_trees
score. d. Draw confusion matrix of each model e. Check Data Visualization model3 = SVC(probability=True) [Link] = [DecisionTreeClassifier() for _ in range(n_trees)]
whether scaling improves the performance or not. 6. Consider Data visualization is crucial for interpreting complex data, and different types of plots ensemble = VotingClassifier(estimators=[ def fit(self, X, y):
are used to highlight various aspects of the data. ('lr', model1), ('dt', model2), ('svc', model3)],
two features x and y based on the following function: y = 3x + for i in range(5):
Types of Plots: voting='soft') [Link][i].fit(X, y)
k, where k can be prepared based on 1000 random values Now Bar Plot: Used for comparing categorical data, it displays bars with heights for i in range(5, 10):
generate 1000 random values between 0 and 1 for x. Calculate representing the values of each category. It’s ideal for showing differences between [Link][i].fit(X, y)
Ensemble learning is a machine learning technique that combines the
y based on above function for these 1000 values of x. Now groups.
predictions of multiple models to improve overall performance. Rather than relying def predict(self, X):
train Linear Regression model and check the score. Histogram: Displays the distribution of continuous data by dividing it into bins or predictions = [Link]([[Link](X) for tree in [Link]])
on a single model, ensemble methods use a group of models to make predictions.
import pandas as pd intervals. It helps in understanding the frequency distribution of the data. majority_vote = [Counter(predictions[:5, i]).most_common(1)[0][0] for i in
The core idea is that a group of "weak" learners can combine to create a "strong"
import numpy as np Box Plot: Shows the distribution of data based on quartiles, highlighting the median, range([Link][0])]
learner, improving the model's generalization [Link] idea is to reduce the risk of
from sklearn.model_selection import train_test_split range, and outliers. It is useful for identifying the spread and potential outliers in the minority_vote = [Counter(predictions[5:, i]).most_common()[-1][0] for i in
overfitting, improve accuracy, and handle complex datasets by leveraging the
from [Link] import OneHotEncoder, StandardScaler dataset. range([Link][0])]
strengths of multiple models.
from sklearn.linear_model import LogisticRegression Scatter Plot: Used to visualize relationships between two continuous variables. final_prediction=[Counter([majority_vote[i],minority_vote[i]]).most_common(1)[0][0]
Categories of Ensemble Learning:
from [Link] import DecisionTreeClassifier Each point represents a pair of values, making it ideal for identifying correlations or for i in range([Link][0])]
Bagging (Bootstrap Aggregating): In bagging, multiple models are trained
from [Link] import RandomForestClassifier trends. return final_prediction
independently using random subsets of the training data. The final prediction is
from [Link] import accuracy_score, f1_score, confusion_matrix Line Plot: Best for visualizing trends over time (time-series data), where data points X = [Link]([[6.1, 4.5], [5.7, 4.2], [4.6, 2.5], [6, 4.6], [4.4, 1.2],
made by combining the predictions of all models (e.g., voting for classification,
import seaborn as sns are connected by a line to show changes over intervals. [2.5, 1.1], [2.6, 1.2], [2.5, 1.4], [4.9, 1.4], [4.8, 4.5]])
averaging for regression).
import [Link] as plt Pie Chart: Represents parts of a whole for categorical data. It is often used for y = [Link](['No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes'])
Example: Random Forest.
df = pd.read_csv('[Link]') showing proportions or percentages of categories within a single variable. rf = CustomRandomForest(n_trees=10)
Boosting: Boosting involves training models sequentially, where each new model
encoder = OneHotEncoder(sparse=False, drop='first') # Drop first for avoiding Heatmap: A color-coded matrix that visualizes the magnitude of values across a [Link](X, y)
tries to correct the errors made by the previous ones. The final prediction is based
multicollinearity two-dimensional space, useful for showing correlation or intensity across variables. predictions = [Link](X)
on a weighted combination of the predictions from all models. : Aims to reduce bias
x5_encoded = encoder.fit_transform(df[['x5']]) import [Link] as plt print(f"Predictions: {predictions}")
and variance. Sequentially trains models, where each subsequent model corrects
x5_columns = encoder.get_feature_names_out(['x5']) import seaborn as sns Explanation of the Random Forest Implementation:
errors made by the previous one. Example: AdaBoost, Gradient Boosting,
x5_df = [Link](x5_encoded, columns=x5_columns) import pandas as pd Majority Voting (First 5 Trees): For the first 5 trees, the prediction is based on the
XGBoost.
X = [Link]([[Link](columns=['x5', 'y']), x5_df], axis=1) data = sns.load_dataset("tips") majority vote of each tree. This means if at least 3 trees predict "Yes" or "No", the
Stacking: Stacking involves training multiple models and then using another model
y = df['y'] [Link](data=data, x="total_bill", y="tip", hue="sex") final prediction for that input will be that majority class.
(often called a meta-model) to learn how to best combine the outputs of the base
[Link](figsize=(10, 8)) [Link]() Minority Voting (Next 5 Trees): For the next 5 trees, the prediction is based on the
models. Example: Stacked Generalization. Combines predictions from multiple
[Link]([Link](), annot=True, cmap='coolwarm', fmt='.2f') models using another "meta-model" that learns the best combination of the base minority vote. This means the class that is least predicted among the 5 trees
becomes the prediction.
[Link]("Correlation Heatmap of Independent Features") Box Plot Explanation model outputs.
[Link]() Detailed Discussion of Bagging: Final Prediction: The final prediction for each input is based on the majority vote
Box Plot: A box plot (also known as a box-and-whisker plot) is a graphical
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, Bagging (Bootstrap Aggregating) is a simple and effective ensemble technique. between the majority and minority voting results from the two groups of trees.
representation of the distribution of a dataset. It displays the minimum, first quartile
random_state=42) The idea is to reduce variance and prevent overfitting by creating multiple versions Voting Classifiers • A very simple way to create a better classifier is to aggregate
(Q1), median (Q2), third quartile (Q3), and maximum of the data, providing insights
models = { of a model, each trained on a random subset of the data. The predictions from these the predictions of each classifier and predict the class that gets the most votes.
into the spread and symmetry of the data. The central box represents the
"Logistic Regression": LogisticRegression(max_iter=1000), multiple models are then averaged (for regression) or voted on (for classification) This majority-vote classifier is called a hard voting classifier Voting Classifiers • If
interquartile range (IQR), which contains the middle 50% of the data, while the
"Decision Tree": DecisionTreeClassifier(), all classifiers are able to estimate class probabilities (i.e., they have a
"whiskers" extend to show the range of data excluding outliers. Outliers, if present, to give the final prediction.
"Random Forest": RandomForestClassifier(n_estimators=100, Detailed Discussion: Bagging (e.g., Random Forest) predict_proba() method), then you can tell Scikit-Learn to predict the class with
are typically marked as individual points outside the whiskers. Box plots are useful
random_state=42)} Bagging focuses on creating diverse models by training them on random subsets the highest class probability, averaged over all the individual classifiers. This is
for comparing distributions across different categories and identifying patterns,
results = {} of the data. Each subset is sampled with replacement. The final prediction is made called soft voting. • It often achieves higher performance than hard voting because
outliers, and variability in the data.
for model_name, model in [Link](): by averaging (for regression) or majority voting (for classification). it gives more weight to highly confident votes.
A visual representation of data showing the median, quartiles, and outliers.
[Link](X_train, y_train) Key Features: Reduces variance, handles overfitting by averaging multiple noisy from [Link] import RandomForestClassifier
Example Weights: 28, 25, 29, 29, 30, 35, 34, 35, 37, 38
y_pred = [Link](X_test) models, and is Highly parallelizable, making it computationally efficient. from [Link] import VotingClassifier
Median: 31.5, Q1: 28.5, Q3: 35, IQR: 6.5 (Q3-Q1)
accuracy = accuracy_score(y_test, y_pred) Steps for Bagging: from sklearn.linear_model import LogisticRegression
Outliers: None (based on 1.5 * IQR rule).
f1 = f1_score(y_test, y_pred) Randomly sample subsets of the training data with replacement (bootstrap from [Link] import SVC
conf_matrix = confusion_matrix(y_test, y_pred) sampling). Train a separate model (e.g., Decision Tree) on each of the subsets. log_clf = LogisticRegression()
results[model_name] = {"accuracy": accuracy, "f1_score": f1, "confusion_matrix": (a) List vs Tuple Aggregate the predictions from all models: rnd_clf = RandomForestClassifier()
conf_matrix} For classification: Use majority voting. svm_clf = SVC(Probability=True)
Aspect List Tuple
for model_name, metrics in [Link](): For regression: Calculate the average of the outputs. voting_clf = VotingClassifier(estimators=[('lr’, log_clf), ('rf', rnd_clf), ('svc',
print(f"{model_name} - Accuracy: {metrics['accuracy']:.4f}, F1 Score: Mutable (can change Immutable (values cannot be Example of Bagging: svm_clf)], voting=‘soft')
Mutability voting_clf.fit(X_train, Y_train) Y_pred=voting_clf.predict(X_test)
{metrics['f1_score']:.4f}") values). changed). Random Forest is a popular bagging method that builds many decision trees and
for model_name, metrics in [Link](): aggregates their results using majority voting or averaging.
Syntax Defined using []. Defined using ().
[Link](figsize=(6, 6)) Decision Tree Algorithm splits data recursively based on a splitting criterion, like Data Science is a multi-disciplinary field that uses scientific methods,
[Link](metrics["confusion_matrix"], annot=True, fmt='d', cmap="Blues", Performance Slower due to mutability. Faster due to immutability. Gini Index or Entropy. processes, algorithms, and systems to extract insights and knowledge from
cbar=False) Custom Random Forest with distinct ensemble strategies demonstrates structured and unstructured data. It combines various fields like statistics,
[Link](f"Confusion Matrix: {model_name}") Ideal for dynamic combining majority and minority voting schemes machine learning, data mining, big data technologies, and data visualization to
Use Case Ideal for fixed collections.
[Link]("Predicted") collections. analyze and interpret complex data to help make informed decisions.
[Link]("Actual") Data science typically involves several stages, each of which plays an essential
[Link]() AdaBoost • Predictor gives more attention to the training instances that the role in extracting actionable insights. Below is a detailed discussion of the various
scaler = StandardScaler() (b) String vs Tuple predecessor underfitted. • Predictors focusing more and more on the hard cases. operations involved in data science.
X_scaled = scaler.fit_transform(X) This is the technique used by Ada-Boost. • A first base classifier (such as a Decision Key Operations in Data Science
Aspect String Tuple
X_train_scaled, X_test_scaled, y_train, y_test = train_test_split(X_scaled, y, Tree) is trained and used to make predictions on the training set. • The relative Data Collection: The first step in any data science project is gathering data. This
test_size=0.2, random_state=42) Type Sequence of characters. Collection of objects. weight of misclassified training instances is then increased. • A second classifier is can involve pulling data from databases, APIs, web scraping, sensors, or
scaled_results = {} trained using the updated weights and again it makes predictions on the training collecting information from surveys, sensors, or transaction logs. Eg: Collecting
for model_name, model in [Link]():
Mutability Immutable. Immutable. set, weights are updated, and so on. AdaBoost • Each instance weight w(i) is initially historical weather data from an API like OpenWeatherMap.
[Link](X_train_scaled, y_train) Has extensive string-specific Limited methods (e.g., set to 1/m. • First predictor is trained and its weighted error rate r1 is computed on Data Cleaning and Preprocessing: Raw data is often incomplete, noisy, and
Operations inconsistent. Cleaning the data involves handling missing values, correcting
y_pred = [Link](X_test_scaled) methods. indexing). the training set. • Weighted error rate of the jth predictor: AdaBoost • The predictor’s
inconsistencies, and transforming the data into a usable format. Preprocessing
accuracy = accuracy_score(y_test, y_pred) weight αj is then computed as below , where η(eta operator) is the learning rate
may involve normalizing, scaling, and encoding the data to make it suitable for
f1 = f1_score(y_test, y_pred) Use Case Ideal for text data. Ideal for grouping items. hyperparameter (defaults to 1): • The more accurate the predictor is, the higher its analysis and machine learning. Operations include: Handling Missing Data:
scaled_results[model_name]={"accuracy":accuracy,"f1_score": f1} weight will be. • If it is just guessing randomly, then its weight will be close to zero. Replacing missing values with the mean, median, or using algorithms like KNN
for model_name in [Link](): • However, if it is most often wrong (i.e., less accurate than random guessing), then imputation. Removing Outliers: Identifying and removing data points that deviate
unscaled_acc = results[model_name]["accuracy"] (c) Dictionary vs List its weight will be negative. AdaBoost • Next the instance weights are updated as significantly from other observations. Normalization/Standardization: Scaling
scaled_acc = scaled_results[model_name]["accuracy"] below, the misclassified instances are boosted. • Then all the instance weights are numerical data into a specific range, especially important for algorithms that rely
Aspect Dictionary List
print(f"{model_name} - Unscaled Accuracy: {unscaled_acc:.4f}, Scaled Accuracy: normalized (i.e., divided byσ𝑖=1 𝑚 𝑤(𝑖)) • Finally, a new predictor is trained using on distance calculations, like K-means or neural networks. Categorical Encoding:
{scaled_acc:.4f}") Structure Key-value pairs. Ordered collection of items. the updated weights, and the whole process is repeated. • To make predictions, Transforming categorical data into numeric forms (e.g., one-hot encoding, label
AdaBoost simply computes the predictions of all the predictors and weighs them encoding). Example: Filling missing values of a "Salary" column with the median
Access Access via keys. Access via indices. using the predictor weights αj. The predicted class is the one that receives the salary for employees in similar departments.
Linear Regression majority of weighted votes. Exploratory Data Analysis (EDA) EDA is the process of analysing and
Use Case Mapping data, e.g., JSON structures. Storing ordered collections. summarizing the main characteristics of the data, often using visual methods. It
Regression is about trying to fit a curve or some sort of function, to a set of
observations and then using that function to predict new values that you haven't Mutability Mutable (keys must be immutable). Mutable. involves understanding the data distribution, relationships between features, and
seen yet. Decision Tree Algorithm: identifying patterns or anomalies. Operations include: Statistical Summary:
•Linear regression is fitting a straight line to a set of observations. A Decision Tree is a supervised machine learning algorithm used for both Calculating mean, median, mode, standard deviation, and correlations.
•A linear model makes a prediction by simply computing a weighted sum of the input classification and regression tasks. It models the decision-making process by Example: Using a histogram to visualize the distribution of ages in a customer
database.
features, plus a constant called the bias term(also called the intercept term). Supervised and Unsupervised Learning & Data Preprocessing splitting data into branches based on feature values, leading to a decision (or
Feature Engineering: Feature engineering involves creating new features from
Supervised Learning: The model is trained on labeled data where input-output prediction) at the leaves.
existing ones, which can help improve the performance of machine learning
pairs are provided. Examples include classification (spam detection) and regression Working of Decision Tree Algorithm: models. This may include transforming, extracting, or combining features.
(price prediction). To build a decision tree, the algorithm recursively splits the dataset into subsets Operations include: Binning/Discretization: Converting continuous features into
Unsupervised Learning: The model works with unlabeled data and finds hidden based on feature values. The objective at each step is to choose the feature that categorical bins Feature Creation: Deriving new features based on domain
patterns or groupings. Examples include clustering (e.g., k-means) and best separates the data. knowledge (e.g., creating a "month" feature from a date-time column).
dimensionality reduction (PCA). Steps for building a Decision Tree: Model Building: In this step, machine learning algorithms are applied to the
Purpose of Data Preprocessing: Data preprocessing involves cleaning and Choose the Best Split: Select the feature and threshold that best split the data. prepared data. Based on the task (classification, regression, clustering, etc.), the
This is typically based on metrics like: appropriate models are selected, trained, and tested. Operations include:
preparing raw data to make it suitable for modeling. Tasks include: Handling missing
Gini Impurity for classification. Supervised Learning: Using labeled data to train models (e.g., Linear Regression
values, Scaling or normalizing data, Encoding categorical variables, Removing
for continuous data, Decision Trees for classification).
outliers Unsupervised Learning: Identifying patterns in data without labeled outcomes
(e.g., K-means clustering for customer segmentation).
In descriptive statistics, data is categorized into different types based on Deep Learning: Training complex neural networks (e.g., convolutional neural
networks for image classification).
their characteristics and the type of analysis that can be performed on them:
Training A Model: Model Evaluation: After training the model, its performance must be evaluated
Nominal Data: This type of data consists of categories that have no intrinsic order Entropy (Information Gain) for classification. using appropriate metrics. The choice of metric depends on the type of problem
Training a model means setting its parameters so that the model best fits the
or ranking. Examples include gender, race, or the color of a car. The values simply Entropy: Measures the amount of uncertainty or impurity in the dataset (e.g., accuracy, precision, recall for classification, or mean squared error for
training set.
serve as labels for different groups. Mean Squared Error (MSE) for regression. regression). Operations include: Cross-Validation, Confusion Matrix, Performance
A model can be trained by:Using a direct “closed-form” equation that directly
Ordinal Data: These are categories with a defined order or ranking, but the intervals Split the Data: Divide the data into subsets based on the chosen feature and Metrics
computes the model parameters that best fit the model to the training set (i.e., the
between the categories are not necessarily equal. For example, customer threshold. Model Tuning and Hyperparameter Optimization: involves adjusting the
model parameters that minimize the cost function over the training set).
satisfaction ratings (poor, average, good) have a natural order but do not specify Recursion: repeat the process on each subset until a stopping condition is met hyperparameters of the model to improve its performance. This can be done using
Using an iterative optimization approach, called Gradient Descent (GD), that
how much better one category is than the next. Step 1: Find the best Gini Index/score from initial set methods like Grid Search or Random Search. Operations include: Grid Search,
gradually tweaks the model parameters to minimize the cost function over the
Interval Data: Interval data are numeric values where the difference between Step 2: Find the best split from initial/training setThe algorithm will loop “attribute” Random Search, Bayesian Optimization
training set, eventually converging to the same set of parameters as the first Model Deployment: Once the model is trained and tuned, it is deployed in a
values is meaningful, but there is no absolute zero point. A good example is number of times & create two buckets left & right.
method. For the purpose of training a model, we first need a measure of how well
temperature measured in Celsius or Fahrenheit, where the difference between 10°C The left one will have nothing assigned while right one have all the sorted row values production environment to make real-time predictions or batch predictions on new,
(or poorly) the model fits the training data. unseen data. Operations include: Model Exporting, API Development, Monitoring.
and 20°C is the same as between 30°C and 40°C, but 0°C does not represent a named as thresholds.
•The most common performance measure of a regression model is the Root Mean Data Visualization and Reporting: is crucial to help stakeholders understand
total absence of temperature. The two initialized buckets (left & right) will enter into next loop, which will iterate
Square Error (RMSE). Therefore, to train a Linear Regression model, you need to insights derived from data science. It involves presenting the data in visual
Ratio Data: This type of data also consists of numeric values, but with a true zero number of row times, on each iteration algorithm will assign each class observation
find the value of θ that minimizes the RMSE. formats such as charts, graphs, and dashboards. Operations include:
point, meaning zero represents the complete absence of the measured quantity. from right to left & calculate the new_gini every time. Dashboards Visualization Libraries
•In practice, it is simpler to minimize the Mean Square Error (MSE) than the RMSE,
Examples include weight, height, and age. The ratios between values are If the new_gini is lower than best_gini then next we will find the best attribute &
and it leads to the same result (because the value that minimizes a function also
meaningful, so, for instance, a person who weighs 60 kg is twice as heavy as one threshold (f,t)
minimizes its square root). Polynomial Regression.
who weighs 30 kg. For the dataset (Play Golf based on Humidity and Wind):
MSE: When data is actually more complex than a simple straight line then surprisingly,
Humidity Wind Play Golf you can actually use a linear model to fit nonlinear data.
•A simple way to do this is to add powers of each feature as new features, then
6.1 4.5 No
The "else" clause in loops (for or while) is a unique feature in Python. It is train a linear model on this extended set of features. This technique is called
executed when the loop completes all its iterations without encountering a break 5.7 4.2 Yes Polynomial Regression.
Gradient Descent •A straight line will never fit the data scattered like polynomial.
statement. If the loop is exited normally after iterating over all the elements, the
Gradient Descent is a very generic optimization algorithm capable of finding optimal 4.6 2.5 No •We can use Scikit-Learn’s PolynomialFeatures class to transform our training
code within the else block is run. However, if the loop is terminated prematurely due
solutions to a wide range of problems. data, adding the square (2nd-degree polynomial) of each feature in the training
to a break (e.g., when a condition inside the loop is met), the else block will not be 6 4.6 Yes
•The general idea of Gradient Descent is to tweak parameters iteratively in order to set as new feature.
executed.
minimize a cost function. •When using Gradient Descent, you should ensure that all 4.4 1.2 No from [Link] import PolynomialFeatures
In the example: poly_features = PolynomialFeatures(degree=2, include_bias=False)
features have a similar scale.
for i in range(5): X_poly = poly_features.fit_transform(X)
linear regression using Scikit-Learn 2.5 1.1 No
print(i)
from sklearn.linear_model import LinearRegression else: 2.6 1.2 Yes
lin_reg = LinearRegression() print("Loop completed without a break.") Machine Learning Definition
lin_reg.fit(X, y) The loop prints numbers from 0 to 4. Since there is no break in this loop, it 2.5 1.4 No Machine learning is a subset of AI that enables systems to learn patterns from data
lin_reg.intercept_, lin_reg.coef_ completes all iterations, and then the else block is executed, printing "Loop and make predictions or decisions without being explicitly programmed.
4.9 1.4 No
Computational complexity completed without a break." If a break were added, the else part would be skipped. Types of Problems:
The Normal Equation computes the inverse of XT X, which is an (n + 1) × (n + 1) This feature can be useful for distinguishing between a loop that completed all 4.8 4.5 Yes Predicting house prices (Regression).
matrix. iterations and one that was prematurely stopped. Identifying spam emails (Classification).
Step-by-Step Decision Tree Construction:
The computational complexity of inverting such a matrix is typically about O(n2.4) Supervised Tasks:
Choose the first split based on the feature that best separates the data (using
to O(n3) (depending on the implementation). Regression: Predicting continuous values (e.g., temperature).
Types of Function Arguments in Python criteria like Gini or entropy). Suppose we choose "Wind" as the first feature.
Once Linear Regression model is trained , predictions are very fast: the Classification: Predicting discrete categories (e.g., spam or not).
Python allows different types of arguments when defining and calling functions: Split on Wind:
computational complexity is linear with regards to both the number of instances you Machine Learning Steps (Pima Indians Diabetes Dataset)
Positional Arguments: Passed in order. If Wind <= 2.5 → Play Golf = No.
want to make predictions on and the number of features. Data Cleaning: Handle missing values.
def greet(name, age): If Wind > 2.5 → Play Golf is mixed (Yes and No), so split further on another feature,
Will explore a different way to train a Linear Regression model, better suited for Exploratory Data Analysis: Understand feature distributions.
print(f"Hello {name}, you are {age} years old.") e.g., "Humidity."
cases where there are a large number of features, or too many training instances Feature Scaling: Standardize features.
greet("Alice", 25) Continue splitting recursively based on the best feature and threshold, until you
to fit in memory. Splitting Data: Train-test split.
Default Arguments: Default values are used if arguments are not provided. reach a point where further splitting doesn’t provide useful information
Modeling: Train models like Logistic Regression, SVM, etc.
Learning Rate (Trade-off) def greet(name="Guest"): Classification And Regression Tree (CART) algorithm Scikit-Learn uses the
Evaluation: Measure performance (e.g., accuracy, F1 score).
An important parameter in Gradient Descent is the size of the steps, determined by the print(f"Hello {name}!") Classification And Regression Tree(CART) algorithm to train Decision Trees(also
learning rate hyperparameter. If the learning rate is too small, then the algorithm will greet() # Output: Hello Guest! called “growing” trees). The idea is really quite simple: the algorithm first splits
take a long time. On the other hand, if the learning rate is too high, the algorithm may fail Keyword Arguments: Passed with argument names. training set in two subsets using as ingle feature k and a threshold tk Purpose of Feature Scaling
to find a good solution. When using Gradient Descent, you should ensure that all features (e.g.,“petallength≤2.45cm”). Purpose: To normalize the range of features so they contribute equally to the
def greet(name, age):
have a similar scale . The Normal Equation To find the value of θ that minimizes print(f"Hello {name}, age {age}.") CART cost function for classification: model. Example: Without scaling, a feature with a large range (e.g., salary) might
the cost function, there is a closed-form solution —in other words, a mathematical greet(age=30, name="Bob") dominate others (e.g., age).
equation that gives the result directly. This is called the Normal Equation •Normal Methods: Min-Max Scaling: Scales features to a range [0, 1].
Arbitrary Positional Arguments (*args): Accepts multiple arguments as a tuple.
Equation from [Link] import MinMaxScaler
def sum_numbers(*args):
return sum(args) import numpy as np
print(sum_numbers(1, 2, 3, 4)) data = [Link]([[1], [5], [10]])
Arbitrary Keyword Arguments (**kwargs): Accepts multiple keyword arguments scaler = MinMaxScaler()
as a dictionary. scaled_data = scaler.fit_transform(data)
def display_info(**kwargs): print(scaled_data)
for key, value in [Link](): Standardization: Centers data around 0 with std. dev(sigma) of 1
print(f"{key}: {value}")
display_info(name="Alice", age=25, location="NY")