M2_SETI/D3/TP/TP_SETI_Kmeans/test.ipynb

243 lines
6.6 KiB
Text
Raw Normal View History

2022-11-28 11:40:47 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import scipy.spatial"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"clusters = 3\n",
"mean = np.random.randint(5, size=clusters)\n",
"sd = [0.25, 0.25, 0.3]\n",
"dim = 2\n",
"nb = 50\n",
"K= clusters"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"def gen_points(mean=1,sd=0.5, nb=100, dim=2, clusters=2):\n",
" size = []\n",
" # for i in range(0,dim):\n",
" size.append(nb)\n",
" size.append(dim)\n",
" points = np.random.normal(mean[0],sd[0],size=size)\n",
" for i in range(1,clusters):\n",
" points = np.concatenate((points,np.random.normal(mean[i],sd[i],size=size)),axis=0)\n",
" \n",
" return points"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"def distance(points,Pc): \n",
" return scipy.spatial.distance.cdist(points[:,:], Pc[:,:])"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"def kmeans(points = [0,0], K = 1, nb=1, dim=2):\n",
" # Initialisation K prototypes\n",
" Pc_index = []\n",
" Pc_save = np.zeros([K,dim])\n",
" clusters = []\n",
" iter = 0\n",
" eps = 0.1\n",
"\n",
" for i in range(0,K):\n",
" Pc_index.append(np.random.randint(0,nb))\n",
" Pc = points[Pc_index,:]\n",
"\n",
" # print(Pc.shape)\n",
" # print(points.shape)\n",
"\n",
" while (np.mean(distance(Pc,Pc_save)) > eps and iter < 10):\n",
" iter += 1\n",
" Pc_save = Pc\n",
" # print(Pc.shape[1])\n",
" # toto = points[:,:Pc.shape[0]]\n",
" # print(toto.shape)\n",
" dist = distance(points=points[:,:Pc.shape[1]],Pc=Pc)\n",
" clust = np.argmin(dist, axis=1)\n",
" clust = np.expand_dims(clust, axis=0)\n",
" points = np.append(points[:,:Pc.shape[1]], clust.T, axis=1)\n",
" # print(points)\n",
" Pc = np.zeros([K,dim])\n",
" index = np.array([])\n",
"\n",
" for n in range(0,2*nb):\n",
" for k in range(0,K):\n",
" index = np.append(index, (clust==k).sum())\n",
" if points[n,-1] == k:\n",
" # print(points)\n",
" # print(Pc)\n",
" Pc[k,:] = np.add(Pc[k,:], points[n,:-1])\n",
"\n",
" for k in range(0,K):\n",
" Pc[k,:] = np.divide(Pc[k,:],index[k])\n",
"\n",
" # print(Pc)\n",
" return Pc, points\n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"points = gen_points(mean,sd,nb,dim,clusters)\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"Pc, clusters = kmeans(points,K=K,nb=nb,dim=dim)\n"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"def img_2_mat(my_img):\n",
" mat = my_img.reshape(my_img.shape[0]*my_img.shape[1],my_img.shape[2])\n",
" return mat\n",
"\n",
"def mat_2_img(mat,my_img):\n",
" img_seg = mat.reshape(my_img.shape[0], my_img.shape[1], my_img.shape[2])\n",
" return img_seg\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(103230, 3)\n"
]
}
],
"source": [
"from skimage import io\n",
"\n",
"path_image = \"fruits.jpg\"\n",
"my_img = io.imread(path_image)\n",
"Mat = img_2_mat(my_img)\n",
"print(Mat.shape)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]]\n",
"\n",
" [[0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]]]\n",
"[[0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]\n",
" [0. 1. 2.]]\n"
]
},
{
"ename": "IndexError",
"evalue": "tuple index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [108], line 11\u001b[0m\n\u001b[1;32m 7\u001b[0m B \u001b[39m=\u001b[39m img_2_mat(A)\n\u001b[1;32m 9\u001b[0m \u001b[39mprint\u001b[39m(B)\n\u001b[0;32m---> 11\u001b[0m A \u001b[39m=\u001b[39m mat_2_img(B,A)\n\u001b[1;32m 13\u001b[0m \u001b[39mprint\u001b[39m(A)\n",
"Cell \u001b[0;32mIn [106], line 6\u001b[0m, in \u001b[0;36mmat_2_img\u001b[0;34m(mat, my_img)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmat_2_img\u001b[39m(mat,my_img):\n\u001b[0;32m----> 6\u001b[0m img_seg \u001b[39m=\u001b[39m mat\u001b[39m.\u001b[39mreshape(my_img\u001b[39m.\u001b[39mshape[\u001b[39m0\u001b[39m], my_img\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m], my_img\u001b[39m.\u001b[39;49mshape[\u001b[39m3\u001b[39;49m])\n\u001b[1;32m 7\u001b[0m \u001b[39mreturn\u001b[39;00m img_seg\n",
"\u001b[0;31mIndexError\u001b[0m: tuple index out of range"
]
}
],
"source": [
"A = np.zeros((2,3,3))\n",
"for i in range(3):\n",
" A[:,:,i] = i\n",
"\n",
"print(A)\n",
"\n",
"B = img_2_mat(A)\n",
"\n",
"print(B)\n",
"\n",
"A = mat_2_img(B,A)\n",
"\n",
"print(A)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"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.10"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}