-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
67 lines (50 loc) · 1.57 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import matplotlib
import numpy as np
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import Price, Tweet
from itertools import islice
from matplotlib import pyplot as plt
username = "postgres"
password = "password"
port = "5433"
db = "twitterbot"
print ( "Connecting to database\n")
engine = create_engine("postgresql+psycopg2://{}:{}@localhost:{}/{}".format(username, password, port, db))
print ( "Connected!\n" )
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
def window(seq, n=1000):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def moving_averages(values, size):
for selection in window(values, size):
yield sum(selection) / size
sentiments = []
sentiment_MAs = []
print("processing tweet sentiments...\n")
tweets = session.query(Tweet).all()
for tweet in tweets:
sentiments.append(tweet.sentiment)
print(sentiments)
print("calculating moving averages...\n")
for avg in moving_averages(sentiments, 1000):
sentiment_MAs.append(avg)
print(sentiment_MAs)
#plot sentiment moving average over the length of the array
length = len(sentiment_MAs)
y = sentiment_MAs
x = np.arange(0, length)
plt.xlim(0,length)
plt.ylim(-1,1)
plt.plot(x, y, ".")
plt.show()