| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | ||
---|---|---|
SkillCate AI: Twitter & Tweepy | ||
| Part 1 | Part 2 | Part 3 | |
from helpers_new import *
import_all()
import matplotlib.pyplot as plt
import tweepy as tw
plt.style.use('pinks.mplstyle')
%matplotlib inline
%%html
<style>
a:link {color: #f6b48f !important; font-weight: 600 !important;}
a:visited {color: #f6b48f !important; font-weight: 600 !important;}
</style>
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | ||
---|---|---|
Quick Query Overview |
Querying twitter |
---|
client = tw.Client('')
query = '#fed -is:retweet lang: en'
tws = client.search_recent_tweets(query=query,
tweet_fields = ['context_annotations', 'created_at'],
max_results = 10)
Looking at results |
---|
for tweet in tws[0]:
pretty(tweet.text, f'Tweet id: {tweet.id}')
Tweet id: 1621179092468142080 |
👨⚕️👷♀️NFP PREVIEW VIDEO 👷♀️👨⚕️ See the key figures you need to watch ahead of the #NFP report, including what it might mean for the #Fed. Our full preview report has potential trade setups in the #FX market: https://t.co/jN2VpXn0zB This video is not intended for the US audience. https://t.co/mrFXUjvlyD |
Tweet id: 1621178305490866178 |
📊 The #Fed increased interest rates by 25 bp & indicated a need to tighten monetary policy further in its effort to combat #inflation. The #FOMC raised its rate to a range of 4.5% to 4.75%, the highest level since Oct 07. #LeverageShares Capital at risk. https://t.co/I0SYnhswwe |
Tweet id: 1621178136758042629 |
The #FederalReserve and the #EuropeanCentralBank have raised their interest rates. 📊 The #FED raised rates a quarter point, from 4.5% to 4.75%, and the #ECB hiked interest rates by 0.5%. https://t.co/g7pPyZVlgD |
Tweet id: 1621178059675009024 |
"US | Fed slows to 25bp hike and recognizes that “the…" - BBVA #fed https://t.co/MfMlm6HWjw |
Tweet id: 1621177827436662784 |
May be bears have 1 last try left. Let's see. Look like bears can't give up that easy in FED QT environment. It is not QE environment but market is crazy over bullish. #Fed #market #StockMarket |
Tweet id: 1621177618530811904 |
Jobless Claims (#LEADINGindicator) again white hot, getting stronger, and NOT showing a weakening labor market. Someone tell the #Fed maybe? |
Tweet id: 1621177579595235329 |
The only chance for the #fed to fix financial conditions is to start world war 3 |
Tweet id: 1621177029927342080 |
ECB policymakers see at least two more rate hikes -sources #Ainvest #Ainvest_Wire #Fed #FederalReserve #inflation View more: https://t.co/RoG3eime4L https://t.co/VqOYx6u7S6 |
Tweet id: 1621177014312132613 |
#Fed #Rate #Hike Crypto Market Pumping & News https://t.co/bFKBYoyOAd |
Tweet id: 1621177010042335234 |
#Fed #Rate #Hike Crypto Market Pumping & News https://t.co/AsHMqiK9M0 |
get_tweets(hashtag, num_tweets) |
---|
dow_30_df = get_tweets('#dow30', 10)
10 twitter results for #dow30 |
tweet_id | tweet_text |
---|---|
1621163221871910912 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/4jpETboyt9 |
1621055322902372352 | At December’s meeting, bolstering optimism that #inflation is cooling, but still giving no indication of an upcoming pause in #RateHikes. The #SP500 increased 42.61pts (+1.05%) to 4119, the #Nasdaq100 jumped 261.17pts(+2.16%) to 12363 & the #Dow30 rose 6.92pts (+0.02%) to 34092. https://t.co/PwSsAItmnW |
1620859477389107200 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/VeEUDbNPiK |
1620782852874051584 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/AktFlN4eyt |
1620710422495236096 | February 01/2023, #StockMarketNews: On Tuesday, #USIndices closed higher as the #SP500 rose 58.83pts (+1.46%) to 4076, the #Nasdaq100 increased 189.54pts (+1.59%) to 12101 and the #Dow30 gained 368.95pts (+1.09%) to 34086.#StocksToTrade #stocksinnews #TradingOpportunity #trade https://t.co/LQn7nq06aG |
1620491934023442432 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/6mI2v5e1mx |
1620453054708596736 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/copNjMdcFs |
1620135138456928256 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/D0UCvYel3R |
1620088967730462720 | Drugmaker Amgen $AMGN said on #Monday that it let go ~300 U.S. employees or about 1.2% of its total workforce, citing recent organizational changes to its commercial team. $DIA $SPY $QQQ #Dow30 #stocks #SwingTrading #Mondayvibes $IBB $XBI $ATOS $INM https://t.co/5zFYSARkES |
1620061074296045568 | #DOW30 (Indices Future) : Live example on T.F. 15m, demark 9, supertrend, LRegr.Forec., Interc., Slope, RSquar., Supp and Resist. Lorenz attractor area, Pivot and Target price, and new Open Interest Long (AreaGreen) Short (AreaRed). https://t.co/3U75IjUh85 |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | ||
---|---|---|
Twitter API v1.1 Authentication |
API_KEY = ''
API_SECRET = ''
BEARER = ''
TOKEN = ''
TOKEN_SECRET = ''
auth = tw.OAuth1UserHandler(API_KEY, API_SECRET, TOKEN, TOKEN_SECRET)
api = tw.API(auth)
Posting a tweet (Must have upgraded access. Still waiting...) |
---|
# tweet = api.update_status('Whaddup, World! Ignore everything I have ever said on Twitter before...unless you LIKE what I have said. This is the new DATA SCIENCE me. (Tweeted via Python and Tweepy. Tweep Tweep!)')
Pulling data from personal feed (Must have upgraded access. Still waiting...) |
---|
# tws = api.home_timeline(count = 5)
# for tweet in tws[0]:
# pretty(tweet.text, f'Tweet id: {tweet.id}')
Getting your own tweets (Must have upgraded access. Still waiting...) |
---|
# tws = api.user_timeline(count = 2)
# for tweet in tws[0]:
# pretty(tweet.text, f'Tweet id: {tweet.id}')
Getting attributes of tweets - created_at - id - text |
---|
query = '#NYSE -is:retweet lang: en'
tws = client.search_recent_tweets(query=query,
tweet_fields = ['context_annotations',
'created_at'],
max_results = 10)
for tweet in tws[0]:
print(tweet.created_at)
2023-02-02 16:10:11+00:00 2023-02-02 16:10:07+00:00 2023-02-02 16:10:03+00:00 2023-02-02 16:09:53+00:00 2023-02-02 16:09:44+00:00 2023-02-02 16:09:35+00:00 2023-02-02 16:09:30+00:00 2023-02-02 16:09:27+00:00 2023-02-02 16:09:18+00:00 2023-02-02 16:09:02+00:00
for tweet in tws[0]:
print(tweet.id)
1621179182184030208 1621179162739474433 1621179147446886401 1621179106179022854 1621179069936066566 1621179028437696512 1621179008225349633 1621178994866397191 1621178958292127744 1621178890344448003
for tweet in tws[0]:
print(tweet.text)
$AMZN new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 883 Spiking 🚀 Today's Acquisition ➡02 February 2023 : First American acquires 1031 Solutions #First #American #Solutions #NYSE #FAF To learn more, visit :https://t.co/NdENbVpvlo $NEWT new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 881 $AAPL new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 879 $AAPL new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 877 $MCO new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 875 $BLNK Stock Alert: @BlinkCharging plan to battle naked short sellers! #ShortSqueeze https://t.co/Wb7nz6Yg72 #stockmarket #stocks #trading #investing #markets #EVcharging #EV #electricvehicles #EVchargers #ESG #SwingTrading #fintwit #nyse #dowjones #sp500 #NASDAQ #wallstreetbets https://t.co/CxKUcfuLAr $XOM new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 873 $BDN new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 871 $GOOS new alert at https://t.co/A7qrDarJHY #stocks #daytrading #NYSE #NASDAQ #market 867
Deleting tweets (Must have upgraded access. Still waiting...) |
---|
# tweeting the tweet that will be removed
# bad_tweet = api.update_status('bad_tweet')
# removing the tweet
# api.destroy_status(bad_tweet.id)
Following an account (Must have upgraded access. Still waiting...) |
---|
# api.create_friendship(screen_name = '#YouTube')
Unfollowing an account (Must have upgraded access. Still waiting...) |
---|
# api.destroy_friendship(screen_name = '#YouTube')
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Twitter API v2.0 Authentication |
---|
client = tw.Client('AAAAAAAAAAAAAAAAAAAAAH4tlgEAAAAAJz3BHC1CYpWAtTrmvqMEc6ZHlqQ%3D1vpTrgfbZHqdQJlljVcso99m9rb4abITpUYbrAoyWiaISNGOLr')
- is:retweet <- no retweets - lang: en <- get tweets in English - max_results = 10 <- get a maximum of ten results |
---|
query = '#stockmarket -is:retweet lang: en'
tws = client.search_recent_tweets(query=query,
tweet_fields = ['context_annotations',
'created_at'],
max_results = 10)
for tweet in tws[0]:
pretty(tweet.text, f'Tweet id: {tweet.id}')
Tweet id: 1621179186730921986 |
$MDLZ company #profile, #Fundamental and #Technical Analysis Score Update (FA Score, TA Score) #stocks #stock #StockMarket #Investment #investing #invest #investor #investors https://t.co/I8hjTlnne3 https://t.co/NGeeMjXDAH https://t.co/qS5FvMWozZ |
Tweet id: 1621179166673764352 |
Today Top Flow in #NASDAQ #NASDAQ100, Buy Flow & Sell Flow $AAPL $AMD $MSFT $GILD $INTC $TSLA $COST $CSCO $FISV $FAST $AMZN $NVDA $MRNA $BIDU $PDD $BKNG $JD $GOOG $NFLX $QCOM #stock #stocks #StockMarket #Investment #investing https://t.co/Gr3YvQz1MY https://t.co/roiDq7E7zz |
Tweet id: 1621179145886617600 |
HUBEOS MARKET ALERT @ 2023-02-02 10:40: +3.72% GAIN in DISH Stock (DISH Network) – Price $15.485. More info: https://t.co/m0j1Tlny29. RT #stockmarket #marketalert https://t.co/O73rVoCV22 |
Tweet id: 1621179126462943233 |
$BASFY - BASF: Slightly Undervalued With A Reliable Dividend. https://t.co/r65StN0B02 #stockmarket #stocks #investing |
Tweet id: 1621179123963162624 |
$CNHI - CNH Industrial N.V. 2022 Q4 - Results - Earnings Call Presentation. https://t.co/Jnmg5Eb1l7 #economy #stocks #stockmarket |
Tweet id: 1621179095559340035 |
OMG!!! Tesla is mooning right now. WOW!!! 🚀💸🤑 $TSLA #StockMarket #invest |
Tweet id: 1621179091570548736 |
Coforge is consolidating but it's better to observe and grab the opportunity #investing #trade #StockMarket #intradaytrading https://t.co/CQzneuKhu2 |
Tweet id: 1621179008225349633 |
$BLNK Stock Alert: @BlinkCharging plan to battle naked short sellers! #ShortSqueeze https://t.co/Wb7nz6Yg72 #stockmarket #stocks #trading #investing #markets #EVcharging #EV #electricvehicles #EVchargers #ESG #SwingTrading #fintwit #nyse #dowjones #sp500 #NASDAQ #wallstreetbets https://t.co/CxKUcfuLAr |
Tweet id: 1621178999581073408 |
Big Bull Securities Pvt. Ltd - Stock Market Analysis (02-02-2023) | Mahesh Kaku #StockMarketAnalysis #StockMarket #UnionBudget #budgets2023 #indianbudgetupdate #bigbull #bigbullsecurities https://t.co/VGU5N99ubZ https://t.co/zIPKbvJYPC |
Tweet id: 1621178960490160129 |
This is why Hedge Funds use A.I. $ENVB entered Uptrend as Momentum indicator ascends above 0 level. #EnvericBiosciences #stockmarket #stock https://t.co/NpnPlPOiDF https://t.co/GtNXYgpeo0 |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Sentiment Analysis |
---|
* TextBlob is a sentiment analysis Python library |
---|
NLTK further indicates the degree to which text is postive, neutral, or negative |
---|
import re
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from textblob import TextBlob
import nltk
nltk.download('vader_lexicon')
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
[nltk_data] Downloading package vader_lexicon to [nltk_data] /Users/evancarr/nltk_data... [nltk_data] Package vader_lexicon is already up-to-date!
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Getting 1,000 Tweets with #apple |
---|
query = '#apple -is:retweet lang:en'
paginator = tw.Paginator(client.search_recent_tweets,
query = query,
max_results = 100,
limit = 10)
tweets = []
for tweet in paginator.flatten():
tweets.append(tweet)
tweets_df = pd.DataFrame(tweets)
head_tail_vert(tweets_df, 3, '100 Imported Tweets')
100 Imported Tweets: head(3) |
edit_history_tweet_ids | id | text | |
---|---|---|---|
0 | [1621178996493885440] | 1621178996493885440 | $AAPL #Apple’s Earnings Are Today. #IPhoneSales Will Be Key for the Stock. https://t.co/aWyAWAkZpi via @BarronsOnline |
1 | [1621178787592536064] | 1621178787592536064 | #Apple should be required to let #iPhone users sideload apps, #Biden admin says. That's probably one of the few things, the @GOP somewhat agrees on.😉 https://t.co/CdBS3fa1fR |
2 | [1621178669317201922] | 1621178669317201922 | Investors Cheer Meta’s Slimmed-Down Ambitions\n\n#Activision #Adani #Adani #Alphabet #Amazon.com #Apple #Bank #Berkshire #Blackstone #European #Strive #Meta https://t.co/Eitl5wiQYf |
100 Imported Tweets: tail(3) |
edit_history_tweet_ids | id | text | |
---|---|---|---|
996 | [1620955902705639424] | 1620955902705639424 | Apple's ane-transformers - experiences? #nlp #apple https://t.co/OpJavNehzt |
997 | [1620955803825094657] | 1620955803825094657 | Is there any Hacking attempts on your device? DM now for assistance and help. #tech #technology #iphone #technews #gadgets #apple #innovation #android #business #smartphone #samsung #pro #techno #instatech #programming #engineering #electronics #coding #gadget #computer #gaming |
998 | [1620955719456391168] | 1620955719456391168 | " Everybody loves $rndr " \n\n#ai #metaverse #gpu #top10crypto #apple https://t.co/rohD5jBLwz |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Preprocessing the Tweets |
---|
def preprocess_tweet(sentence):
sentence = sentence.lower()
# remove RT
sentence = re.sub('RT @\w+: ', " ", sentence)
# remove punctuation and numbers
sentence = re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\s+)", ' ', sentence)
# single character removal
sentence = re.sub(r'\s+[a-zA-Z]\s+', ' ', sentence)
#remove multiple spaces
sentence = re.sub(r'\s+', ' ', sentence)
sentence = sentence.replace('https', '')
return sentence
cleaned_tweets = []
for tweet in tweets_df['text']:
cleaned_tweet = preprocess_tweet(tweet)
cleaned_tweets.append(cleaned_tweet)
tweets_df['cleaned'] = pd.DataFrame(cleaned_tweets)
tweets_df.drop(columns = ['edit_history_tweet_ids'], inplace = True)
head_tail_vert(tweets_df, 2, 'New Cleaned Column')
New Cleaned Column: head(2) |
id | text | cleaned | |
---|---|---|---|
0 | 1621178996493885440 | $AAPL #Apple’s Earnings Are Today. #IPhoneSales Will Be Key for the Stock. https://t.co/aWyAWAkZpi via @BarronsOnline | aapl apple earnings are today iphonesales will be key for the stock co awyawakzpi via |
1 | 1621178787592536064 | #Apple should be required to let #iPhone users sideload apps, #Biden admin says. That's probably one of the few things, the @GOP somewhat agrees on.😉 https://t.co/CdBS3fa1fR | apple should be required to let iphone users sideload apps biden admin says that probably one of the few things the somewhat agrees on co cdbs3fa1fr |
New Cleaned Column: tail(2) |
id | text | cleaned | |
---|---|---|---|
997 | 1620955803825094657 | Is there any Hacking attempts on your device? DM now for assistance and help. #tech #technology #iphone #technews #gadgets #apple #innovation #android #business #smartphone #samsung #pro #techno #instatech #programming #engineering #electronics #coding #gadget #computer #gaming | is there any hacking attempts on your device dm now for assistance and help tech technology iphone technews gadgets apple innovation android business smartphone samsung pro techno instatech programming engineering electronics coding gadget computer gaming |
998 | 1620955719456391168 | " Everybody loves $rndr " \n\n#ai #metaverse #gpu #top10crypto #apple https://t.co/rohD5jBLwz | everybody loves rndr ai metaverse gpu top10crypto apple co rohd5jblwz |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Generating Sentiment Labels |
---|
Breaking down the steps for using TextBlob and NLTK |
---|
cleaned = list(tweets_df.cleaned.items())
cleaned[0][1]
' aapl apple earnings are today iphonesales will be key for the stock co awyawakzpi via '
pretty(TextBlob(cleaned[0][1]).sentiment, 'TextBlob(cleaned[0][1]).sentiment')
polarity, subjectivity = TextBlob(cleaned[0][1]).sentiment
pretty(polarity, 'polarity')
pretty(subjectivity, 'subjectivity')
TextBlob(cleaned[0][1]).sentiment |
Sentiment(polarity=0.0, subjectivity=1.0) |
polarity |
0.000000 |
subjectivity |
1.000000 |
pretty(SentimentIntensityAnalyzer().polarity_scores(cleaned[0][1]),
'SentimentIntensityAnalyzer().polarity_scores(cleaned[0][1])')
SentimentIntensityAnalyzer().polarity_scores(cleaned[0][1]) |
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0} |
Adding sentiment columns |
---|
def add_sentiment_columns(df, text_column):
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from textblob import TextBlob
df[['polarity',
'subjectivity']] = df['cleaned']\
.apply(lambda Text: pd.Series(TextBlob(Text).sentiment))
for idx, row in df[text_column].items():
score = SentimentIntensityAnalyzer().polarity_scores(row)
neg = score['neg']; neu = score['neu']
pos = score['pos']; comp = score['compound']
if comp <= -0.05:
df.loc[idx, 'sentiment'] = 'negative'
elif comp >= 0.05:
df.loc[idx, 'sentiment'] = 'positive'
else:
df.loc[idx, 'sentiment'] = 'neutral'
df.loc[idx, 'neg'] = neg
df.loc[idx, 'neu'] = neu
df.loc[idx, 'pos'] = pos
df.loc[idx, 'compound'] = comp
return df
tweets_df = add_sentiment_columns(tweets_df, 'cleaned')
head_tail_vert(tweets_df[['cleaned', 'polarity', 'subjectivity',
'sentiment', 'neg', 'neu', 'pos', 'compound']],
2,
'DF with Sentiment Columns')
DF with Sentiment Columns: head(2) |
cleaned | polarity | subjectivity | sentiment | neg | neu | pos | compound | |
---|---|---|---|---|---|---|---|---|
0 | aapl apple earnings are today iphonesales will be key for the stock co awyawakzpi via | 0.00 | 1.00 | neutral | 0.00 | 1.00 | 0.00 | 0.00 |
1 | apple should be required to let iphone users sideload apps biden admin says that probably one of the few things the somewhat agrees on co cdbs3fa1fr | -0.20 | 0.10 | positive | 0.00 | 0.94 | 0.06 | 0.13 |
DF with Sentiment Columns: tail(2) |
cleaned | polarity | subjectivity | sentiment | neg | neu | pos | compound | |
---|---|---|---|---|---|---|---|---|
997 | is there any hacking attempts on your device dm now for assistance and help tech technology iphone technews gadgets apple innovation android business smartphone samsung pro techno instatech programming engineering electronics coding gadget computer gaming | 0.00 | 0.00 | positive | 0.00 | 0.86 | 0.14 | 0.65 |
998 | everybody loves rndr ai metaverse gpu top10crypto apple co rohd5jblwz | 0.00 | 0.00 | positive | 0.00 | 0.71 | 0.29 | 0.57 |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Sentiments DataFrame |
---|
negative_tweets = tweets_df[tweets_df.sentiment == 'negative']
positive_tweets = tweets_df[tweets_df.sentiment == 'positive']
neutral_tweets = tweets_df[tweets_df.sentiment == 'neutral']
sentiments_df = pd.DataFrame(tweets_df.sentiment.value_counts())
sentiments_df['percentage'] = sentiments_df.sentiment / len(tweets_df) * 100
see(sentiments_df, 'Sentiments DataFrame with Percentages')
Sentiments DataFrame with Percentages |
sentiment | percentage | |
---|---|---|
positive | 555 | 55.56 |
neutral | 317 | 31.73 |
negative | 127 | 12.71 |
Sentiments Donut |
---|
labels = sentiments_df.index
percentages = sentiments_df['percentage']
donut = plt.Circle((0,0), 0.7, color = '#35193e')
plt.pie(percentages,
labels = labels,
colors = ['C0', 'C1', 'C2'])
plt.title('Sentiment Distribution')
plotster = plt.gcf()
plotster.gca().add_artist(donut)
<matplotlib.patches.Circle at 0x7faf69cbeef0>
def word_cloud_it(text, title):
mask = np.array(Image.open('./twitter_materials_skillcate/a1_inputs/cloud.png'))
stopwords = set(STOPWORDS)
word_cloud = WordCloud(background_color = 'white',
mask = mask,
max_words = 100,
stopwords = stopwords,
repeat = True)
word_cloud.generate(str(text))
word_cloud.to_file('my_word_cloud.png')
pretty('Your WordCloud has been successfully saved.')
header_text(title)
display(Image.open('my_word_cloud.png'))
word_cloud_it(tweets_df.cleaned.values, 'WordCloud: All Tweets')
Your WordCloud has been successfully saved. |
WordCloud: All Tweets |
word_cloud_it(positive_tweets.cleaned.values, 'WordCloud: Postive Tweets')
Your WordCloud has been successfully saved. |
WordCloud: Postive Tweets |
word_cloud_it(negative_tweets.cleaned.values, 'WordCloud: Negative Tweets')
Your WordCloud has been successfully saved. |
WordCloud: Negative Tweets |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Character and Word Length |
---|
tweets_df['characters'] = tweets_df['cleaned'].astype(str).apply(len)
tweets_df['num_words'] = tweets_df['cleaned'].apply(lambda x: len(str(x).split()))
head_tail_vert(tweets_df[['cleaned', 'characters', 'num_words']], 3, 'Added Length Columns')
Added Length Columns: head(3) |
cleaned | characters | num_words | |
---|---|---|---|
0 | aapl apple earnings are today iphonesales will be key for the stock co awyawakzpi via | 88 | 15 |
1 | apple should be required to let iphone users sideload apps biden admin says that probably one of the few things the somewhat agrees on co cdbs3fa1fr | 150 | 26 |
2 | investors cheer meta slimmed down ambitions activision adani adani alphabet amazon com apple bank berkshire blackstone european strive meta co eitl5wiqyf | 154 | 21 |
Added Length Columns: tail(3) |
cleaned | characters | num_words | |
---|---|---|---|
996 | apple ane transformers experiences nlp apple co opjavnehzt | 59 | 8 |
997 | is there any hacking attempts on your device dm now for assistance and help tech technology iphone technews gadgets apple innovation android business smartphone samsung pro techno instatech programming engineering electronics coding gadget computer gaming | 255 | 35 |
998 | everybody loves rndr ai metaverse gpu top10crypto apple co rohd5jblwz | 71 | 10 |
see(pd.DataFrame(tweets_df.groupby('sentiment').characters.mean()),
'Average Number of Characters by Sentiment')
Average Number of Characters by Sentiment |
characters | |
---|---|
sentiment | |
negative | 172.59 |
neutral | 140.63 |
positive | 178.48 |
see(pd.DataFrame(tweets_df.groupby('sentiment').num_words.mean()),
'Average Number of Words by Sentiment')
Average Number of Words by Sentiment |
num_words | |
---|---|
sentiment | |
negative | 27.31 |
neutral | 20.98 |
positive | 27.86 |
| Top | Query Overview | API v1.1 Actions | API v2.0 Actions | Sentiment Analysis | Getting Apple Tweets | Data Preprocessing | Generating Sentiment Labels | Sentiments DataFrame | WordClouds | Tweet Length | Saving the DataFrame | | Saving Tweets DataFrame to CSV |
---|
tweets_df.to_csv('tweets_df.csv')