In [3]:
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 10)
# imports
import csv
from matplotlib import pyplot as plt
import numpy as np
import pylab
from collections import namedtuple
In [4]:
# Load the data
def load_tweet_affect(fstream):
reader = csv.reader(fstream, delimiter="\t")
header = next(reader)
print(header)
assert header == ["tweet_id", "hc", "bs", "dt", "tc", "valence", "arousal", "dominance"]
return np.array([[float(i) for i in row] for row in reader])
def load_user_affect(fstream):
reader = csv.reader(fstream, delimiter="\t")
header = next(reader)
print(header)
assert header == ["user_id", "hc", "bs", "dt", "tc", "count", "valence", "arousal", "dominance"]
return np.array([[float(i) for i in row] for row in reader])
with open('data/twit_stance_affect.tsv') as f:
tweet_affect = load_tweet_affect(f)
with open('data/twit_user_stance_affect.tsv') as f:
user_affect = load_user_affect(f)
FIELD_MAP = {key:i for i, key in enumerate(["id", "hc", "bs", "dt", "tc", "valence", "arousal", "dominance"])}
USER_FIELD_MAP = {key:i for i, key in enumerate(["id", "hc", "bs", "dt", "tc", "count", "valence", "arousal", "dominance"])}
In [14]:
USER_FIELD_MAP = {key:i for i, key in enumerate(["id", "hc", "bs", "dt", "tc", "count", "valence", "arousal", "dominance"])}
In [5]:
CANDIDATES = ['hc', 'bs', 'dt',] # 'tc']
AXES = ['valence', 'arousal', 'dominance']
In [10]:
# Plot tweet affect
for i, candidate in enumerate(CANDIDATES):
for j, axis in enumerate(AXES):
print("candidate: ", candidate, "axis: ", axis)
plt.subplot(len(CANDIDATES), len(AXES), i * len(AXES) + j + 1)
X, Y = tweet_affect.T[FIELD_MAP[candidate]], tweet_affect.T[FIELD_MAP[axis]]
z = np.polyfit(X, Y, 1)
p = np.poly1d(z)
print("y=%.6fx+(%.6f)"%(z[0],z[1]))
plt.scatter(X, Y, s=4, alpha=0.1)
plt.xlabel(candidate)
plt.ylabel(axis)
plt.xlim(-1,1)
plt.ylim(0,10)
pylab.plot(X,p(X),"r--")
# the line equation:
plt.savefig('tweet_stance_affect.png')
plt.show()
In [15]:
# Plot tweet affect
for i, candidate in enumerate(CANDIDATES):
for j, axis in enumerate(AXES):
print("candidate: ", candidate, "axis: ", axis)
plt.subplot(len(CANDIDATES), len(AXES), i * len(AXES) + j + 1)
X, Y = user_affect.T[USER_FIELD_MAP[candidate]], user_affect.T[USER_FIELD_MAP[axis]]
z = np.polyfit(X, Y, 1)
p = np.poly1d(z)
print("y=%.6fx+(%.6f)"%(z[0],z[1]))
plt.xlabel(candidate)
plt.ylabel(axis)
#plt.xlim(-1,1)
#plt.ylim(0,10)
plt.scatter(X, Y, s=4, alpha=0.1)
pylab.plot(X,p(X),"r--")
# the line equation:
plt.savefig('user_stance_affect.png')
plt.show()
In [ ]: