{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## HashingVectorizer\n", "* HashingVectorizer is the combination of FeatureHasher and CountVectorizer i.e, we get the Term frequency of words as well as the reduced dimension\n", "* When we used FeatureHasher the dimension of vector reduced but we couldn't get any understandable format in output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### If we have large vocabulary of words we can choose to use the HashingVectorizer rather than the CountVectorizer\n", "* The use of hashing buckets to represent words allows us to scale large data sets when we use the HashingVectorizer.\n", "* The input argument to the vectorizer is the number of hash buckets (n_features)\n", "* Result : numeric representation of all the words in documents.\n", "* Word ids are from 0 to (n_features - 1) because total of n_features buckets.\n", "* Because the size of vocabulary is larger than the number of buckets, multiple words can hash to the same bucket.\n", "* No way to get back to the original value from the hash bucket value.\n", "* Frequencies of each is represented in normalized from\n", "\n", "https://scikit-learn.org/stable/modules/feature_extraction.html#vectorizing-a-large-text-corpus-with-the-hashing-trick" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### words are mapped directly to indices with a hashing function" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 27)" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.feature_extraction.text import CountVectorizer\n", "\n", "text_array = [\"Good things come to those who wait.\",\n", " \"These watches cost $1500! \",\n", " \"These are other fish in the sea.\",\n", " \"The ball is in your court.\",\n", " \"Mr. Smith Goes to Washington \",\n", " \"Doogie Howser M.D.\"]\n", "\n", "count_vectorizer = CountVectorizer()\n", "feature_vector = count_vectorizer.fit_transform(text_array)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,\n", " 1, 0, 0, 1, 0],\n", " [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,\n", " 0, 0, 1, 0, 0],\n", " [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0,\n", " 0, 0, 0, 0, 0],\n", " [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 1],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1,\n", " 0, 1, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0, 0]], dtype=int64)" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feature_vector.toarray()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'good': 9,\n", " 'things': 19,\n", " 'come': 3,\n", " 'to': 21,\n", " 'those': 20,\n", " 'who': 25,\n", " 'wait': 22,\n", " 'these': 18,\n", " 'watches': 24,\n", " 'cost': 4,\n", " '1500': 0,\n", " 'are': 1,\n", " 'other': 14,\n", " 'fish': 7,\n", " 'in': 11,\n", " 'the': 17,\n", " 'sea': 15,\n", " 'ball': 2,\n", " 'is': 12,\n", " 'your': 26,\n", " 'court': 5,\n", " 'mr': 13,\n", " 'smith': 16,\n", " 'goes': 8,\n", " 'washington': 23,\n", " 'doogie': 6,\n", " 'howser': 10}" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_vectorizer.vocabulary_" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "analyzer = count_vectorizer.build_analyzer()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['good', 'things', 'come', 'to', 'those', 'who', 'wait']" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "word_tokens = analyzer(text_array[0])\n", "\n", "word_tokens" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "frequency_list = []\n", "\n", "for i, text in enumerate(text_array):\n", " tokens = analyzer(text)\n", " \n", " word_frequency = {}\n", "\n", " for token in tokens:\n", " word_idx = count_vectorizer.vocabulary_[token]\n", " \n", " word_frequency[token] = feature_vector[i, word_idx]\n", " \n", " frequency_list.append(word_frequency)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'good': 1, 'things': 1, 'come': 1, 'to': 1, 'those': 1, 'who': 1, 'wait': 1},\n", " {'these': 1, 'watches': 1, 'cost': 1, '1500': 1},\n", " {'these': 1, 'are': 1, 'other': 1, 'fish': 1, 'in': 1, 'the': 1, 'sea': 1},\n", " {'the': 1, 'ball': 1, 'is': 1, 'in': 1, 'your': 1, 'court': 1},\n", " {'mr': 1, 'smith': 1, 'goes': 1, 'to': 1, 'washington': 1},\n", " {'doogie': 1, 'howser': 1}]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "frequency_list" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 8)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.feature_extraction import FeatureHasher\n", "\n", "hasher = FeatureHasher(n_features=8, input_type='string')\n", "hashed_features = hasher.fit_transform(frequency_list)\n", "\n", "hashed_features.shape" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 3., 0., 1., -2., -1., 0., 0.],\n", " [ 0., 0., -1., 0., -1., 0., 2., 0.],\n", " [ 0., 1., -1., 0., 0., 0., 0., 1.],\n", " [ 0., 1., 0., 0., 0., 1., 0., 0.],\n", " [ 0., -1., 0., 1., 0., -1., 0., 0.],\n", " [ 0., 1., 0., 0., 1., 0., 0., 0.]])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hashed_features.toarray()" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 8)" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.feature_extraction.text import HashingVectorizer\n", "\n", "vectorizer = HashingVectorizer(n_features=8, norm=None)\n", "feature_vector = vectorizer.transform(text_array)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 3., 0., 1., -2., -1., 0., 0.],\n", " [ 0., 0., -1., 0., -1., 0., 2., 0.],\n", " [ 0., 1., -1., 0., 0., 0., 0., 1.],\n", " [ 0., 1., 0., 0., 0., 1., 0., 0.],\n", " [ 0., -1., 0., 1., 0., -1., 0., 0.],\n", " [ 0., 1., 0., 0., 1., 0., 0., 0.]])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feature_vector.toarray()" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 8)" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vectorizer = HashingVectorizer(n_features=8, norm='l1')\n", "feature_vector = vectorizer.transform(text_array)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 0.42857143, 0. , 0.14285714, -0.28571429,\n", " -0.14285714, 0. , 0. ],\n", " [ 0. , 0. , -0.25 , 0. , -0.25 ,\n", " 0. , 0.5 , 0. ],\n", " [ 0. , 0.33333333, -0.33333333, 0. , 0. ,\n", " 0. , 0. , 0.33333333],\n", " [ 0. , 0.5 , 0. , 0. , 0. ,\n", " 0.5 , 0. , 0. ],\n", " [ 0. , -0.33333333, 0. , 0.33333333, 0. ,\n", " -0.33333333, 0. , 0. ],\n", " [ 0. , 0.5 , 0. , 0. , 0.5 ,\n", " 0. , 0. , 0. ]])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feature_vector.toarray()" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 8)" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vectorizer = HashingVectorizer(n_features=8, norm='l2')\n", "feature_vector = vectorizer.transform(text_array)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 0.77459667, 0. , 0.25819889, -0.51639778,\n", " -0.25819889, 0. , 0. ],\n", " [ 0. , 0. , -0.40824829, 0. , -0.40824829,\n", " 0. , 0.81649658, 0. ],\n", " [ 0. , 0.57735027, -0.57735027, 0. , 0. ,\n", " 0. , 0. , 0.57735027],\n", " [ 0. , 0.70710678, 0. , 0. , 0. ,\n", " 0.70710678, 0. , 0. ],\n", " [ 0. , -0.57735027, 0. , 0.57735027, 0. ,\n", " -0.57735027, 0. , 0. ],\n", " [ 0. , 0.70710678, 0. , 0. , 0.70710678,\n", " 0. , 0. , 0. ]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feature_vector.toarray()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 3., 0., 1., 2., 1., 0., 0.],\n", " [0., 0., 1., 0., 1., 0., 2., 0.],\n", " [2., 1., 1., 0., 0., 0., 2., 1.],\n", " [0., 1., 0., 0., 2., 1., 2., 0.],\n", " [0., 1., 0., 3., 0., 1., 0., 0.],\n", " [0., 1., 0., 0., 1., 0., 0., 0.]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vectorizer = HashingVectorizer(n_features=8, norm=None, alternate_sign=False)\n", "feature_vector = vectorizer.transform(text_array)\n", "\n", "feature_vector.toarray()" ] }, { "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }