Skip to content

Commit

Permalink
implemented spectral clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
sohompaul committed Jun 17, 2020
1 parent 960e663 commit 3fdeabd
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion multitask/Cluster.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"outputs": [],
"source": [
"import itertools\n",
"import pandas as pd"
"import pandas as pd\n",
"import pickle\n",
"\n",
"from sklearn.cluster import SpectralClustering"
]
},
{
Expand Down Expand Up @@ -124,6 +127,24 @@
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def cluster_from_similarity_matrix(df, num_clusters):\n",
" mat = df.to_numpy()\n",
" df[:] = (mat+mat.T)/2\n",
" sc = SpectralClustering(n_clusters=num_clusters, affinity='precomputed')\n",
" labels = sc.fit_predict(df.to_numpy())\n",
" d = {c:[] for c in labels}\n",
" for c, t in zip(labels, df.columns):\n",
" d[c].append(t)\n",
" clusters = [v for k, v in d.items()]\n",
" return clusters"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -141,6 +162,16 @@
"source": [
"save_transfer_coefficients(tasks, A_test, X_test, E_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = load_transfer_coefficients()\n",
"print(cluster_from_similarity_matrix(df, 6))"
]
}
],
"metadata": {
Expand Down

0 comments on commit 3fdeabd

Please sign in to comment.