Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

## Create distance metric weight matrix weighted by standard deviation
weight_diagonal = x_vals.std(0)
weight_matrix = tf.cast(tf.diag(weight_diagonal), dtype=tf.float32)
weight_matrix = tf.cast(tf.expand_dims(weight_diagonal,1), dtype=tf.float32)

# Split the data into train and test sets
np.random.seed(13) # reproducible results
Expand All @@ -73,9 +73,8 @@
# Declare weighted distance metric
# Weighted L2 = sqrt((x-y)^T * A * (x-y))
subtraction_term = tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))
first_product = tf.matmul(subtraction_term, tf.tile(tf.expand_dims(weight_matrix,0), [batch_size,1,1]))
second_product = tf.matmul(first_product, tf.transpose(subtraction_term, perm=[0,2,1]))
distance = tf.sqrt(tf.matrix_diag_part(second_product))
product = tf.matmul(tf.square(subtraction_term), tf.tile(tf.expand_dims(weight_matrix,0), [batch_size,1,1]))
distance = tf.sqrt(tf.squeeze(product,axis=2))

# Predict: Get min distance index (Nearest neighbor)
top_k_xvals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)
Expand Down Expand Up @@ -113,4 +112,4 @@
plt.xlabel('Med Home Value in $1,000s')
plt.ylabel('Frequency')
plt.legend(loc='upper right')
plt.show()
plt.show()