/News_Mood

A visualization of the last 100 tweets of popular news outlets.

Primary LanguageJupyter Notebook

News Sentiments

Analysis

  • BBCWorld, CBSNews and NYTimes has the most negative Vader sentiment analysis.
  • CNN and FoxNews has an overall positive Vader scores.CNN has the highest score with 0.08,the FoxNews has 0.01.
  • The New York Times has a negative score of -0.1. Even though its score is negative, the New York Times had the scores closes to zero. It has the news mood with the closest neutral sentiment.
  • The Vader analysis on news channels is highly dependent on they type of news happens on a particular day. A more fair analysis would analyze a larger sample of tweets over a larger period of time.
# Dependencies
import tweepy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Import and Initialize Sentiment Analyzer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()

# Twitter API Keys
from config import (consumer_key, 
                    consumer_secret, 
                    access_token, 
                    access_token_secret)

# Setup Tweepy API Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
# Target Search Term
target_terms = ("CNN","BBCWorld","CBSNews","FoxNews","NYTimes")

# List to hold results
results_list = []

# Loop through all target users
for target in target_terms:
    
    count = 0

    # Run search around each tweet
    public_tweets = api.user_timeline(target, count=100)

        # Loop through all tweets
    for tweet in public_tweets:
            
        # Run Vader Analysis on each tweet
        results = analyzer.polarity_scores(tweet["text"])
        compound = results["compound"]
        pos = results["pos"]
        neu = results["neu"]
        neg = results["neg"]
        tweets_ago = count
        tweet_text = tweet["text"]
            
        # Append news results to 'results_list'
        results_list.append({"User":target,
                        "Date": tweet['created_at'],
                        "Compound" : compound,
                        "Positive" : pos,
                        "Negative" : neg,
                        "Neutral" : neu,
                        "Tweets Ago" : count,
                        "Tweet Text" : tweet_text})
        count= count+ 1
        
news_df = pd.DataFrame.from_dict(results_list)#.set_index("User").round(3)
news_df
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Compound Date Negative Neutral Positive Tweet Text Tweets Ago User
0 -0.0813 Fri Jun 08 14:30:16 +0000 2018 0.110 0.794 0.096 President Trump says he doesn't disagree with ... 0 CNN
1 0.0000 Fri Jun 08 14:20:11 +0000 2018 0.000 1.000 0.000 Former NBA star Dennis Rodman is traveling to ... 1 CNN
2 -0.3597 Fri Jun 08 14:10:04 +0000 2018 0.146 0.773 0.082 Venezuela ranks as the most dangerous country ... 2 CNN
3 0.0000 Fri Jun 08 14:00:06 +0000 2018 0.000 1.000 0.000 Anthony Bourdain, the chef and gifted storytel... 3 CNN
4 0.0516 Fri Jun 08 13:50:09 +0000 2018 0.000 0.938 0.062 The "gig economy" of freelancers and short-ter... 4 CNN
5 0.5994 Fri Jun 08 13:40:07 +0000 2018 0.000 0.803 0.197 "It concerns me that so many voices within the... 5 CNN
6 -0.7351 Fri Jun 08 13:30:06 +0000 2018 0.267 0.733 0.000 Federal investigators say in a preliminary rep... 6 CNN
7 0.0000 Fri Jun 08 13:20:05 +0000 2018 0.000 1.000 0.000 President Trump says he wants to meet with NFL... 7 CNN
8 -0.5267 Fri Jun 08 13:12:07 +0000 2018 0.145 0.855 0.000 A 25-year-old South Korean hiker is recovering... 8 CNN
9 -0.8225 Fri Jun 08 13:04:02 +0000 2018 0.336 0.664 0.000 A longtime US Senate staffer was arrested on c... 9 CNN
10 0.0516 Fri Jun 08 12:59:08 +0000 2018 0.000 0.938 0.062 Stormy Daniels' attorney Michael Avenatti says... 10 CNN
11 0.0000 Fri Jun 08 12:57:07 +0000 2018 0.000 1.000 0.000 Time cover gets Trump just right | By Michael ... 11 CNN
12 -0.4767 Fri Jun 08 12:55:43 +0000 2018 0.134 0.866 0.000 President Trump says Russia should be reinstat... 12 CNN
13 0.5719 Fri Jun 08 12:54:50 +0000 2018 0.000 0.821 0.179 A new television ad showing a gubernatorial ca... 13 CNN
14 0.0000 Fri Jun 08 12:52:04 +0000 2018 0.000 1.000 0.000 FX renews "Atlanta" for Season 3 https://t.co/... 14 CNN
15 0.4019 Fri Jun 08 12:50:09 +0000 2018 0.000 0.870 0.130 JUST IN: President Trump says he is considerin... 15 CNN
16 -0.2960 Fri Jun 08 12:45:00 +0000 2018 0.217 0.664 0.119 Krill are critical to the fight against climat... 16 CNN
17 -0.7351 Fri Jun 08 12:43:06 +0000 2018 0.279 0.721 0.000 In a preliminary report about a fatal Tesla cr... 17 CNN
18 -0.5994 Fri Jun 08 12:38:42 +0000 2018 0.242 0.659 0.099 If you or someone you know might be at risk of... 18 CNN
19 0.1779 Fri Jun 08 12:37:04 +0000 2018 0.124 0.723 0.153 The Washington Capitals defeated the Vegas Gol... 19 CNN
20 0.0000 Fri Jun 08 12:31:06 +0000 2018 0.000 1.000 0.000 For almost two-and-a-half hours, Democratic me... 20 CNN
21 0.0000 Fri Jun 08 12:26:05 +0000 2018 0.000 1.000 0.000 In the Maldives, a resort offers endangered se... 21 CNN
22 0.0000 Fri Jun 08 12:21:08 +0000 2018 0.000 1.000 0.000 The office of Arizona Republican Rep. Martha M... 22 CNN
23 0.0000 Fri Jun 08 12:20:10 +0000 2018 0.000 1.000 0.000 KFC says it will break with its meat-loving tr... 23 CNN
24 0.7650 Fri Jun 08 12:16:05 +0000 2018 0.145 0.526 0.329 The race to save the Great Barrier Reef? A cor... 24 CNN
25 -0.4767 Fri Jun 08 12:15:05 +0000 2018 0.162 0.838 0.000 President Trump says he looks forward to 'stra... 25 CNN
26 0.0000 Fri Jun 08 12:12:03 +0000 2018 0.000 1.000 0.000 The Trump administration has announced a new, ... 26 CNN
27 0.0000 Fri Jun 08 12:06:04 +0000 2018 0.000 1.000 0.000 Republican patience is waning when it comes to... 27 CNN
28 -0.5859 Fri Jun 08 11:58:07 +0000 2018 0.211 0.789 0.000 Police are investigating the London Fire Briga... 28 CNN
29 0.0000 Fri Jun 08 11:52:03 +0000 2018 0.000 1.000 0.000 A group of Republicans went to the White House... 29 CNN
... ... ... ... ... ... ... ... ...
470 0.0000 Thu Jun 07 22:47:03 +0000 2018 0.000 1.000 0.000 Evening Briefing: Here's what you need to know... 70 NYTimes
471 0.0000 Thu Jun 07 22:32:06 +0000 2018 0.074 0.851 0.074 Half-price MetroCards for low-income New Yorke... 71 NYTimes
472 0.0000 Thu Jun 07 22:17:02 +0000 2018 0.000 1.000 0.000 RT @EricLiptonNYT: Been writing a lot re Pruit... 72 NYTimes
473 0.0000 Thu Jun 07 22:06:07 +0000 2018 0.000 1.000 0.000 We found a trove of secret documents after Mos... 73 NYTimes
474 0.0000 Thu Jun 07 21:56:02 +0000 2018 0.000 1.000 0.000 A 4-ingredient, 5-star roast chicken recipe ht... 74 NYTimes
475 0.0000 Thu Jun 07 21:46:02 +0000 2018 0.000 1.000 0.000 This weekend, get thee to Shakespeare in the P... 75 NYTimes
476 -0.4019 Thu Jun 07 21:30:16 +0000 2018 0.137 0.863 0.000 The Twitter habits of a sports executive’s wif... 76 NYTimes
477 0.0000 Thu Jun 07 21:15:07 +0000 2018 0.000 1.000 0.000 “We should do more and talk less.” That was th... 77 NYTimes
478 -0.4019 Thu Jun 07 21:01:05 +0000 2018 0.130 0.870 0.000 RT @TheSteinLine: .@StephenCurry30 insists on ... 78 NYTimes
479 -0.4019 Thu Jun 07 20:45:04 +0000 2018 0.114 0.886 0.000 Is Glen Buchenbach a rare Scotch? Nope. It's m... 79 NYTimes
480 0.7717 Thu Jun 07 20:32:24 +0000 2018 0.000 0.774 0.226 RT @tannercurtis: The New York issue of @NYTma... 80 NYTimes
481 0.1189 Thu Jun 07 20:30:05 +0000 2018 0.169 0.563 0.268 More and more, politicians in competitive race... 81 NYTimes
482 0.0000 Thu Jun 07 20:15:06 +0000 2018 0.000 1.000 0.000 The New Jersey Legislature voted to legalize s... 82 NYTimes
483 0.7783 Thu Jun 07 20:07:02 +0000 2018 0.000 0.764 0.236 RT @MichaelPaulson: Who will win the big prize... 83 NYTimes
484 0.0762 Thu Jun 07 20:00:21 +0000 2018 0.000 0.939 0.061 Couples do not pay for their wedding announcem... 84 NYTimes
485 -0.2263 Thu Jun 07 19:53:08 +0000 2018 0.112 0.888 0.000 RT @kchangnyt: Life not discovered on Mars. Bu... 85 NYTimes
486 0.0000 Thu Jun 07 19:45:07 +0000 2018 0.000 1.000 0.000 Almost all mammals experience REM sleep, but e... 86 NYTimes
487 -0.2716 Thu Jun 07 19:30:11 +0000 2018 0.162 0.838 0.000 Delta is the latest carrier to warn that fligh... 87 NYTimes
488 0.2732 Thu Jun 07 19:30:06 +0000 2018 0.090 0.769 0.140 Google, reeling from an employee protest over ... 88 NYTimes
489 0.0000 Thu Jun 07 19:15:05 +0000 2018 0.000 1.000 0.000 Democrats appear set to test whether conventio... 89 NYTimes
490 -0.1280 Thu Jun 07 19:03:01 +0000 2018 0.067 0.933 0.000 RT @nytimesbusiness: Look to Iowa's vast acres... 90 NYTimes
491 -0.7181 Thu Jun 07 19:00:07 +0000 2018 0.240 0.760 0.000 "E-scooters might look and feel kind of dorky,... 91 NYTimes
492 0.0624 Thu Jun 07 18:45:04 +0000 2018 0.109 0.771 0.120 The judge could not help himself. He used term... 92 NYTimes
493 -0.5267 Thu Jun 07 18:40:01 +0000 2018 0.159 0.841 0.000 An internal White House analysis of President ... 93 NYTimes
494 -0.9081 Thu Jun 07 18:30:04 +0000 2018 0.388 0.612 0.000 While evil clowns and serial killers at sorori... 94 NYTimes
495 -0.5267 Thu Jun 07 18:22:35 +0000 2018 0.167 0.833 0.000 RT @nytimesarts: A lost John Coltrane recordin... 95 NYTimes
496 -0.5106 Thu Jun 07 18:15:02 +0000 2018 0.125 0.875 0.000 Liz and Dick Uihlein are spending big to sway ... 96 NYTimes
497 -0.3818 Thu Jun 07 18:00:14 +0000 2018 0.126 0.874 0.000 Kate Spade came from the sort of modest beginn... 97 NYTimes
498 0.0000 Thu Jun 07 17:58:05 +0000 2018 0.000 1.000 0.000 G-7 meetings are usually moments of camaraderi... 98 NYTimes
499 -0.4404 Thu Jun 07 17:45:11 +0000 2018 0.127 0.873 0.000 When José, 5, landed in Michigan, all he carr... 99 NYTimes

500 rows × 8 columns

news_df.to_csv("Twitter_News_Mood.csv", index=False)
#plot scatterplot using a for loop.
#sns.set(color_codes=True)

for target in target_terms:
    df = news_df.loc[news_df["User"] == target]
    plt.scatter(df["Tweets Ago"],df["Compound"],label = target)
    
# adding y limit and x limit
plt.xlim(101,-2)
plt.ylim(-1,1)
    
#Add legend
plt.legend(bbox_to_anchor = (1,1))

#Add title, x axis label, and y axis label.
plt.title("Sentiment Analysis of News Tweets (06/08/2018)")
plt.xlabel("Tweets Ago")
plt.ylabel("Tweet Polarity")


#Set a grid on the plot.
plt.grid()

plt.savefig("Sentiment Analysis of News Tweets")
plt.show()

png

average_sentiment = news_df.groupby("User")["Compound"].mean()
average_sentiment
User
BBCWorld   -0.072110
CBSNews    -0.073402
CNN         0.078002
FoxNews     0.013268
NYTimes    -0.097817
Name: Compound, dtype: float64
# plotting graph
x_axis = np.arange(len(average_sentiment))
xlabels = average_sentiment.index
count = 0
for sentiment in average_sentiment:
    plt.text(count, sentiment+.01, str(round(sentiment,2)), ha = 'center', va = 'center')
    count = count + 1
plt.bar(x_axis, average_sentiment, tick_label = xlabels, color = ['r', 'b', 'y', 'g', 'c'])
#Set title, x axis label, and y axis label.
plt.ylim(-.1,.12)
plt.title("Overall Sentiment of News Tweets (06/08/2018)")
plt.xlabel("News Channel")
plt.ylabel("Tweet Polarity")
plt.savefig("Overall Sentiment of News Tweets")
plt.show()

png