thesis-prediction-ml/building-features-from-text-data/09-ReduceDimensionsInText_HashingTrick.ipynb
2023-06-18 14:45:35 +02:00

197 lines
5.7 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hashing trick"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instead of building a hash table of the features encountered in training, as the vectorizers do, instances of FeatureHasher apply a hash function to the features to determine their column index in sample matrices directly. The result is increased speed and reduced memory usage, at the expense of inspectability; the hasher does not remember what the input features looked like and has no inverse_transform method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html\n",
"* https://scikit-learn.org/stable/modules/feature_extraction.html#feature-hashing"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 8)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.feature_extraction import FeatureHasher\n",
"\n",
"text = [\"A bird in hand is worth two in the bush.\",\n",
" \"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",
"hasher = FeatureHasher(n_features=8, input_type='string')\n",
"hashed_features = hasher.fit_transform(text)\n",
"\n",
"hashed_features.shape"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 10., 0., 1., 1., -3., -11., 1., -1.],\n",
" [ 6., -2., 0., 3., 0., -6., 0., 0.],\n",
" [ 4., -5., 1., -1., 2., -3., 0., 0.],\n",
" [ 6., 2., 2., 0., -1., -8., 0., 3.],\n",
" [ 7., 1., 1., 1., 1., -6., 1., 0.],\n",
" [ 4., 2., 1., 0., -2., -5., 0., -1.],\n",
" [ 2., 1., 0., 3., 0., -3., 2., 1.]])"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hashed_features.toarray()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 16)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hasher = FeatureHasher(n_features=16, input_type='string')\n",
"hashed_features = hasher.fit_transform(text)\n",
"\n",
"hashed_features.shape"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 0., 1., 1., -3., -4., 0., 1., 9., 0., 0., 0., 0.,\n",
" -7., 1., -2.],\n",
" [ 0., -3., 1., 3., -1., -2., 0., 2., 6., 1., -1., 0., 1.,\n",
" -4., 0., -2.],\n",
" [ 0., -5., 1., -1., 2., 1., 0., 3., 4., 0., 0., 0., 0.,\n",
" -4., 0., -3.],\n",
" [ 0., 2., 2., -1., -1., -2., 0., 6., 6., 0., 0., 1., 0.,\n",
" -6., 0., -3.],\n",
" [ 2., 1., 1., 1., -1., -2., 1., 1., 5., 0., 0., 0., 2.,\n",
" -4., 0., -1.],\n",
" [-1., 1., 1., 0., -2., -2., 0., 1., 5., 1., 0., 0., 0.,\n",
" -3., 0., -2.],\n",
" [ 0., 0., 0., 3., 0., -1., 2., 2., 2., 1., 0., 0., 0.,\n",
" -2., 0., -1.]])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hashed_features.toarray()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'FeatureHasher' object has no attribute 'inverse_transform'",
"output_type": "error",
"traceback": [
"\u001b[0;31m--------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-44-acc2ce62ec0e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhasher\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minverse_transform\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhashed_features\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'FeatureHasher' object has no attribute 'inverse_transform'"
]
}
],
"source": [
"hasher.inverse_transform(hashed_features)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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
}