<jupyter_start><jupyter_text><jupyter_code>import nltk                      
from os import getcwd
import pandas as pd              
from nltk.corpus import twitter_samples 
import matplotlib.pyplot as plt    
import numpy as np                  


nltk.download('twitter_samples')
all_positive_tweets = twitter_samples.strings('positive_tweets.json')
all_negative_tweets = twitter_samples.strings('negative_tweets.json')

tweets = all_positive_tweets + all_negative_tweets
labels = np.append(np.ones((len(all_positive_tweets),1)), np.zeros((len(all_negative_tweets),1)), axis = 0)


train_pos  = all_positive_tweets[:4000]
train_neg  = all_negative_tweets[:4000]

train_x = train_pos + train_neg<jupyter_output>[nltk_data] Downloading package twitter_samples to /root/nltk_data...
[nltk_data]   Unzipping corpora/twitter_samples.zip.<jupyter_text><jupyter_code>from google.colab import files
  
uploaded = files.upload()
data = pd.read_csv('logistic_features.csv'); 
data.head(10) 

X = data[['bias', 'positive', 'negative']].values 
Y = data['sentiment'].values; 
theta = [7e-08, 0.0005239, -0.00055517]

fig, ax = plt.subplots(figsize = (8, 8))

colors = ['red', 'green']

ax.scatter(X[:,1], X[:,2], c=[colors[int(k)] for k in Y], s = 0.1)  
plt.xlabel("Positive")
plt.ylabel("Negative")
# Equation for the separation plane
# It give a value in the negative axe as a function of a positive value
# f(pos, neg, W) = w0 + w1 * pos + w2 * neg = 0
# s(pos, W) = (w0 - w1 * pos) / w2
def neg(theta, pos):
    return (-theta[0] - pos * theta[1]) / theta[2]

# Equation for the direction of the sentiments change
# We don't care about the magnitude of the change. We are only interested 
# in the direction. So this direction is just a perpendicular function to the 
# separation plane
# df(pos, W) = pos * w2 / w1
def direction(theta, pos):
    return    pos * theta[2] / theta[1]

fig, ax = plt.subplots(figsize = (8, 8))

colors = ['red', 'green']


ax.scatter(X[:,1], X[:,2], c=[colors[int(k)] for k in Y], s = 0.1)  # Plot a dot for each pair of words
plt.xlabel("Positive")
plt.ylabel("Negative")

 
maxpos = np.max(X[:,1])

offset = 5000 


ax.plot([0,  maxpos], [neg(theta, 0),   neg(theta, maxpos)], color = 'gray') 

# Plot a green line pointing to the positive direction
ax.arrow(offset, neg(theta, offset), offset, direction(theta, offset), head_width=500, head_length=500, fc='g', ec='g')
# Plot a red line pointing to the negative direction
ax.arrow(offset, neg(theta, offset), -offset, -direction(theta, offset), head_width=500, head_length=500, fc='r', ec='r')

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14460596786534047, 'fraction_numerical': 0.029839326702371844, 'mean_word_length': 4.4365904365904365, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '288518', 'license': 'MIT', 'n_tokens_mistral': 1013, 'n_tokens_neox': 936, 'n_words': 256, 'path': 'Visualizing_tweets_and_the_Logistic_Regression_model.ipynb', 'repo_name': 'sakshamceo/Twitter-Sentiment-Analysis_Project_NLP', 'size': '2614'}