Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBSCAN notebook Twitter exmaple #638

Merged
merged 2 commits into from
May 3, 2024
Merged
Changes from 1 commit
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
89 changes: 88 additions & 1 deletion notebooks/dbscan.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,93 @@
"plt.grid(True)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Twitter Dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Download Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Full dataset\n",
"# !curl --output twitter.h5.h5 https://b2share.eudat.eu/api/files/189c8eaf-d596-462b-8a07-93b5922c4a9f/twitter.h5.h5\n",
"\n",
"# Partial small dataset\n",
"!curl --output twitterSmall.h5.h5 https://b2share.eudat.eu/api/files/189c8eaf-d596-462b-8a07-93b5922c4a9f/twitterSmall.h5.h5"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we always get the same dataset? May need to keep a copy of it locally or on the cloud storage (e.g. the /dbfs mentioned).

]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run DBSCAN over Twitter Dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"\n",
"# Consider using multiple GPUs given the large datasize\n",
"with h5py.File('twitterSmall.h5.h5', 'r') as f: \n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the file be converted to parquet format and copied to /dbfs ?

" data = np.array(f[\"DBSCAN\"]).tolist()\n",
" df = spark.createDataFrame(data, [\"f1\", \"f2\"])\n",
"\n",
" start_time = time.time()\n",
"\n",
" eps = 0.1\n",
" gpu_dbscan = DBSCAN(eps=eps, min_samples=40, metric=\"euclidean\")\n",
" gpu_dbscan.setFeaturesCols([\"f1\", \"f2\"])\n",
" gpu_model = gpu_dbscan.fit(df)\n",
" gpu_model.setPredictionCol(\"prediction\")\n",
" transformed = gpu_model.transform(df)\n",
" transformed.show()\n",
"\n",
" end_time = time.time()\n",
" elapsed_time = (end_time - start_time)\n",
"\n",
" print(\"Time\", elapsed_time)\n",
"\n",
" dbscan_np = transformed.toPandas().to_numpy()\n",
"\n",
" n_cluster = max(dbscan_np[:,2])\n",
" clusters = [[[],[]] for i in range(int(n_cluster) + 1)]\n",
" \n",
" for p in dbscan_np:\n",
" if int(p[2]) == -1:\n",
" continue\n",
"\n",
" clusters[int(p[2])][0].append(p[0])\n",
" clusters[int(p[2])][1].append(p[1])\n",
"\n",
" clusters = sorted(clusters, key=lambda x: len(x[0]), reverse=True)\n",
" print(\"Number of clusters: \", len(clusters))\n",
"\n",
" for i, c in enumerate(clusters):\n",
" plt.scatter(c[0], c[1], s=0.5, label=f\"cluster {i}\")\n",
" \n",
" plt.xlabel('X')\n",
" plt.ylabel('Y')\n",
" plt.title(f'Twitter API Geo Clusters with DBSCAN eps={eps}')\n",
" plt.show()\n",
" # plt.savefig('plot.png', dpi=1200)"
]
}
],
"metadata": {
Expand Down Expand Up @@ -509,7 +596,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.9.19"
},
"vscode": {
"interpreter": {
Expand Down