Skip to content

Instantly share code, notes, and snippets.

@timotheos
Last active May 27, 2019 16:54
Show Gist options
  • Save timotheos/9323d736f595b495ef488a3ae8287e87 to your computer and use it in GitHub Desktop.
Save timotheos/9323d736f595b495ef488a3ae8287e87 to your computer and use it in GitHub Desktop.
Fisher's Linear Discriminant Analysis (LDA) is a dimension reduction technique that can be used for classification
import numpy as np
class_1 = np.array([[4,1],[2,4],[2,3],[3,6],[4,4]], dtype=np.float64)
class_2 = np.array([[9,10],[6,8],[9,5],[8,7],[10,8]], dtype=np.float64)
m_1, m_2, s_1, s_2, s_w = ([] for i in range(5))
m_1 = np.append(m_1, np.sum(class_1, axis=0)/len(class_1), axis=0)
m_2 = np.append(m_2, np.sum(class_2, axis=0)/len(class_2), axis=0)
print("m_1:", m_1, "\nm_2:", m_2)
for i in range(0, len(class_1), 1):
a = m_1 - class_1[i]
s_1.append(a.reshape(-1,1)*a)
s_1 = np.sum(s_1, axis=0)
for i in range(0, len(class_2), 1):
a = m_2 - class_2[i]
s_2.append(a.reshape(-1,1)*a)
s_2 = np.sum(s_2, axis=0)
s_w = np.sum([s_1 , s_2], axis=0)
print("s_1:\n", s_1, "\ns_2:\n", s_2, "\ns_w:\n", s_w)
w = np.sum(np.linalg.inv(s_w)*(m_1-m_2), axis=1)
print("w:",w)
m_1:
[3. 3.6]
m_2:
[8.4 7.6]
s_1:
[[ 4. -2. ]
[-2. 13.2]]
s_2:
[[ 9.2 -0.2]
[-0.2 13.2]]
s_w:
[[13.2 -2.2]
[-2.2 26.4]]
w:
[-0.44046095 -0.18822023]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment