Examples#

  • Complex numbers

    • Absolute value

  • Evenly spaced numbers

  • Mean

    • Arithmetic mean

    • Geometric mean

    • Harmonic mean

  • Classification

Complex Numbers#

A complex number \(z\) is represented by \(z = x + j* y\), where \(x\) and \(y\) are real numbers.

c1 = complex(0, 0)

print(c1)
x = 2
y = 3
c2 = complex(x, y)

print(c2)

Absolute value#

Absolute value of a complex number \(z = x + j* y\) is obtained by the formula \(\sqrt {(x^2 + y^2)}\)

from math import sqrt

absc2 = sqrt(x**2 + y**2)
print(absc2)

Another way is to use the function \(abs()\).

absc2 = abs(c2)
print(absc2)
absc1 = abs(c1)
print(absc1)

Evenly spaced numbers#

import numpy as np

np.linspace(1.0, 5.0, num=5)
import numpy as np

np.linspace(1.0, 5.0, num=10)

Mean#

Arithmetic mean#

If \(a_1, a_2, \ldots, a_n\) are numbers, then the arithmetic mean is obtained by:

\[A=\frac{1}{n}\sum_{i=1}^n a_i=\frac{a_1+a_2+\cdots+a_n}{n}\]
num = np.linspace(1.0, 100.0, num=100)

a = np.mean(num)
print(a)

Geometric Mean#

If \(a_1, a_2, \ldots, a_n\) are numbers, then the arithmetic mean is obtained by:

\[\left(\prod_{i=1}^n a_i\right)^\frac{1}{n} = \sqrt[n]{a_1 a_2 \cdots a_n}\]
from scipy.stats import gmean

g = gmean(num)
print(g)

Harmonic Mean#

If \(a_1, a_2, \ldots, a_n\) are numbers, then the harmonic mean is obtained by:

\[H = \frac{n}{\frac1{a_1} + \frac1{a_2} + \cdots + \frac1{a_n}} = \frac{n}{\sum\limits_{i=1}^n \frac1{a_i}} = \left(\frac{\sum\limits_{i=1}^n a_i^{-1}}{n}\right)^{-1}.\]
from scipy.stats import hmean

h = hmean(num)
print(h)

Classification#

Understanding IRIS dataset#

from sklearn.datasets import load_iris

data = load_iris()

# Target Classes and Names
for target, name in enumerate(data.target_names):
    print(target, name)
# Features
for feature in data.feature_names:
    print(feature)
X = data.data
Y = data.target

print(f"Data size: {len(X)}")
# Printing the data

for x, y in zip(X, Y):
    print(f" {x}: {y} ({data.target_names[y]})")
from sklearn.model_selection import train_test_split

# Random state ensures reproducibility
X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.33, random_state=42
)

print(f"Training data size: {len(X_train)}")
print(f"Test data size: {len(X_test)}")
from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plot


def display_confusion_matrix(classifier, title):
    disp = ConfusionMatrixDisplay.from_estimator(
        classifier,
        X_test,
        Y_test,
        display_labels=data.target_names,
        cmap=plot.cm.Blues,
        normalize="true",
    )
    disp.ax_.set_title(title)
    plot.show()
## Classifier
from sklearn import svm, metrics

for kernel in ["linear", "rbf", "poly"]:
    print("")
    classifier = svm.SVC(kernel=kernel, gamma=10)
    classifier.fit(X_train, Y_train)

    predicted_values = classifier.predict(X_test.reshape((X_test.shape[0], -1)))

    # classification report
    title = "SVM classifier with" + kernel + " kernel"
    print(metrics.classification_report(Y_test, predicted_values))
    display_confusion_matrix(classifier, title)

References#