In [1]:
import graphlab
In [2]:
image_train_url = 'https://d396qusza40orc.cloudfront.net/phoenixassets/image_train_data.csv'
image_test_url = 'https://d396qusza40orc.cloudfront.net/phoenixassets/image_test_data.csv'
image_train_data = graphlab.SFrame(image_train_url)
image_test_data = graphlab.SFrame(image_test_url)
In [3]:
image_train_data.head()
Out[3]:
In [4]:
# create a k nearest neighbor model using deep features and outputting the labels of the most similar images
knn_model = graphlab.nearest_neighbors.create(image_train_data,
features=['deep_features'],
label='id')
In [10]:
# start with one image
cat = image_train_data[18:19]
cat
Out[10]:
In [11]:
# query the model to find nearest neighbors (most similar images) to the above image
knn_model.query(cat)
Out[11]:
In [14]:
# create a function to get the remainder of the data for the nearest neighbors
def get_image_from_id(query_result):
return image_train_data.filter_by(query_result['reference_label'], 'id')
In [18]:
# calling this function returns the profiles for the five most similar images
get_image_from_id(knn_model.query(cat))
Out[18]:
In [19]:
show_neighbors = lambda x: get_image_from_id(knn_model.query(image_train_data[x:x+1]))
In [20]:
# enter row number to get list of that row's closest images
show_neighbors(8)
Out[20]:
In [21]:
show_neighbors(1000)
Out[21]:
In [ ]: