Skip to content

Commit

Permalink
Simplify log ratios in Trask notebooks
Browse files Browse the repository at this point in the history
The original calculations of log ratios for words
with negative sentiment are unnecessarily complex.
Simplify them and add some explanation.
  • Loading branch information
clapollo committed May 19, 2017
1 parent d1541a8 commit 6f855ae
Show file tree
Hide file tree
Showing 2 changed files with 2,949 additions and 2,809 deletions.
46 changes: 32 additions & 14 deletions sentiment-network/Sentiment_Classification_Projects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"nbpresent": {
"id": "eba2b193-0419-431e-8db9-60f34dd3fe83"
}
Expand Down Expand Up @@ -105,7 +106,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"len(reviews)"
Expand All @@ -115,6 +118,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"nbpresent": {
"id": "bb95574b-21a0-4213-ae50-34363cf4f87f"
}
Expand All @@ -128,6 +132,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"nbpresent": {
"id": "e0408810-c424-4ed4-afb9-1735e9ddbd0a"
}
Expand All @@ -148,6 +153,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"nbpresent": {
"id": "e67a709f-234f-4493-bae6-4fb192141ee0"
}
Expand Down Expand Up @@ -196,7 +202,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create three Counter objects to store positive, negative and total counts\n",
Expand Down Expand Up @@ -318,11 +326,9 @@
"\n",
"To fix these issues, we'll convert all of our ratios to new values using logarithms.\n",
"\n",
"**TODO:** Go through all the ratios you calculated and convert their values using the following formulas:\n",
"> * For any postive words, convert the ratio using `np.log(ratio)`\n",
"> * For any negative words, convert the ratio using `-np.log(1/(ratio + 0.01))`\n",
"**TODO:** Go through all the ratios you calculated and convert them to logarithms. (i.e. use `np.log(ratio)`)\n",
"\n",
"That second equation may look strange, but what it's doing is dividing one by a very small number, which will produce a larger positive number. Then, it takes the `log` of that, which produces numbers similar to the ones for the postive words. Finally, we negate the values by adding that minus sign up front. In the end, extremely positive and extremely negative words will have positive-to-negative ratios with similar magnitudes but oppositite signs."
"In the end, extremely positive and extremely negative words will have positive-to-negative ratios with similar magnitudes but opposite signs."
]
},
{
Expand Down Expand Up @@ -415,7 +421,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
Expand All @@ -428,7 +436,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"review = \"The movie was excellent\"\n",
Expand All @@ -448,7 +458,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# TODO: Create set named \"vocab\" containing all of the words from all of the reviews\n",
Expand Down Expand Up @@ -484,7 +496,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -543,7 +557,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
Expand Down Expand Up @@ -1082,7 +1098,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
Expand Down Expand Up @@ -1854,7 +1872,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -1868,7 +1886,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.5.2"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 6f855ae

Please sign in to comment.