Skip to content

Commit

Permalink
clustering ipynb update with json url request
Browse files Browse the repository at this point in the history
  • Loading branch information
HonglinZhu0930 committed Mar 4, 2024
1 parent 481090c commit ab4aeed
Show file tree
Hide file tree
Showing 214 changed files with 205 additions and 23,679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "aec2eb7c-6e76-430c-a9b8-e538ca16fc8b",
"metadata": {},
"outputs": [],
Expand All @@ -11,7 +11,9 @@
"import os\n",
"import sklearn\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
"import numpy as np\n",
"import requests\n",
"import ast"
]
},
{
Expand All @@ -24,13 +26,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "e4cdb558-4a43-47be-bf7a-22fdab92f0c5",
"metadata": {},
"outputs": [],
"source": [
"state_folders = [item for item in os.listdir(os.getcwd()) if os.path.isdir(item) and item[-3:]==\"1.0\"]\n",
"state_folders"
"#state_folders = [item for item in os.listdir(os.getcwd()) if os.path.isdir(item) and item[-3:]==\"1.0\"]\n",
"#state_folders\n",
"state_folders = [\"GAEEIOv1.0\", \"MEEEIOv1.0\", \"MNEEIOv1.0\", \"OREEIOv1.0\", \"WAEEIOv1.0\"]"
]
},
{
Expand All @@ -43,27 +46,60 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "70360120-acf9-4c02-adef-cf71352c42be",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[2.73512871e-02, 3.16932045e-04, 8.95749314e-04, ...,\n",
" 3.78456417e-01, 5.26837268e-01, 6.84648057e-01],\n",
" [1.15993681e-02, 2.57649388e-03, 1.33494368e-03, ...,\n",
" 3.78535840e-01, 5.27454394e-01, 6.84647861e-01],\n",
" [2.37501604e-02, 4.60245354e-03, 3.08355898e-05, ...,\n",
" 3.78508037e-01, 5.27398289e-01, 6.84647894e-01],\n",
" [9.28698653e-03, 4.92553224e-04, 1.92236215e-02, ...,\n",
" 3.78536806e-01, 5.27483448e-01, 6.84647893e-01],\n",
" [5.01933968e-03, 5.74106379e-04, 9.16535035e-05, ...,\n",
" 3.78557173e-01, 5.25713579e-01, 6.84648014e-01]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_all = []\n",
"for state_folder in state_folders:\n",
" json_file_path = os.path.join(state_folder, 'matrix', 'D.json')\n",
" with open(json_file_path, 'r') as file:\n",
" data = json.load(file)\n",
" #json_file_path = os.path.join(state_folder, 'matrix', 'D.json')\n",
" #with open(json_file_path, 'r') as file:\n",
" # data = json.load(file)\n",
" url = f\"https://raw.githubusercontent.com/ModelEarth/io/main/build/api/{state_folder}/matrix/D.json\"\n",
" data = requests.get(url).json()\n",
" data_all.append(np.array(data).reshape(-1))\n",
"data_all = np.array(data_all)\n",
"data_all"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "1e11e2f3-3ed7-4699-8f69-48e2ac7c02d8",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"(5, 2336)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_all.shape"
]
Expand All @@ -78,7 +114,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "493a0e1b-6829-4cdc-a74f-36db8ba60998",
"metadata": {},
"outputs": [],
Expand All @@ -95,10 +131,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "18fd1b4f-4884-4955-bb95-624c06897122",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[-3.29386491e+02, -1.38102639e+02, -4.35914067e+01],\n",
" [-3.54474781e+02, -1.23905093e+02, -1.26811229e+02],\n",
" [-4.40413592e+02, -1.03079475e+02, 1.58609087e+02],\n",
" [ 8.59300724e+02, -3.89594967e+02, 1.24827146e+01],\n",
" [ 2.64974140e+02, 7.54682175e+02, -6.89165907e-01]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_reduced"
]
Expand All @@ -113,10 +164,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "09ea0e8c-5317-4cb2-9312-853d3e25cfec",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cluster labels: [0 0 0 2 1]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\Python\\Lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n",
" super()._check_params_vs_input(X, default_n_init=10)\n"
]
}
],
"source": [
"from sklearn.cluster import KMeans\n",
"n_clusters = 3\n",
Expand All @@ -128,10 +195,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "17d0f565-f9f6-4b40-b444-28953a140e82",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'GAEEIOv1.0': 0,\n",
" 'MEEEIOv1.0': 0,\n",
" 'MNEEIOv1.0': 0,\n",
" 'OREEIOv1.0': 2,\n",
" 'WAEEIOv1.0': 1}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cluster_result = dict(zip(state_folders, cluster_labels))\n",
"cluster_result"
Expand All @@ -144,14 +226,6 @@
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c323c35d-3b6d-4aea-a116-0897417cb5e2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -170,7 +244,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
File renamed without changes.
Loading

0 comments on commit ab4aeed

Please sign in to comment.