mirror of
https://github.com/fenago/data-science.git
synced 2026-05-04 16:41:05 +00:00
268 lines
5.4 KiB
Plaintext
268 lines
5.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "JBMdxhsVohF2"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"from sklearn.cluster import KMeans\n",
|
|
"import altair as alt"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "iSwgbS3CooqD"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"file_url = '../DataSet/taxstats2015.csv'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "91P_EE8NpNHg"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df = pd.read_csv(file_url, usecols=['Postcode', 'Average total business income', 'Average total business expenses'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"colab_type": "code",
|
|
"id": "YTXNZwfYpQkg",
|
|
"outputId": "6b1ab7f7-cc4c-434e-c482-db4063c6d68d"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "86yRMdUtpgnI"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"X = df[['Average total business income', 'Average total business expenses']]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "tU9JPKJJpzBg"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clusters = pd.DataFrame()\n",
|
|
"inertia = []"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "Hu0Dv2tzqEB0"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clusters['cluster_range'] = range(1, 15)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "RlEkLMUkqsbq"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for k in clusters['cluster_range']:\n",
|
|
" kmeans = KMeans(n_clusters=k).fit(X)\n",
|
|
" inertia.append(kmeans.inertia_)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 483
|
|
},
|
|
"colab_type": "code",
|
|
"id": "slnfmG3Mqtyy",
|
|
"outputId": "7ea3a4ec-e566-44d7-d48a-d2602d33feb9"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clusters['inertia'] = inertia\n",
|
|
"clusters"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 368
|
|
},
|
|
"colab_type": "code",
|
|
"id": "WHUQBZ8uqvly",
|
|
"outputId": "195438e3-b79a-41f8-afd5-e2430070aed9"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"alt.Chart(clusters).mark_line().encode(alt.X('cluster_range'), alt.Y('inertia'))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "CUMT2XeIrIpf"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"optim_cluster = 4"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 68
|
|
},
|
|
"colab_type": "code",
|
|
"id": "UTb91dx0rsqD",
|
|
"outputId": "f84bdf2a-e092-46ff-cc4e-baddebcc2ccb"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"kmeans = KMeans(random_state=42, n_clusters=optim_cluster)\n",
|
|
"kmeans.fit(X)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "4Zf9IGEsruV8"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df['cluster2'] = kmeans.predict(X)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"colab_type": "code",
|
|
"id": "LfxHSNcUrzA0",
|
|
"outputId": "c220d3eb-6145-4bcf-b53e-e4e0b35c7b11"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 368
|
|
},
|
|
"colab_type": "code",
|
|
"id": "JM-UjFvyr73R",
|
|
"outputId": "c05e1733-37ea-4174-b279-ea028639d185"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"alt.Chart(df).mark_circle().encode(x='Average total business income', y='Average total business expenses',color='cluster2:N', tooltip=['Postcode', 'cluster2', 'Average total business income', 'Average total business expenses']).interactive()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "aGMIPBYOscBQ"
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "Exercise5_3.ipynb",
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|