k-means clustering
This commit is contained in:
		
							
								
								
									
										206
									
								
								Project/Tools/kmeans_on_Emojis.ipynb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								Project/Tools/kmeans_on_Emojis.ipynb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,206 @@ | ||||
| { | ||||
|  "cells": [ | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 13, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [ | ||||
|     "import sys\n", | ||||
|     "import numpy as np\n", | ||||
|     "from sklearn.cluster import KMeans\n", | ||||
|     "sys.path.append(\"..\")\n", | ||||
|     "\n", | ||||
|     "from Tools.Emoji_Distance import sentiment_vector_to_emoji\n", | ||||
|     "from Tools.Emoji_Distance import emoji_to_sentiment_vector\n", | ||||
|     "from Tools.Emoji_Distance import dataframe_to_dictionary\n", | ||||
|     "\n", | ||||
|     "def emoji2sent(emoji_arr):\n", | ||||
|     "    return np.array([emoji_to_sentiment_vector(e) for e in emoji_arr])\n", | ||||
|     "\n", | ||||
|     "def sent2emoji(sent_arr, custom_target_emojis=None):\n", | ||||
|     "    return [sentiment_vector_to_emoji(s, custom_target_emojis=custom_target_emojis) for s in sent_arr]" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 8, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [ | ||||
|     "data , data_only_emoticons, list_sentiment_vectors , list_emojis , list_sentiment_emoticon_vectors , list_emoticon_emojis = dataframe_to_dictionary()" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 10, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "data": { | ||||
|       "text/plain": [ | ||||
|        "array([[0.46813021, 0.24716181, 0.28470797],\n", | ||||
|        "       [0.72967448, 0.05173769, 0.21858783],\n", | ||||
|        "       [0.34310532, 0.43648208, 0.2204126 ],\n", | ||||
|        "       [0.75466009, 0.0529057 , 0.19243421],\n", | ||||
|        "       [0.70401758, 0.05932203, 0.23666039],\n", | ||||
|        "       [0.57697579, 0.12699863, 0.29602558],\n", | ||||
|        "       [0.22289823, 0.59126106, 0.18584071],\n", | ||||
|        "       [0.49837557, 0.0805718 , 0.42105263],\n", | ||||
|        "       [0.44415243, 0.11169514, 0.44415243],\n", | ||||
|        "       [0.5634451 , 0.09927679, 0.33727811]])" | ||||
|       ] | ||||
|      }, | ||||
|      "execution_count": 10, | ||||
|      "metadata": {}, | ||||
|      "output_type": "execute_result" | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "array_sentiment_vectors = np.array(list_sentiment_emoticon_vectors)\n", | ||||
|     "array_sentiment_vectors[:10]" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 42, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [ | ||||
|     "kmeans = KMeans(n_clusters=5, random_state=0).fit(array_sentiment_vectors)" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 43, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "data": { | ||||
|       "text/plain": [ | ||||
|        "array([[0.43555605, 0.2777192 , 0.28672476],\n", | ||||
|        "       [0.21254481, 0.57576584, 0.21168936],\n", | ||||
|        "       [0.56669216, 0.13017252, 0.30313532],\n", | ||||
|        "       [0.33453667, 0.45309312, 0.21237021],\n", | ||||
|        "       [0.71664806, 0.06648547, 0.21686647]])" | ||||
|       ] | ||||
|      }, | ||||
|      "execution_count": 43, | ||||
|      "metadata": {}, | ||||
|      "output_type": "execute_result" | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "centers = kmeans.cluster_centers_\n", | ||||
|     "centers" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 44, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "name": "stdout", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "🙇\n", | ||||
|       "😿\n", | ||||
|       "😄\n", | ||||
|       "😭\n", | ||||
|       "😍\n" | ||||
|      ] | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "for center in centers:\n", | ||||
|     "    print(sentiment_vector_to_emoji(center))" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "markdown", | ||||
|    "metadata": {}, | ||||
|    "source": [ | ||||
|     "* only most used emojis" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 46, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [ | ||||
|     "top_emojis = [('😂', 10182),\n", | ||||
|     " ('😭', 3893),\n", | ||||
|     " ('😍', 2866),\n", | ||||
|     " ('😩', 1647),\n", | ||||
|     " ('😊', 1450),\n", | ||||
|     " ('😘', 1151),\n", | ||||
|     " ('🙏', 1089),\n", | ||||
|     " ('🙌', 1003),\n", | ||||
|     " ('😉', 752),\n", | ||||
|     " ('😁', 697),\n", | ||||
|     " ('😅', 651),\n", | ||||
|     " ('😎', 606),\n", | ||||
|     " ('😢', 544),\n", | ||||
|     " ('😒', 539),\n", | ||||
|     " ('😏', 478),\n", | ||||
|     " ('😌', 434),\n", | ||||
|     " ('😔', 415),\n", | ||||
|     " ('😋', 397),\n", | ||||
|     " ('😀', 392),\n", | ||||
|     " ('😤', 368)]" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": 47, | ||||
|    "metadata": {}, | ||||
|    "outputs": [ | ||||
|     { | ||||
|      "name": "stdout", | ||||
|      "output_type": "stream", | ||||
|      "text": [ | ||||
|       "😂\n", | ||||
|       "😒\n", | ||||
|       "😁\n", | ||||
|       "😭\n", | ||||
|       "😍\n" | ||||
|      ] | ||||
|     } | ||||
|    ], | ||||
|    "source": [ | ||||
|     "for center in centers:\n", | ||||
|     "    print(sentiment_vector_to_emoji(center, custom_target_emojis=top_emojis))" | ||||
|    ] | ||||
|   }, | ||||
|   { | ||||
|    "cell_type": "code", | ||||
|    "execution_count": null, | ||||
|    "metadata": {}, | ||||
|    "outputs": [], | ||||
|    "source": [] | ||||
|   } | ||||
|  ], | ||||
|  "metadata": { | ||||
|   "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.6.4" | ||||
|   } | ||||
|  }, | ||||
|  "nbformat": 4, | ||||
|  "nbformat_minor": 2 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user