1. Temperature & Fan Speed Membership
Fuzzy Logicdef hot_temperature(temp):
if temp <= 20:
return 0.0
elif temp >= 35:
return 1.0
else:
return (temp - 20) / (35 - 20)
temperature = 30
membership = hot_temperature(temperature)
print("Temperature:", temperature)
print("Degree of Hotness:", membership)
def hot(temp):
if temp <= 20:
return 0
elif temp >= 35:
return 1
else:
return (temp - 20) / 10
temperature = 32
hot_value = hot(temperature)
fan_speed = hot_value * 100
print("Hot membership:", hot_value)
print("Fan Speed:", fan_speed)
Temperature: 30
Degree of Hotness 0.6666666666666666
Hot membership: 1.2
Fan Speed 120.0
2. Fuzzy Rule Space Calculation
Combinatoricsdef number_of_rules(inputs, fuzzy_sets_per_input):
return fuzzy_sets_per_input ** inputs
inputs = 4
sets = 3
rules = number_of_rules(inputs, sets)
print("Number of rules:", rules)
Number of rules: 81
3. Basic Fuzzy Set Operations
Set TheoryA = [0.2, 0.7, 1.0]
B = [0.6, 0.4, 0.8]
union = [max(a, b) for a, b in zip(A, B)]
intersection = [min(a, b) for a, b in zip(A, B)]
complement_A = [1 - a for a in A]
difference = [min(a, 1 - b) for a, b in zip(A, B)]
print("Union:", union)
print("Intersection:", intersection)
print("Complement of A:", complement_A)
print("A - B (Difference):", difference)
Union: [0.6, 0.7, 1.0]
Intersection: [0.2, 0.4, 0.8]
Complement of A: [0.8, 0.30000000000000004, 0.0]
A - B: [0.2, 0.6, 0.19999999999999996]
4. De Morgan's Law Verification
ValidationA = [0.3, 0.8]
B = [0.7, 0.4]
lhs = [1 - max(a, b) for a, b in zip(A, B)]
rhs = [min(1 - a, 1 - b) for a, b in zip(A, B)]
print("LHS:", lhs)
print("RHS:", rhs)
print("De Morgan law satisfied:", lhs == rhs)
LHS: [0.30000000000000004, 0.19999999999999996]
RHS: [0.30000000000000004, 0.19999999999999996]
De Morgan law satisfied: True
5. Joint Membership (T-Norms)
T-Normshot = 0.7
humid = 0.6
joint_min = min(hot, humid)
joint_product = hot * humid
print("Joint membership using min:", joint_min)
print("Joint membership using product:", joint_product)
Joint membership using min: 0.6
Joint membership using product: 0.42
6. Algebraic and Bounded Fuzzy Operators
Operatorsa = 0.6
b = 0.7
algebraic_product = a * b
algebraic_sum = a + b - a * b
bounded_sum = min(1, a + b)
bounded_difference = max(0, a + b - 1)
print("Algebraic product:", algebraic_product)
print("Algebraic sum:", algebraic_sum)
print("Bounded sum:", bounded_sum)
print("Bounded difference:", bounded_difference)
Algebraic product: 0.42
Algebraic sum: 0.8799999999999999
Bounded sum: 1
Bounded difference: 0.2999999999999998
7. Fuzzy Relations & Tolerance Spaces
Relations# Matrix Creation
A = [1, 2, 3]
R = [[1 if x < y else 0 for y in A] for x in A]
for row in R:
print(row)
# Mapping Properties
students = ["S1", "S2"]
subjects = ["Math", "Physics"]
R_interest = [[0.9, 0.6], [0.4, 0.8]]
for i, student in enumerate(students):
for j, subject in enumerate(subjects):
print(f"{student} interest in {subject} = {R_interest[i][j]}")
# Tolerance Validation
R_tol = [[1, 0.5, 0.3], [0.5, 1, 0.4], [0.3, 0.4, 1]]
is_reflexive = all(R_tol[i][i] == 1 for i in range(len(R_tol)))
is_symmetric = all(R_tol[i][j] == R_tol[j][i] for i in range(len(R_tol)) for j in range(len(R_tol)))
print("Reflexive:", is_reflexive)
print("Symmetric:", is_symmetric)
print("Tolerance relation:", is_reflexive and is_symmetric)
[0, 1, 1]
[0, 0, 1]
[0, 0, 0]
S1 interest in Math = 0.9
S1 interest in Physics = 0.6
S2 interest in Math = 0.4
S2 interest in Physics = 0.8
Reflexive: True
Symmetric: True
Tolerance relation: True
8. Parameter Estimation (OLS, RLS & Gradient Descent)
Optimization & Machine Learningimport numpy as np
# 1. Ordinary Least Squares (OLS)
X = np.array([[1], [2], [3]])
Y = np.array([[2], [4], [6]])
theta_ols = np.linalg.inv(X.T @ X) @ X.T @ Y
print("Estimated parameter (OLS):", theta_ols)
# 2. Recursive Least Squares (RLS)
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([2, 4, 6, 8, 10])
theta_rls = np.array([[0.0]])
P = np.array([[1000.0]])
lam = 1.0
for x, y in zip(x_data, y_data):
phi = np.array([[x]])
y_pred = phi.T @ theta_rls
error = y - y_pred[0, 0]
K = (P @ phi) / (lam + phi.T @ P @ phi)
theta_rls = theta_rls + K * error
P = (1 / lam) * (P - K @ phi.T @ P)
print("Estimated theta (RLS):", theta_rls)
# 3. Gradient Descent
theta_gd = 0.0
eta = 0.1
x_gd = [1, 2, 3]
y_gd = [2, 4, 6]
for epoch in range(20):
for x, y in zip(x_gd, y_gd):
y_pred = theta_gd * x
error = y - y_pred
theta_gd = theta_gd + eta * error * x
print("Estimated theta (Gradient Descent):", theta_gd)
Estimated parameter (OLS): [[2.]]
Estimated theta (RLS): [[1.99996364]]
Estimated theta (Gradient Descent): 2.0