Lab5-Specs-checkpoint
COMP9318-Lab5
Instructions
This note book contains instructions for COMP9318-lab5.
You are required to complete your implementation in a file submission.py provided along with this notebook.
You are not allowed to print out unnecessary stuff. We will not consider any output printed out on the screen. All results should be returned in appropriate data structures via corresponding functions.
You can submit your implementation for lab5 via following link: http://kg.cse.unsw.edu.au:8318/lab5/ .
For each question, we have provided you with detailed instructions along with question headings. In case of any problem, you can post your query @ Piazza.
You are allowed to add other functions and/or import modules (you may have to in this lab), but you are not allowed to define global variables. Only functions are allowed in submission.py.
You should not import unnecessary modules/libraries, failing to import such modules at test time will lead to errors.
We will provide immediate feedback on your submission. You can access your scores using the online submission portal on the same day.
For Final Evaluation we will be using a different dataset, so your final scores may vary.
You are allowed to submit as many times as you want before the deadline, but ONLY the latest version will be kept and marked.
Submission deadline for this assignment is 23:59:59 on 10th June, 2018. We will not accept any late submissions.
Question-1: Spectral Clustering
In this lab you are required to implement the spectral clustering algorithm to cluster a given graph G into TWO clusters.
In[1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import networkx as nx
# Reading edges in a Graph
data_file=./asset/a
with open(data_file) as infile:
edges = [tuple(map(int,(line.strip().split( )))) for line in infile]
# Creating a new Graph
G = nx.Graph(name=Ex1)
for edge in edges:
G.add_edge(*edge)
# You can visualize the Graph for your own understanding
nx.draw_networkx(G, with_labels=True, node_color=y)
plt.show()
You need to implement spectral clustering algorithm (i.e., spectral_clustering() in the file: submission.py). The input arguments of spectral_clustering() are:
Input:
G: A graph as defined above
Output:
eigenvectors, i.e., a numpy matrix containing two eigenvectors starting with the second smallest eigenvector. Its dimensions should be (N,2), where N stands for number of nodes in the graph.
clusters, i.e., lists of sub-lists containing the NODE-IDs corresponding to two clusters.
For example, a sample output is shown in the cell given below:
In[2]:
import submission as submission
# Reading edges in a Graph
data_file=./asset/a
with open(data_file) as infile:
edges = [tuple(map(int,(line.strip().split( )))) for line in infile]
# Creating a new Graph
G = nx.Graph(name=Ex1)
for edge in edges:
G.add_edge(*edge)
eigenvector, clusters =submission.spectral_clustering(G)
print(eigenvector)
print(eigenvector.shape)
print(type(eigenvector))
print(clusters = ,clusters)
[[-0.50495006 -0.07917647]
[-0.50495006 -0.07917647]
[-0.327897650.05021194]
[ 0.141179060.22693353]
[ 0.29532479 -0.25362181]
[ 0.287737640.03212914]
[ 0.260049360.70816516]
[ 0.3535069-0.60546503]]
(8, 2)
clusters =[[6, 5, 7, 9, 8], [2, 3, 4]]
Note:
For this lab we will be using Un-normalized Graph Laplacian.
For k-means clustering you should use nltk with Euclidean distance.
Please use the following nltk commands to generate clusters from the eigenvectors
kmeans_ = KMeansClusterer(num_means=2, distance=nltk.cluster.util.euclidean_distance, repeats=50, normalise=True,rng=random.Random(10))
clusters = kmeans_.cluster(eigenvectors, assign_clusters=True)
Submission
You need to complete the function spectral_clustering() in the file: submission.py. You can test your submission against sample test cases via online submission system (i.e., http://kg.cse.unsw.edu.au:8318/lab5/).
Test Environment
For testing, we have pre-installed the requisite modules and/or libraries in the testing environment. You are only allowed to use following libraries:
python: 3.5.2
numpy: 1.14.0
networkx: 2.1
nltk: 3.2.5
Note:
1. You need to implement the methodology by yourself. You are not allowed to import **sklearn** and/or any other library in Lab5.
2. For cluster labels you should use the priorly provided commands (given in last cell) to generate the clusters.
In[]:
Reviews
There are no reviews yet.