# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ### Analysing Data

import pandas as  pd

# reading the dataset
all_tweets = pd.read_csv('Machine Learning-tweets.csv')

# As I scraped 5000 tweets
print(len(all_tweets))

# +
# Text, No-of-Retweets, Followers, Friends

print(len(all_tweets.columns))
# -

print(len(all_tweets.loc[0]['Text']))

print(all_tweets.info())

all_tweets.describe()

all_tweets.head()

all_tweets.tail()

# ### Defining the Tweets as Viral or Not

import numpy as np

all_tweets['is_viral'] = np.where(all_tweets['No-of-Retweets'] > 1000, 1, 0)

# 0 means the tweet is not_viral and 1 mean is is_viral
all_tweets['is_viral'].value_counts()

# ### Feature Engineering

# Now finding the features that we think are relevant for our analysis for whether or not the tweet will go viral. We are considering tweet_length, followers_count, friends_count, hashtag_count, http_count, word_count and average_word_len as our features for training of model.

# +
# apply() function calls the lambda function and applies it to every row or 
# column of the dataframe and returns a modified copy of the dataframe:

# the axis = 1 means the column

all_tweets['tweet_length'] = all_tweets.apply(lambda tweet: len(tweet['Text']), axis=1)
# -

all_tweets['followers_count'] = all_tweets.apply(lambda tweet: tweet['Followers'], axis=1)

all_tweets['friends_count'] = all_tweets.apply(lambda tweet: tweet['Friends'], axis=1)

all_tweets['hashtags_count'] = all_tweets.apply(lambda tweet: tweet['Text'].count('#'), axis = 1)

all_tweets['http_count'] = all_tweets.apply(lambda tweet: tweet['Text'].count('http'), axis = 1)

all_tweets['word_count'] = all_tweets.apply(lambda tweet: len(tweet['Text'].split()), axis = 1)

all_tweets['average_word_len'] = all_tweets['tweet_length']*1.0 / all_tweets['word_count']

# ### Normalizing the Data
# To normalize the data assign names to it i-e to the features and label.

data = all_tweets[['tweet_length', 'hashtags_count', 'followers_count', 'friends_count', 'http_count', 'word_count', 'average_word_len']]

label = all_tweets['is_viral']

data.head()

label.head()

# ### Train and Test Splitting
# Not split the data into 80% for training and 20% for testing.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.2)

# ### KNN Classifier

# Training the model with KNN Classifier
from sklearn.neighbors import KNeighborsClassifier

# n_neighbors means the number of k
clf = KNeighborsClassifier(n_neighbors=5)

clf.fit(X_train, y_train)

clf.score(X_test, y_test)

# ### Optimal K
# Experimenting with different values of K. Plot them on a graph to see which value of k gives optimal accuracy. 
# The under-fitting and over-fitting which can happen on choosing too high or two low k values.

# This will plot graph vs accuracy for 100 values of k
import seaborn as sn
score = []
for k in range(1,100):
    classifier = KNeighborsClassifier(n_neighbors = k)
    classifier.fit(X_train, y_train)
    score.append(classifier.score(X_test, y_test))
sn.lineplot(range(1,100),score)



StarcoderdataJupyterScriptsDedupFiltered