0
$\begingroup$

Binary Random Projection of Features, Samples

Creating a binary random projection that will be used in a kNN Hanning function for hamming distances on nearest neighbors that will be processed by computing the bin count of the y_train. Prior to this kNN hanning function is necessary to define binary random projection for the X_train and x_test.

Specifications for the binary random projection

Specification for this is to ensure that this binary random projection computes the structure of features and number of samples that is representative as samples in X-train (255,112) features=255, samples=112, to the samples as contained in the y_train(112,) labels.

The custom code for create_random_projection_matrix() and binary_random_projection() appears to be structural fit for generating a Y_binary random projection matrix from the X_train samples. As X_train samples will be used to apply these projections to my projections in X_train to obtain a suitable Y_binary. Key to this is the that the Y_binary properly process the computing scipy.spatial.distance.cdist() distances on the X_train (features=255,samples=112) for y_train (features=112,).

Error in processing binary projection

The issue may in fact be related more to the mismatch between the expected dimensions of the output and the indexing problem via computed distances in scipy.spatial.distance.cdist() and encounter later in the kNN_hamming function as an error. This error resulted in kNN_hamming after the scipy.spatial.distance.cdist(): IndexError: index 117 is out of bounds for axis 0 with size 112

Effort needed for binary project solution

To ensure that the binary random projection computes the structure of features and number of samples), to corresponds to features=255, samples=112 for X_train(255,112), and features=255, samples=28 for x_test (255,28). These must to align with computing how to apply the random projection and understanding the resultant shapes to have y_train (112,) properly in the kNN_hamming() function for the distance measurement of nearest neighbor.

After the X_train and x_test binary random projection for the Y_binary, need to know how to set Y_binary to have the shape to match the X_train(255,112) samples=112.

After projection, Y_binary will have the same shape of samples=112 as X_train, since the binary projection is onto the space with 255 feature dimensions.

Binary project problem in kNN_Hanning fuction

IndexError: index 117 is out of bounds for axis 0 with size 112. Clearly the 117 is referring to the X_train features dimension, which is 255 and which 117 would be in range, but this is clear that the logic of distances = scipy.spatial.distance.cdist(X_test_binary, X_train_binary, metric='hamming') is creating a distance on features (255) rather than samples (112).

Question

So if the binary projection of X_train is projected to match with Y_train number of samples, 112, not on features, 255, then how would the scipy.spatial.distance.cdist() be applied to create distance on samples and not features, as it appears to be currently doing?

Shape of matrix in problem space

X_train (255, 112)
x_test (255, 28)
y_train (112,)
y_test (28, 1)

Effort code samples Here is my effort to create random projection matrix and the Y_binary.

def create_random_projection_matrix(L, M):
    projection_matrix = np.random.randn(L, M)
    projection_matrix /= np.linalg.norm(projection_matrix, axis=1)[:, np.newaxis]  
    return projection_matrix

def binary_random_projection(X, param):
    print('---binary_random_projection---')
    print('X:', X.shape)
    print('param:', param.shape)
    Y = np.dot(X, param.T)  
    Y_binary = np.sign(Y)  
    return Y_binary.astype(int)
parmTrain = create_random_projection_matrix(L=255, M=112)
X_train_binary = binary_random_projection(matrix_train_stft, parmTrain)

The main drive code for the binary projection, etc.

for L in L_list:
    print('---main driver---')
    print('matrix_train_stft:', matrix_train.shape)
    print('matrix_train_stft data types:', matrix_train.dtype)
    parmTrain = create_random_projection_matrix(L=255, M=112)
    X_train_binary = binary_random_projection(matrix_train_stft, parmTrain)
    parmTest = create_random_projection_matrix(L=255, M=28)
    X_test_binary = binary_random_projection(matrix_test_stft, parmTest)
    y_train = np.where(np.abs(matrix_y_train_stft[0]) > 0, 2, 1)
    
    for k in K_list:
        y_pred = kNN_hamming(X_train_binary, X_test_binary, y_train, k)
        accuracy = np.mean(y_pred == y_test) 

When this code executes, the following shape of matrix in problem space is:

---main driver---
matrix_train_stft: (255, 112)
matrix_train_stft data types: complex128
---binary_random_projection---
X: (255, 112)
param: (255, 112)
---binary_random_projection---
X: (255, 28)
param: (255, 28)
---kNN_hamming---
X_train_binary: (255, 255)
y_train: (112,)
X_test_binary: (255, 255)
y_train_flat (112,)
distances: (255, 255)

Sample data X_train:

array([[[-0.01074219, -0.0234375 , -0.01025391, ..., -0.11767578,
          0.04931641,  0.01171875],
        [-0.00830078,  0.02441406, -0.015625  , ..., -0.10400391,
          0.03320312, -0.02587891],
        [ 0.00634766,  0.0546875 ,  0.01806641, ..., -0.15332031,
         -0.03222656, -0.01660156]],

       [[ 0.03466797,  0.01660156, -0.10742188, ..., -0.11669922,
         -0.00048828, -0.05615234],
        [-0.02197266, -0.00390625, -0.02685547, ..., -0.10595703,
          0.04931641, -0.0546875 ],
        [-0.00146484,  0.03271484, -0.05224609, ..., -0.11132812,
          0.02783203, -0.04345703]],

       [[-0.02441406,  0.01611328, -0.14013672, ..., -0.06933594,
         -0.01806641, -0.17041016],
        [-0.04882812, -0.01464844, -0.07080078, ..., -0.07666016,
          0.07421875, -0.10498047],
        [-0.03369141, -0.01855469, -0.11474609, ..., -0.12646484,
          0.07519531, -0.07617188]]

y_train:

array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2])
$\endgroup$

1 Answer 1

1
$\begingroup$

The size of the distances resulting from the binary projection of X_train (binary) and x_test (binary) for the hamming kNN needed to have the Y_train reshape results of the distances set to [:112,0] to match with the sample shapes of X_train.

Hence this code fixed that sample shapes problem:

    distances = cdist(X_test_binary, X_train_binary, metric='hamming')[:112,0]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.