how to calculate the scatter within classes for a 50x20 matrix

Viewed 249

I am trying to reduce a largely dimentional matrix to only 2D, i was using an example for 2D arrays,which works, but i would need to do the same for a higher dimentional scatter. I have two classes and each classes have matrices of 50x20 dimensional feature spaces.

For my example i have these 2D arrays:

rectangles = np.array([[1,1.5,1.7,1.45,1.1,1.6,1.8],[1.8,1.55,1.45,1.6,1.65,1.7,1.75]])
triangles = np.array([[0.1,0.5,0.25,0.4,0.3,0.6,0.35,0.15,0.4,0.5,0.48],[1.1,1.5,1.3,1.2,1.15,1.0,1.4,1.2,1.3,1.5,1.0]])

Afterwards im finding the mean for both classes triangles and rectangles

# Calculate the mean vectors per class
mean_rectangles = np.mean(rectangles,axis=1).reshape(2,1)
    
mean_triangles = np.mean(triangles,axis=1).reshape(2,1)

The value given by the means of classes rectangle and triangles, i use them to calculate the scatter:

scatter_triangles = np.dot((triangles-mean_triangles),(triangles-mean_triangles).T)
scatter_circles = np.dot((circles-mean_circles),(circles-mean_circles).T)

# Calculate the SW by adding the scatters within classes 
SW = scatter_triangles+scatter_circles+scatter_rectangles
print(SW)


plt.show()

enter image description here

I would like to know how would i find the scatter within classes and plot them, exactly the same way but for a larger data, precisely for a 50x20 matrix?

For reproducibility this is my code:

    import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')

# Create data
c_A_array = [[ 31,  25,  17,  62,  26,  23, 193, 143,  37,  29, 220, 216, 175, 195, 207, 198, 190, 222, 178, 214],
 [ 31,  26,  19,  59,  25,  23, 193, 140,  37,  29, 220, 216, 174, 195, 207, 198, 190, 220, 178, 214],
 [ 31,  23,  17,  67,  23,  22, 195, 147,  38,  31, 222, 215, 182, 195, 213, 198, 185, 221, 178, 207],
 [ 31,  23,  19,  67,  23,  23, 194, 144,  37,  31, 222, 218, 179, 198, 216, 198, 186, 221, 179, 207],
 [ 31,  28,  17,  65,  23,  22, 193, 142,  36,  31, 222, 217, 177, 195, 216, 196, 182, 220, 174, 207]]

c_B_array = [[ 16,  24,  33,  43,  43,  58, 163,  76,  57, 105, 205, 200, 193, 188, 186, 193, 182, 227, 193, 227],
 [  9,  13,  22,  36,  13,  49, 163,  39,  33, 105, 204, 200, 193, 191, 188, 193, 183, 224, 194, 227],
 [ 23,  17,  10,  28,  21,  40, 166,  46,  28, 102, 208, 206, 196, 198, 195, 202, 190, 225, 196, 229],
 [ 25,  19,  11,  30,  23,  39, 166,  46,  26,  99, 208, 206, 199, 196, 198, 201, 189, 227, 198, 231],
 [ 25,  20,  12,  31,  25,  40, 169,  48,  27, 101, 211, 206, 198, 198, 196, 202, 190, 226, 198, 229]]

#Plot the data
fig = plt.figure(figsize=(10,10))
ax0 = fig.add_subplot(111)

ax0.scatter(c_A_array[0],c_A_array[1],marker='s',c='grey',edgecolor='black')
ax0.scatter(c_B_array[0],c_B_array[1],marker='o',c='blue',edgecolor='black')




# Calculate the mean vectors per class
c_A_array_mean = np.mean(c_A_array,axis=1)
c_B_array_mean = np.mean(c_A_array,axis=1)

# Calculate the scatter matrices for the SW (Scatter within) and sum the elements up

scatter_c_A_array = np.dot((c_A_array-c_A_array_mean),(c_A_array-c_A_array_mean).T)
scatter_c_B_array = np.dot((c_B_array-c_B_array_mean),(c_B_array-c_B_array_mean).T)

# Calculate the SW by adding the scatters within classes 
SW = scatter_c_A_array+scatter_c_B_array
print(SW)


plt.show()

I get the following error:

ValueError: operands could not be broadcast together with shapes (5,20) (5,)

1 Answers

Numpy doesnt broadcast array to columns unfortunaltely. Try:

(np.array(c_A_array).T-c_A_array_mean).T 

For all the subtractions.

This works too tried both of them, guess the second is faster:

np.array(c_A_array)-c_A_array_mean.reshape((5,1))
Related