Notebooks
M
MongoDB
Tensorflow Mongodbcharts Horoscopes

Tensorflow Mongodbcharts Horoscopes

mlagentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag

Open In Colab

View Article

Sentiment Analysis on my scraped horoscopes

Our first step is to do sentiment analysis on our .csv file of scraped horoscopes. Luckily for us, at this point in the tutorial we don't need to build a model (yet!), we can use a pre-trained model to figure out whether or not our horoscopes from the past six months are positive or negative.

I am using this tutorial here from Medium, please feel free to take a look at it to better understand the code used below.

[ ]

Once everything is imported in, let's choose which pre-trained model we want to use. Since this is a TensorFlow tutorial, we can go ahead and use the "distilbert-base-uncased-finetuned-sst-2-english" model since it's compatible with TensorFlow, but there are a ton of options out there to choose from if you would like to switch it up.

[ ]
/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: 
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
  warnings.warn(
tokenizer_config.json:   0%|          | 0.00/48.0 [00:00<?, ?B/s]
config.json:   0%|          | 0.00/629 [00:00<?, ?B/s]
vocab.txt:   0%|          | 0.00/232k [00:00<?, ?B/s]
model.safetensors:   0%|          | 0.00/268M [00:00<?, ?B/s]
All PyTorch model weights were used when initializing TFDistilBertForSequenceClassification.

All the weights of TFDistilBertForSequenceClassification were initialized from the PyTorch model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFDistilBertForSequenceClassification for predictions without further training.
Horoscope is NEGATIVE

This is great! As we can see, that Capricorn horoscope is in fact negative, and we were able to use our pre-trained model to classify it. But, now we need to make some changes because we don't want to put in everything manually, we want to use this pre-trained model and put in our .csv file of all our horoscopes and figure out the sentiment analysis of everything in our file, while also incorporating in a new "sentiment" column that will include 1's if the horoscope is positive and 0 if the horoscope is negative.

[ ]

Now load our "anaiya-six-months-horoscopes.csv" file

[ ]
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (2.1.4)
Requirement already satisfied: numpy<2,>=1.22.4 in /usr/local/lib/python3.10/dist-packages (from pandas) (1.26.4)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas) (2024.1)
Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas) (2024.1)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
[ ]
[ ]
[ ]
saved to new file called anaiya-six-months-horoscopes-sentiment.csv
[ ]

Save our new .csv file into MongoDB Atlas so we can visualize our data in MongoDB Charts

This part is done using MongoDB Compass and MongoDB Charts.

TRAIN AND TEST MODEL WITH TENSORFLOW

Now that we have our dataset ready with our sentiment analysis done using our pre-trained model, we can go ahead and set up a way to train and test our data so that if we wanted to incorporate new horoscopes, we can see if they will be negative or positive.

In order to help me learn how to do this, I watched this video from freeCodeCamp.org: https://www.youtube.com/watch?v=VtRLrQ3Ev-U, and I used the skeleton code from this TensorFlow docs: https://www.tensorflow.org/tutorials/structured_data/preprocessing_layers

Feel free to watch it to get a better understanding of the code used below.

[ ]
[ ]

We want to split up our dataset into three sets. We need a training set, a validation set, and a test set.

BALANCE DATASET

We need to balance our dataset since we need to make sure our model is trained on the same exact amount of negative and positive horoscopes, otherwise things will be swayed in one direction or the other. Check out this article for help on how to balance your dataset: https://medium.com/@daniele.santiago/balancing-imbalanced-data-undersampling-and-oversampling-techniques-in-python-7c5378282290 and https://semaphoreci.com/blog/imbalanced-data-machine-learning-python

[ ]
[ ]
[ ]
[ ]
sentiment
0    495
1    495
Name: count, dtype: int64

SPLIT UP OUR DATASET

[ ]
[ ]
Training set: 792
Validation set: 198
Test set: 330
[ ]

CONVERT TO TENSORFLOW DATASET

Now, let's convert our dataframes to TensorFlow datasets. Use this code from the documentation: https://www.tensorflow.org/tutorials/structured_data/preprocessing_layers

it converts each train, validation, and test dataset into a tensorflow dataset and will shuffle again and batch the data for you.

[ ]
[ ]
[ ]
[ ]
(<tf.Tensor: shape=(32,), dtype=string, numpy=
, array([b'Feb 13, 2024 - Machines involved in financial transactions, such as ATMs, phone systems, or banking websites could malfunction today, Gemini, so you might have to resort to dealing with money in the old fashioned way: by going into the bank or writing checks. Electrical storms or solar flares could be interfering with satellite signals, so there isn\xe2\x80\x99t much you can do. Needless to say, this isn\xe2\x80\x99t a good day to make any major financial transactions.',
,        b"Feb 26, 2024 - There could be a missing person very much on your mind these days. Is it possible that the relationship is over and you're the last one to know? Don't let your insecurities get the better of you, Sagittarius. It's likely that your friend merely needs some time alone to sort out some big life issues. He or she will seek out your warmth and friendship again soon.",
,        b'Jun 5, 2024 - It\xe2\x80\x99s time to get in touch with the people in your neighborhood, Aries. By reaching out to the people you live near in a jovial and congenial way you build community spirit. You can expect a lot of stimulating conversation once you take the initiative. Valuable business contacts could be made today, although you might not be aware of it at the time.',
,        b"Feb 1, 2024 - Today is a great day for transformation, Aries, and your actions have deeper than usual inner meaning. Be careful where you aim your arrow, because the tip of it has extra sting today. People might be rubbing you the wrong way, making you feel like you\xe2\x80\x99re a pot of water on a hot plate. The heat is turned up and the cover is about to blow. Make sure you don't hit anyone when the lid flies off.",
,        b"Jul 19, 2024 - Your insight, creativity, and inventiveness are in full swing, Cancer, and new ideas come thick and fast. However, you might find yourself too caught up in boring, mundane tasks to do very much about it. Yet physical energy is plentiful, so you have it in you to take care of chores and then have time to do what you enjoy. You'll produce results if you remember to pace yourself. Work too hard and you'll be too tired!",
,        b"May 31, 2024 - Today you're likely focused on sex, romance, and committed relationships. The planetary energies surrounding love are promising, Aries. If you're in a relationship, you and your partner could make plans to embark on a new enterprise together, which is likely to succeed. If you're involved but not committed, expect the relationship to move forward now. If you aren't with someone, who knows? By the end of the day you might be.",
,        b'Feb 25, 2024 - Certainly there are advantages and disadvantages to the high level of intuition you have. Today you may feel weighed down by some strange thoughts that enter your head. Only time will tell if they are true premonitions or simply weird daydreams. In the meantime, you\xe2\x80\x99d probably be best served by ignoring them completely. Life is too short to play the "what if" game.',
,        b"Apr 8, 2024 - A colleague could be in a very bad mood, Taurus, and therefore not the easiest person in the world to deal with. In fact, today he or she could resist working at all, and you might feel obligated to take up the slack. Only do it if the tasks are urgent. It isn't fair to you to have to do someone else's job. Don't feel guilty if you leave it unfinished.",
,        b"Apr 28, 2024 - A friend might be in such a dour mood that you wonder if this is the same person you know and love, Gemini. During the day you might be tempted to try to analyze what's going on, but this is apt to raise more questions than answers. Probably this is a case of too many problems hitting your friend at once. Don't pry, but make sure your friend knows you're there if needed.",
,        b"Jun 1, 2024 - You're normally intuitive, Leo, but today you might feel more psychic than you ever dreamed you could. Information received from the media could have you picking up psychically on the thoughts and feelings of people you've never met. This can be emotionally overpowering, so protect yourself by keeping the white light around you. Channel your thoughts and feelings into art of some kind. It helps.",
,        b"Mar 2, 2024 - Today you might feel stifled by your current situation. Perhaps you're thinking of changing jobs or professions, or maybe you're considering moving to a more exotic place. These all might be good ideas, Cancer, but today isn't the day to make any definite decisions or even consider your options. You aren't in an objective frame of mind. Wait a few days and then think about this some more. You might change your mind.",
,        b"Mar 3, 2024 - Today should find you continuing to work toward cherished career and other goals. Love matters may come to the forefront, Virgo. If you're currently involved, unexpected events could bring you that much closer to your partner. If you aren't involved, you could meet someone exciting, perhaps through some kind of group activities. Also, a longtime friend could suddenly appear to you in a new light. Whichever it is, expect some interesting developments.",
,        b'Jan 31, 2024 - You may be anxious to fit things in your life into pigeonholes, Gemini. When it comes to matters of love and romance, this may be hard to do, especially on a day like today. Call upon your pioneering spirit to seek something new and not limit yourself to what you originally had in mind. Perhaps there is something bigger and better waiting for you. You\xe2\x80\x99ll only find it if you dare to accept something outside the norm.',
,        b"Jul 18, 2024 - You could be feeling the pressure of time now, Libra. At this point in your life you may be more aware than ever of your limited time left on this planet. Perhaps you're thinking more about love and romance and how much they mean to you. Consider the limitations of each, but don't dwell on them. The important thing is to embrace today and make the most of the time and love you have now.",
,        b"Apr 27, 2024 - A female visitor could come to your door today with some interesting, useful information, Leo. It might involve anything from stock market trends to a forthcoming wedding to occult and metaphysical matters. Whatever it is, you'll find it captivating, and might just sit and listen to your guest for quite a while. By the time she leaves, your mind may well be spinning. Take a walk to clear your head or you'll be awake all night.",
,        b'Feb 6, 2024 - Why not take some much deserved rest and relaxation today, Scorpio? After all, even you need to jump out of the rat race once in a while. If there are some things you really need to care of, you can still make plans for leisure or recreation afterward. Spoil yourself with an afternoon nap or ordering out for dinner. Take a leisurely walk or a long bath. Take care of yourself by resting as well as accomplishing.',
,        b"Jun 12, 2024 - Sometimes we all feel a little lost, Taurus. We often want to move mountains and use all our energy and enthusiasm to try to do so. But a few moments later we could feel that all efforts have been in vain. It pays to remember one of the hard facts of life - take nothing for granted. You shouldn't let that keep you from trying to make changes to things around you. Be vigilant.",
,        b"Apr 8, 2024 - This might prove to be one of those days in which it's very hard to get anything related to work or communication off the ground, Virgo. You could find excuses to stop working more often than usual, particularly if it involves calling people on the phone. Don't fight it. Take care of the most urgent tasks. The world won't come to an end if you put the rest off a while.",
,        b"Jun 6, 2024 - Today you might find that your social life takes a definite turn for the better. You could receive more than one text or call that piques your interest, Scorpio, and you\xe2\x80\x99ll spend a lot of time discussing arrangements for future get-togethers. If you aren't romantically involved now, you may find out about someone promising. If you're involved, make sure you reserve time for an intimate encounter with your special someone, if possible. Have fun.",
,        b"Jul 7, 2024 - Get your head out of the clouds, Leo. You'll find out that nitpicky details you failed to attend to earlier are now coming back to haunt you. Don't postpone your work any further. The time to take care of the job is now. Balance your checkbook and do your laundry. Clean your bathtub and go grocery shopping. The more you accomplish today, the better you'll feel about yourself tomorrow.",
,        b'Apr 14, 2024 - You might be feeling a little tense because you and your partner are grappling with some thorny financial issues right now, Gemini. You could be frustrated because you need to postpone some purchases or investments until the cash starts flowing more freely again. The good news is that this is a fine time to work out such a conflict to a satisfactory resolution. Both of you will need to be open-minded and look at all sides as you make your decisions.',
,        b"May 25, 2024 - Things that have been out of whack in your life should begin to come back into balance, Pisces. The bad news is that it may take a while for the pendulum to return to the optimal point. The good news is that this shift is taking place at all. It will be a gradual change, but you're at a pivotal point in which you can set your sights on exactly what you want.",
,        b"Jun 16, 2024 - Some people around you might think that you have never really matured, Scorpio, that you still have the mind of a child. It might not seem obvious at first, but your lighthearted attitude is also a sign of great wisdom. As with those who have truly committed to long-term romances, you will find out that you'll never lose your lightheartedness.",
,        b"Mar 7, 2024 - Communication with a significant other might hit a brick wall, and you may not be able to get through. Money could be coming in the mail, Pisces, but probably won't arrive today. This could be a day full of frustrations, but the evening should make up for it. Love and romance look great right now. This is a wonderful day to schedule an evening with your partner - or to look for one. Enjoy!",
,        b"Jul 17, 2024 - Ingenious techniques for growing your money could come your way today, Gemini. While you aren't one to jump into anything, you're certainly likely to give these ideas serious thought. Take care to only go for those that are totally up front, with no hidden sides. Your passion for delving into new professional fields is only surpassed by your romantic passions. Plan a seductive evening with a partner. Make it a night to remember!",
,        b"Feb 20, 2024 - Your home could be a gloomy place today, Cancer. A member of the household is likely to be away, and this person's presence is apt to be very sorely missed. The only thing you can do under the circumstances is make sure everyone keeps busy until the absent one returns. One way to distract the family is to get them to clean up the house in anticipation of a warm welcome!",
,        b"May 15, 2024 - Today is a good day to dream, Libra. Be aware that structured forces may try to convince you that the route you want to take isn't the most practical. You don't necessarily have to be practical in order to be successful or prosperous. Use your imagination. Let your creative spirit lead you to the next step. Find true strength in your ability to recognize and understand the needs of others.",
,        b"Feb 14, 2024 - If you've been thinking about starting a new business partnership, Taurus, this is the day to take some positive action toward that end. Any new business enterprises started today, particularly involving partnerships, show a lot of promise for success. This might make a profound difference in the course of your life, so make sure you allow for as many contingencies as you can. Go for the gold and enjoy the adventure!",
,        b"May 1, 2024 - Your feelings may deceive you, Aries, so be careful of getting too wrapped up in your own drama. Perhaps you feel you're getting pushed and pulled in uncomfortable directions. You might tolerate this tension for a while without expressing your feelings about it. This is a dangerous policy because it could give others the impression that it's OK to continue treating you the way they do.",
,        b"Jun 29, 2024 - As you follow the path toward acquiring the latest, greatest, fastest, and best, you might find that you're leaving behind some fundamental principles and values along the way, Scorpio. Don't lose sight of your foundation. Things could get shaken up today. Your ego might be on trial for pig-headed behavior. Keep yourself in check and be aware of the way you project yourself to others.",
,        b"Jun 20, 2024 - People would be wise to get out of your way today, Aries. You might find yourself feeling like a steam engine that's stoked to the brim with fiery hot coals. You're likely to be adamant about your course, and no one is going to be able to pull you off track. If people look carefully, they might even be able to see the steam coming out of your ears.",
,        b"Jul 17, 2024 - Social responsibilities might appear to be hitting you all at once. Everyone you know seems to crave your company. As a naturally polite and socially astute person, you might feel obligated to accept any and all invitations. This isn't a good day to commit yourself. Wait a while until you're feeling a little more balanced, then send out your responses. Commit only to those events you would genuinely enjoy."],
,       dtype=object)>,
, <tf.Tensor: shape=(32,), dtype=int64, numpy=
, array([0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1,
,        1, 1, 1, 0, 1, 1, 0, 0, 0, 0])>)

Now we want to embed and build our model

EMBEDDING LAYER

[ ]

MODEL

Now, we need to build out our neural network model. We want various layers here built out with the Sequential model since it's a way of stacking the layers one by one, and is the easiest model to understand and visualize. We are also going to be using Dropout layers since it's a good way to prevent overfitting, which can lead your model astray. We are going to be using a dropout of 0.4, 0.3 and 0.2, so 40%, 30% and 20% of our neural networks neurons will be randomly dropped out, or set to zero, so that our model can work better.

[ ]

Now we want to compile our model

[ ]

Let's now train our model on our training data

[ ]
Epoch 1/5
25/25 [==============================] - 41s 1s/step - loss: 0.6675 - accuracy: 0.6010 - val_loss: 0.6809 - val_accuracy: 0.6061
Epoch 2/5
25/25 [==============================] - 37s 1s/step - loss: 0.6375 - accuracy: 0.6768 - val_loss: 0.6621 - val_accuracy: 0.6515
Epoch 3/5
25/25 [==============================] - 36s 1s/step - loss: 0.6014 - accuracy: 0.7374 - val_loss: 0.6392 - val_accuracy: 0.6818
Epoch 4/5
25/25 [==============================] - 36s 1s/step - loss: 0.5329 - accuracy: 0.8422 - val_loss: 0.6149 - val_accuracy: 0.6919
Epoch 5/5
25/25 [==============================] - 36s 1s/step - loss: 0.4718 - accuracy: 0.8952 - val_loss: 0.5931 - val_accuracy: 0.6970

Once again we see that the loss may be plateauing in some places, but is overall decreasing. We can see that our val_accuracy is increasing so this means that our model is being trained nicely on our dataset.

Now, evaluate our model on our test dataset

[ ]
11/11 [==============================] - 2s 184ms/step - loss: 0.5995 - accuracy: 0.6970
Loss: 0.5995222926139832
Accuracy: 0.6969696879386902

NEW HOROSCOPE PREDICTION

https://www.tensorflow.org/api_docs/python/tf/squeeze

tf.squeeze is how you can get the probability from our prediction

[ ]
1/1 [==============================] - 0s 408ms/step
model probability: 0.615037739276886
1/1 [==============================] - 0s 268ms/step
model probability: 0.43571737408638
This should be positive: 1
This should be negative: 0

Let's see how our week will be going forward

[ ]
1/1 [==============================] - 0s 193ms/step
model probability: 0.6883848905563354
1/1 [==============================] - 0s 202ms/step
model probability: 0.5126816034317017
1/1 [==============================] - 0s 336ms/step
model probability: 0.5237581729888916
1/1 [==============================] - 0s 278ms/step
model probability: 0.3824429214000702
1/1 [==============================] - 0s 269ms/step
model probability: 0.6003984212875366
1/1 [==============================] - 0s 317ms/step
model probability: 0.6250004172325134
1/1 [==============================] - 0s 265ms/step
model probability: 0.6173655390739441
1/1 [==============================] - 0s 278ms/step
model probability: 0.6848816275596619
1/1 [==============================] - 0s 283ms/step
model probability: 0.6913534998893738
1/1 [==============================] - 0s 281ms/step
model probability: 0.6552363634109497
1/1 [==============================] - 0s 270ms/step
model probability: 0.5873440504074097
1/1 [==============================] - 0s 281ms/step
model probability: 0.395006000995636
Aries horoscope is 1
Taurus horoscope is 1
Gemini horoscope is 1
Cancer horoscope is 0
Leo horoscope is 1
Virgo horoscope is 1
Libra horoscope is 1
Scorpio horoscope is 1
Sagittarius horoscope is 1
Capricorn horoscope is 1
Aquarius horoscope is 1
Pisces horoscope is 0

lets save these back into MongoDB Atlas so we can visualize them in Charts

[ ]
Collecting pymongo
  Downloading pymongo-4.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB)
Collecting dnspython<3.0.0,>=1.16.0 (from pymongo)
  Downloading dnspython-2.6.1-py3-none-any.whl.metadata (5.8 kB)
Downloading pymongo-4.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 23.7 MB/s eta 0:00:00
Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.7/307.7 kB 18.1 MB/s eta 0:00:00
Installing collected packages: dnspython, pymongo
Successfully installed dnspython-2.6.1 pymongo-4.8.0
[ ]
Enter connection string WITH USER + PASS here··········
saved in! go check