Bitcoin Sentiment Analysis using Python and X (Formerly Twitter)
Tags: webdev, beginners, programming, python
Published: 2024-05-05 20:24:40

##Introduction

Bitcoin is like the hottest topic right now in finance and tech circles. It's got everyone chatting, from big-shot investors to regular folks. Keeping an eye on what people are saying about Bitcoin is super important because its value can go up and down like crazy. And guess where all the chatter happens these days? Yep, X (Twitter). By using fancy tools that can analyse tweets in real-time, we can really get a sense of how people are feeling about Bitcoin and how that might shake up the market.

Thankfully, [Python](https://www.python.org/), the go-to coding language for loads of developers, is here to save the day. It's got some awesome features for diving into text sentiment analysis. With cool libraries like [Tweepy](https://pypi.org/project/tweepy/), we can sift through X(Twitter) data and snag those interesting tweets about Bitcoin. And then there's [TextBlob](https://pypi.org/project/textblob/0.9.0/), a clever tool for understanding the sentiment in text. When it's time to clean up and organize all that data, libraries like [pandas](https://pypi.org/project/pandas/) and [numpy](https://pypi.org/project/numpy/) are there to help out. And let's not forget about [matplotlib](https://pypi.org/project/matplotlib/), the master of visualisations that can help us see the trends in sentiment crystal clear. Armed with these tools, developers can really dig deep into social media data and figure out what the general public thinks about Bitcoin.

Our project journey starts with using Python to collect tweets about Bitcoin from X(Twitter). We use a tool called Tweepy to help us gather these tweets. Once we have all this data, we then analyse the sentiment of the tweets using TextBlob. To present our findings, we rely on matplotlib to create visualisations that help us better understand how people feel about Bitcoin. By following this step-by-step approach, we can see how the sentiment around Bitcoin is changing over time. This process highlights how Python is a powerful tool for studying social media data and tracking the emotions related to cryptocurrencies, especially Bitcoin.

##Project Setup.

So, in this project, we'll be utilising a tool called [Colaboratory](https://colab.research.google.com/#scrollTo=Wf5KrEb6vrkR) or [Colab](https://colab.research.google.com/#scrollTo=Wf5KrEb6vrkR). It's essentially like a user-friendly version of Jupyter Notebook provided as a service by Google. With [Colab](https://colab.research.google.com/#scrollTo=Wf5KrEb6vrkR), you can easily write and run Python code directly in your browser. The cool thing is you don't need to set up anything, you get free access to GPUs, and sharing your work is super simple.


![Introduction page of Google Colaboratory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgkefbtbs00cepdr5phi.png)



###Step 1

Start by opening [google Colab](https://colab.research.google.com/#scrollTo=Wf5KrEb6vrkR)


![Colab Homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u53144uvhjjh2u7sgygm.png)



###Step 2

Open a new notebook 


![Opening a new notebook page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0mvb9t6cmq4cr438plm.png)



This is what the new notebook will look like


![Colab notebook homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7uwnueey4qhhgvd9nb2.png)




###Step 3

First things first, let's start by importing the necessary libraries:

<prev>
```python
import tweepy
from textblob import TextBlob
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
```
</prev>

Libraries used:

• [Tweepy](https://pypi.org/project/tweepy/): Tweepy is a Python library that allows us to interact with the Twitter API. We will use Tweepy to gather tweets about Bitcoin.
• [TextBlob](https://pypi.org/project/textblob/0.9.0/): TextBlob is a Python library that provides a simple API for performing natural language processing tasks. We will use TextBlob to analyze the sentiment of tweets.
• [Pandas](https://pypi.org/project/pandas/): Pandas is a Python library for data analysis and manipulation. We will use Pandas to store and analyze the results of our sentiment analysis.
• [Matplotlib](https://pypi.org/project/matplotlib/): Matplotlib is a Python library for creating visualizations. We will use Matplotlib to visualize the results of our sentiment analysis.

• [NumPy](https://pypi.org/project/numpy/): NumPy is a library that helps you work with numbers and arrays in Python. We will use NumPy to perform numerical calculations and operations on the data you collect from Twitter.

• [re](https://docs.python.org/3/library/re.html): The re library helps you find and replace patterns in text. We will use re to clean the data by removing unnecessary characters and foformatting

•  **When you use `plt.style.use('fivethirtyeight')` in your Python code with matplotlib, you're giving your plots a cool look. This style gives your plots a sleek vibe with muted colors and nice fonts. It's all about making your data visualizations neat and easy on the eyes.**

##Loading the Login File.

In this step, we're going to load the login file from our local computer using `files.upload()` function. Once you've selected the file, it will be uploaded to a temporary directory on the Colab server. You can access the uploaded file using the
uploaded variable:

###Step 4

To do this, we'll use the
`files.upload()` function from the `google.colab`
library:

<prev>
```python
from google.colab import files
files.upload()
```
</prev>



![Login file homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzm85ct8t2kl650llzfy.png)




##Storing the Data.

Once you have collected the data, you need to store it somewhere. There are a few different ways to do this.

###Step 5
In this step we start by creating a variable `log`:

<prev>
```python
log = pd.read_csv('login.csv')
```
</prev>


##Getting the Twitter API Credentials.

To talk to the Twitter API, we need to give it some secret information. This information is stored in a file called `login.csv`. The `login.csv` file contains your consumer key, consumer secret, access token, and access token secret. These are all long, random-looking strings of characters that you can get from Twitter when you create a developer account.

###Step 6

Let's get the Twitter API credentials from the `login.csv` file:

<prev>
```python
consumer_key = log["key"][0]
consumer_secret = log["key"][1]
access_token = log["key"][2]
access_token_secret = log["key"][3]
```
</prev>

##Creating the Authentication object.

The OAuthHandler object is a Python object that helps us talk to the Twitter API using our secret information from the previous step. 

###Step 7

To create an OAuthHandler object, we use the following code:

<prev>
```python
authenticate = tweepy.OAuthHandler(consumer_key, consumer_secret)
```
</prev>


##Setting the access token and access token secret.

The access token and access token secret are two more pieces of secret information from the `login.csv` file. We need to set them on the OAuthHandler object so that it can properly talk to the Twitter API. 

###Step 8

To do this, we use the following code:

<prev>
```python
authenticate.set_access_token(access_token, access_token_secret)
```
</prev>


##Creating the API object.

The API object is the object that we'll use to actually interact with the Twitter API.

###Step 9
To create an API object, we use the following code:

<prev>
```python
api = tweepy.API(authenticate, wait_on_rate_limit=True)
```
</prev>


##Gathering tweets about Bitcoin and filtering out retweets.

Now we can finally start talking to the Twitter API. We'll use the API object to search for tweets that contain the hashtag `#Bitcoin`. We'll also make sure to exclude retweets, and we'll only search for tweets that are in English and that were posted after a certain date. 

###Step 10

In this step we'll use the following code:

<prev>
```python
search_term = '#Bitcoin -filter:retweets'
tweets = tweepy.Cursor(api.search, q=search_term, lang='en', since='2018-11-01', tweet_mode='extended').items(2000)
```
</prev>


##Storing the tweets in a variable and getting the full text.

We'll loop through the tweets that we found in the previous step and extract the full text of each tweet.

###Step 11

Let's store all of the full tweets in a list. To do this, we use the following code:

<prev>
```python
all_tweets = [tweet.full_text for tweet in tweets]
```
</prev>


##Creating a Data Frame to store the tweets.

We'll create a data frame to store the full tweets in a structured format. This will make it easier to analyse the tweets later on.

###Step 12

To create a data frame, we use the following code:

<prev>
```python
df = pd.DataFrame(all_tweets, columns=['Tweets'])
```
</prev>


##Cleaning the tweets.

Before we can dive into analyzing the tweets, we need to do a little bit of cleanup. We're going to remove any unnecessary stuff like website links, hashtags, mentions, emojis, and punctuation. We'll also get rid of any duplicate tweets and any tweets that aren't in English.

**Why is this step so important?**

Well, cleaning the tweets helps us make sure that our sentiment analysis is accurate and reliable. If we don't clean the tweets, the analysis could be thrown off by all that extra noise.

Once the tweets have been cleaned, we can finally start analysing them for sentiment.

###Step 13

In this step, let's clean the tweets:

<prev>
```python
def clean_tweet(tweet):
	# Remove hashtags with 'bitcoin'
	tweet = re.sub('#bitcoin', 'bitcoin', tweet)
	# Remove hashtags with 'Bitcoin'
	tweet = re.sub('#Bitcoin', 'Bitcoin', tweet)
	# Remove hashtags with any letters or numbers
	tweet = re.sub('#[A-Za-z0-9]+', ' ', tweet)
	# Remove newlines
	tweet = re.sub('\\n', ' ', tweet)
	# Remove hyperlinks
	tweet = re.sub('https?:\/\/\S+', ' ', tweet)
	return tweet

df['Cleaned_tweets'] = df['Tweets'].apply(clean_tweet)
```
</prev>


###Step 14

Let's save and run the code we've written so far.


![Cleaning the tweets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y23h8kysrvd9i7kiny5q.png)




##Getting the subjectivity and polarity.

Subjectivity indicates the degree of personal opinion in the text. High subjectivity means more opinionated content, while low subjectivity suggests factual information.

Polarity determines if sentiment is positive, negative, or neutral towards Bitcoin. Positive polarity shows favorability, negative indicates negativity, and neutral signifies lack of strong sentiment.

By analyzing subjectivity and polarity in Bitcoin sentiment analysis, we can gain a better understanding of public sentiment towards Bitcoin. This information can be used to track trends, assess opinions, and make informed decisions about Bitcoin.

###Step 15

In this step, we want to get the subjectivity and polarity:

<prev>
```python
def get_subjectivity(tweet):
	return TextBlob(tweet).sentiment.subjectivity

def get_polarity(tweet):
	return TextBlob(tweet).sentiment.polarity

df['Subjectivity'] = df['Cleaned_tweets'].apply(get_subjectivity)
df['Polarity'] = df['Cleaned_Tweets'].apply(get_polarity)
```
</prev>

###Step 16

Let's save and run our code.


![Getting the subjectivity and polarity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twfogig9bwl8a888esx5.png)




##Getting the Sentiment text.

Imagine you're at a party and you want to get a sense of the overall mood of the guests. You could ask each guest how they're feeling, and then you could categorise their responses as positive, negative, or neutral. This would give you a good idea of the overall sentiment of the party.

The same principle applies to sentiment text data. By analysing the sentiment of a large amount of text data, researchers can get a good understanding of the overall sentiment towards Bitcoin.

Sentiment text data is a powerful tool that can be used to gain insights into public opinion and market trends. It's a valuable resource for researchers who want to understand how people feel about Bitcoin and other topics:

###Step 17

To get the sentiment text for this project, we use the following code:

<prev>
```python
def get_sentiment(score):
	if score < 0:
    	return 'Negative'
	elif score == 0:
    	return 'Neutral'
	else:
    	return 'Positive'

df['Sentiment'] = df['Polarity'].apply(get_sentiment)
```
</prev>

Step 18

Let's save and run the code we've written so far



![Getting the sentiment text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/beig2was2jpqes6xk3tn.png)



##Creating a scatter plot to show the subjectivity and polarity.

Imagine you have a bunch of text data, like reviews of a movie or tweets about a certain topic. You're interested in understanding the overall sentiment and subjectivity of this data, so you decide to create a scatter plot to visualise the relationship between two aspects of the data:

• Subjectivity: How subjective or biassed the text is, ranging from 0 (completely objective) to 1 (completely subjective).
• Polarity: The emotional sentiment of the text, ranging from -1 (negative) to 1 (positive).

By plotting these two metrics on a scatter plot, you can see how they relate to each other. For example, you might find that highly subjective texts tend to have more negative polarity, or that texts with neutral polarity tend to be more objective.

###Step 19

Here, we'll a scatter plot to show the subjectivity and polarity and to do this we will use the following code:

<prev>
```python
plt.figure(figsize=(8, 6))
for i in range(0, df.shape[0]):
	plt.scatter(df['Polarity'][i], df['Subjectivity'][i], color='purple')
plt.title('Sentiment Analysis Scatter Plot')
plt.xlabel('Polarity')
plt.ylabel('Subjectivity (objective -> subjective)')
plt.show()
```
</prev>

###Step 20

Let's save and run the code we've written so far.



![Creating a scatter plot to show the subjectivity and polarity.
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6x6qfmuw8z720tkgwm4h.png)



##Creating a bar chart to show the count of positive, neutral, and negative sentiments.

Finally, we're going to create a bar chart that shows us how many positive, neutral, and negative sentiments we have.

Each bar in the chart will be like a little tower, representing the count of each sentiment type. This way, we can easily see how many of each type we have and compare them to each other.

This visual representation will make it super easy to understand the different types of sentiments in our data and how they compare to each other. It's like having a snapshot of our sentiment analysis results, all in one place.

###Step 21

In this final step we will create a bar chart to show the count of positive, neutral, and negative sentiments:

<prev>
```python
df['Sentiment'].value_counts().plot(kind='bar')
plt.title('Sentiment Analysis Bar Plot')
plt.xlabel('Sentiments')
plt.ylabel('Number of Tweets')
plt.show()
```
</prev>

###Step 22

Let's save and run our written code so far.


![Creating a bar chart to show the count of positive, neutral, and negative sentiments](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmz4c532ze203b9bpnv2.png)



##Conclusion

Our Bitcoin sentiment analysis project was a lot of fun, and we learned a lot about how people feel about Bitcoin. By analysing tweets in real-time, we were able to see how people's feelings about Bitcoin changed over time. This information could be really useful for investors, traders, or anyone else who is interested in the cryptocurrency market.

The tools and techniques we used in this project can be used to analyse other social media data. So, if you're interested in understanding how people feel about other topics, like politics or sports, you can use the same techniques that we used in this project.

As the cryptocurrency market continues to grow and evolve, it will be more important than ever to understand how people feel about different cryptocurrencies. By using sentiment analysis, we can gain a better understanding of the market and make more informed decisions about our investments.