0% found this document useful (0 votes)
3 views6 pages

GANUMPDT

The document provides an overview of four key computational tools: Genetic Algorithm (GA) for optimization, NumPy for numerical computing, Pandas for data manipulation, and Decision Trees for classification and regression. Each section includes a brief explanation, code examples, and a comparison table highlighting their primary purposes, key features, typical use cases, strengths, and weaknesses. Key takeaways emphasize the unique applications and advantages of each tool in various scenarios.

Uploaded by

Martin Nashaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

GANUMPDT

The document provides an overview of four key computational tools: Genetic Algorithm (GA) for optimization, NumPy for numerical computing, Pandas for data manipulation, and Decision Trees for classification and regression. Each section includes a brief explanation, code examples, and a comparison table highlighting their primary purposes, key features, typical use cases, strengths, and weaknesses. Key takeaways emphasize the unique applications and advantages of each tool in various scenarios.

Uploaded by

Martin Nashaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Genetic Algorithm (GA)

Explanation:

A metaheuristic inspired by natural selection. Solutions (chromosomes) evolve via


selection, crossover, and mutation to optimize a fitness function.

Code Example (Maximize ( f(x) = x^2 )):

import random

def init_population(size, chrom_length):


return [[random.randint(0,1) for _ in range(chrom_length)] for _
in range(size)]

def fitness(chromosome):
x = int(''.join(map(str, chromosome)), 2)
return x**2

def select_parents(population, fitnesses):


parents = random.choices(population, weights=fitnesses, k=2)
return parents

def crossover(p1, p2):


pt = random.randint(1, len(p1)-1)
return p1[:pt] + p2[pt:], p2[:pt] + p1[pt:]

def mutate(chrom, rate=0.1):


return [gene if random.random() > rate else 1-gene for gene in
chrom]

# Parameters
pop_size = 6
chrom_length = 5 # x: 0-31
generations = 5

pop = init_population(pop_size, chrom_length)


for gen in range(generations):
fitnesses = [fitness(chrom) for chrom in pop]
print(f"Gen {gen}: Best = {max(fitnesses)}")
new_pop = []
for _ in range(pop_size//2):
p1, p2 = select_parents(pop, fitnesses)
c1, c2 = crossover(p1, p2)
new_pop.extend([mutate(c1), mutate(c2)])
pop = new_pop

2. NumPy (Numerical Python)

Explanation:

A library for numerical computing with support for arrays, matrices, and mathematical
functions.

Code Example:

import numpy as np

# Create arrays
a = np.array([1, 2, 3, 4])
b = np.arange(12).reshape(3, 4)

print("Array a:\n", a)
print("Matrix b:\n", b)

# Operations
dot_product = np.dot(a, b.T) # Matrix multiplication
mean = np.mean(b)
sum_col = np.sum(b, axis=0)

print("\nDot product:", dot_product)


print("Column sums:", sum_col)
3. Pandas (Data Analysis)

Explanation:

A library for data manipulation and analysis using DataFrame structures.

Code Example:

import pandas as pd

# Create DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [50000, 70000, 90000]}
df = pd.DataFrame(data)

# Data operations
filtered = df[df['Age'] > 25]
df['Bonus'] = df['Salary'] * 0.1
grouped = df.groupby('Age').mean()

print("Original DataFrame:\n", df)


print("\nFiltered DataFrame:\n", filtered)
print("\nGrouped Data:\n", grouped)

4. Decision Tree

Explanation:

A supervised learning algorithm for classification/regression. Splits data using feature


thresholds to maximize information gain.

Code Example (Iris Classification):

from sklearn.datasets import load_iris


from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load data
iris = load_iris()
X, y = iris.data, iris.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Train model
clf = DecisionTreeClassifier(max_depth=3)
clf.fit(X_train, y_train)

# Evaluate
y_pred = clf.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred ):.2f}")

Here’s a detailed comparison table covering Genetic Algorithm (GA),


NumPy, Pandas, and Decision Tree:

Genetic
Feature Algorithm NumPy Pandas Decision Tree
(GA)

Solve
Numerical
optimization
computation Data Classification/Re
Primary problems
s with manipulation and gression via
Purpose using
arrays/matri analysis hierarchical splits
evolutionary
ces
principles
- Population-
-
based - N-
DataFrame/Series
- Stochastic dimensional - Recursive
structures
operators arrays partitioning
- Data
Key Features (crossover, - Vectorized - Feature
cleaning/aggregati
mutation) operations importance
on
- Fitness- - Linear - Interpretability
- Time-series
driven algebra tools
support
selection

- NP-hard - Matrix
- CSV/Excel data - Customer
problems math
handling segmentation
Typical Use (e.g., TSP) - Signal
- Data wrangling - Medical
Cases - Parameter processing
- Exploratory diagnosis
tuning -
analysis - Fraud detection
- Robotics Simulations

- Handles
non- - Fast array - Intuitive data - Easy to visualize
differentiable/ operations handling - No need for
non-convex - Memory - Merge/join feature scaling
Strengths
functions efficiency operations - Handles non-
- Global - Integration - Missing data linear
search with C/C++ tools relationships
capability

-
Computation - Limited to
ally intensive numerical
- Memory-heavy - Prone to
- No data
for large datasets overfitting
guarantee of - Steeper
Weaknesses - Slower than - Unstable (small
optimal learning
NumPy for pure data changes →
solution curve for
math ops different tree)
- advanced
Hyperparame features
ter-sensitive
scikit-learn,
Libraries/Tool DEAP, PyGAD,
numpy pandas XGBoost,
s pymoo
LightGBM

Key Takeaways:

1. Genetic Algorithm:
a. Best for optimization where traditional methods fail (e.g.,
complex, noisy search spaces).
b. Example: Optimizing delivery routes for minimal fuel
consumption.
2. NumPy:
a. Foundation for numerical computing in Python.
b. Example: Solving linear equations Ax = b using
np.linalg.solve().
3. Pandas:
a. Ideal for tabular data (e.g., CSV, Excel).
b. Example: Aggregating sales data by region/month.
4. Decision Tree:
a. Simple interpretable models for classification/regression.
b. Example: Predicting loan defaults based on income/credit
score.

You might also like