Deep learning-based election results prediction using Twitter activity

Nowadays, political parties have widely adopted social media for their party promotions and election campaigns. During the election, Twitter and other social media platforms are used for political coverage to promote the party and its candidates. This research discusses and estimates the stability of many volumetric social media approaches to forecast election results from social media activities. Numerous machine learning approaches are applied to opinions shared on social media for predicting election results. This paper presents a machine learning model based on sentiment analysis to predict Pakistan's general election results. In a general election, voters vote for their favorite party or candidate based on their personal interests. Social media has been extensively used for the campaign in Pakistan general election 2018. Using a machine learning technique, we provide a five-step process to analyze the overall election results, whether fair or unfair. The work is concluded with detailed experimental results along with discussion on the outcomes of sentiment analysis for real-world forecasting and approval of general elections in Pakistan.


Introduction
The widespread use of the social media network for communication, social participation, and self-expression has resulted in many individual data being willingly disclosed online. The social disclosure on social media provides a platform for the researchers to measure general opinion unremarkably (Tumasjan et al. 2011). Computer technology-based social media platforms have benefited people for self-expression, communication, and social participation. These social media sources are responsible for sharing text, audio, and video links between individuals worldwide to be well informed and keeping in touch with others. This explosive social development can be seen on Facebook, Linked In, Friend Feed, Myspace, and many more, but the flow of Twitter is much more than other platforms (Kaplan and Haenlein 2010). Many people have been attracted by social media platforms such as Instagram, Twitter, and Facebook, which allow them to express their thoughts, feelings, and ideas about various people, places, and things. People give their views on these social websites for the political party election campaign. This study focuses primarily on Pakistan for the general election held in 2018. We found that the predictive power of social media performs well for Punjab province due to the population and Communicated by Irfan Uddin.
people's tendency toward social media. However, the social media platform was less useful for Baluchistan.
Nowadays, using social media platforms to stream opinions, emotions, or beliefs about personalities, places, or things exponentially increases. Sentiment analysis methods are mainly classified as Lexicon-based (Sebastiani 2002), machine-learning-based (Taboada et al. 2011), and hybrid. In addition, other categorizations are presented in Cambria (2016) with hybrid approaches, knowledge-based and statistical categories. There is increasing research in broad areas to evaluate computational sentiments and opinions. Therefore, the data available on social media are used to mining facts that are further used to predict elections. Social media behavioral analysis can achieve sentiment analysis accuracy and forecasting (Anjaria and Guddeti 2014). Sentiment analysis is the artificial intelligence process in which people share their emotional beliefs or opinions about different things such as events, places, political parties, and personalities. The trend of using this platform for sharing views is becoming more popular in elections. Before the election, party workers start a campaign for a political party of their interest. Recently in the 2018 election in Pakistan, several political parties began a trend on social media, such as ''Naya Pakistan,'' ''Vote Ko Izzat Do'' and ''Rotti Kapra Makkan.'' These were the top trend on social media, and people shared different emotions and opinions accordingly. Many believed that the election results were fair, and few thought that the results were unfair. These emotions and beliefs are classified as positive, neutral, or negative. To extract the opinion of individuals regarding the trends on social media is challenging. In the 2009 German federal election, more than 100,000 messages were processed using sentiment analysis referred to either a political candidate or a political party (Skoric et al. 2012). In recent years, we have seen a dramatic increase in the use of social networks.
The popularity of Pakistan's political parties in the general election 2013 was tested using a keyword-based tweet collection that concentrated on political parties' names and political celebrities (Razzaq et al. 2014). The dataset was tested on both unsupervised and supervised algorithms such as K-nearest neighbors (KNN) (Soucy and Mineau 2001), naïve Bayes (NB) (Lewis 1998), random forest, support vector machines (SVM) (Segnini and Motchoffo 2021), Naïve Bayes multinomial (NBMN) (Raschka 1410) applied prind and rainbow tool classification technique on unigram data. Porter and Laplace stemmer (Raschka 1410;Porter 1980) remove zeros from the data to smooth the data. Inverse document frequency and term frequency (TF-IDF) (Ramos 1983) were used for relevant documents to find words that are related strongly. It also used the Waikato for Analysis of Knowledge environment to perform fivefold cross-validation in (Weka) (Hasan et al. 2018;Hall et al. 2009). The initial intention of choosing data from Twitter is to gain information better from this stage since Twitter holds the politicians authenticated accounts, not Instagram or Facebook. Similarly, contrary to Facebook, Twitter confines users to give their complete and compact opinions in 280 characters.
In (Miranda Filho et al. 2015;Castro et al. 2017), it has been confirmed that with Twitter, people can gain knowledge from their accounts instead of gathering information on traditional methods of observation. In addition, the authors in Kanavos et al. (2017) developed a model for harnessing the emotions from tweets, while large-scale data were analyzed Jan et al. 2019) for sentiment analysis. Social communities have been described with an influential impact in Kanavos et al. (2018), which has a metrical meaning attributed to each emotional user's position. As a result, the paper's input involves election sentiment analyses obtained from users on Twitter, with separate sentiment analyzers. Moreover, using machine learning classifiers, we receive the validation results from each analyzer ) our experiment basis on comparing several sentiment analyzers and validating the results of the different classifiers. In general, the following are the main contributions of this research: • We examine the effects of many well-known events on election prediction with social media. • The deep learning models are used to propose an eventbased sentiment analysis method for election prediction. • We investigated the performance of the proposed work, which achieved better results as compared to Naïve Bayes, and support vector machine.
The remaining of the paper is organized as follows. Section 2 covers related literature in the domain of sentiment analysis on Twitter-based election prediction results. Section 3 presents the proposed technique, while Sect. 4 includes the experimental discussion and results. Finally, Sect. 5 concludes the paper along with future directions.

Related work
It has been observed that data from Twitter are more appropriate for making a low-level prediction of political sentiment than predicting a particular outcome of a general election (Kaplan and Haenlein 2010). Additionally, reference (Anjaria and Guddeti 2014) adopted a hybrid method that elaborates supervised classifiers (SVM, artificial neural networks with peak entropy, and Naïve Bayes). Authors devised a novel approach of sentiment analysis called ''TWEEL'' ZER'' (TWEE) TS ? ANA (LYZER) .
The sentiment analysis technique was used for real-time analysis of public opinion about the United States presidential election 2012, predicting electoral outcomes from social media data. The results show public views about the election campaigns (Wang et al. 2012). The Singapore General Elections in 2011 also demonstrated the benefits of the Twitter and social network applications, which show Twitter to be a vital part of the election campaign and the voting evaluation of the voters (Skoric et al. 2012). Seven political parties concentrate and draw the results of the MAE at 5.13%. On the other hand, they were incompetent to relate a special relationship among the percentage of votes cast against and the number of received tweets (Skoric et al. 2012). An opinion extraction variation has been exploited, including unigrams, bigrams, part of speech tags, etc. A standard option would be to use the unorganized data representation as a feature to retrieve the best results from previous work that deals with enormous text. The goal is to categorize a tweet into one of the three categories of neutral, positive, and negative sentiments (Hussain et al. 2018;Pang and Lee 2004). On 23 February 2019, Nigeria, the most populous country and the largest economy in Africa, held presidential elections. Many Nigerians expressed their opinions in favor or against different presidential candidates on social media. Authors used a post-election sentiment analysis from Nairaland (targeted social network for Nigerians) using supervised machine learning and lexicon-based methods to detect their polarity feelings (i.e., positive or negative) in 118,421 posts collected (Wilson et al. 2005).
Daniel GayoAvello et al. used the datasets belonging to USsen10 and MAsen10 to organize several sorts of sentiment or opinion analytic research to explore the prediction forecast of a social network against multiple Senators running (Gayo-Avello 2013). They believe that election results cannot be forecast simply by exchanging tweets, and that opinion polls must be conducted in order to exhibit positive results. In addition, the authors (Bansal and Srivastava 2019) used the features of the N-gram to predict the 2017 Uttar Pradesh legislative elections (UP). Also, the distribution of feelings with emojis was expressively dissimilar to tweets without emojis (Navigli 2009). Wang and his colleagues proposed a sentiment analysis system for political tweets collected in the U.S. presidential election of 2012. They collected over 36 million tweets and analyzed the sentiment opinion using Amazon Mechanical Turk. Using a Naive Bayes algorithm with unigram features, their model achieved 59% accuracy on four labels (Wang et al. 2012).
Mohbey, K.K On the Twitter election dataset, used a multi-class technique to forecast user behavior using a deep learning framework. A comparison of traditional methodologies such as Nave Bayes, SVM, decision trees, and logistic regression with the used deep learning method is presented. According to experimental results, the model can achieved up to 98.70% accuracy on multiclass based prediction (Mohbey 2020). Furthermore, Barat et al. two feature extraction algorithms BOW and tf-idf are used and integrated with five machine learning approaches for the Indian Loksabha general elections 2019 (Batra et al. 2020).

Preliminaries used in the proposed study
Some of the terminologies for tweets and the sources used for our data-driven access are listed below.
Tweets The Twitter message or letter is known as a tweet. Tweets shall be limited to 280 characters. A tweet is made up of a group of words, such as a retweet (RT), which refers to a republished tweet from a previous post. In addition, ''@twUser'' means a reply to the user ''twUser;'' and ''# tag'' is a tag used by the user for this message known as a hashtag that provides a link to an external source.
Data Sources The data are collected from Twitter's tweet that provides real-time sentiment or opinion about the upcoming election: Twitter opinion and tweet feel. We built our labeled dataset by gathering tweets from Twitter for three to four weeks, then manually performing sentiment labeling. Table 1 shows details about these sources, tweets, and manually labeled tweets. Some of the tweets provide three class detection: positive, neutral, and negative. The crawling method obtained a very different number of tweets from each tweeter that might result from differences between their sampling procedures of a Twitter stream or a particular type of filtering process applied to outcomes. By its nature, a site may present only the tweets in which opinion it has more confidence. The data sources, tweets and labels are shown in Table 1.
In this article, we did not focus on introducing any method of sentiment-prediction, but the unique opinion methods analyzing in election areas. In the end, the dataset is based on user-based tweets about politics across Pakistan. The analyzers types are also using in other areas such as disease, personality, and health prediction. Furthermore, our analyzer outcomes are thus certified from machine learning classifiers like SVM, Naïve Bayes, and deep neural networks, using RapidMiner.

Proposed methodology
The proposed election results prediction framework predicts Twitter opinions of the general election 2018 in Pakistan. The tweets were posted about current trends of the political party, which users in hashtags consider to express their views. The received tweets are stored in the Deep learning-based election results prediction using Twitter activity 7537 database, and the dataset is pre-processed. After pre-processing, the resting dataset is divided into the testing and training set with 2100 and 900 tweets, respectively. Deep learning is then applied to the training set in RapidMiner, and a classification model is established. The model is tested on the testing dataset, and its accuracy is compared with state-of-the-art schemes. Figure 1 presents a clear picture of the sentiment analysis framework.

Deep learning framework
Deep learning has emerged as the most crucial topic of research in recent years. It is a type of machine learning that uses neural networks to represent several layers of abstraction (Hussain et al. 2018). The architecture of deep learning is made up of several levels. These different layers are used to extract and manipulate features. The previous layer's output is fed into the next ones. Deep networks have the potential to create algorithms to handle problems like opinion mining. Deep learning refers to the many transformations and representations of data from the input to the output layer. Between the input and output layers, there are several hidden layers.

Neural network
Neural networks are neuron-based computational models. In a neural network, a neuron is a basic processing unit that serves as a network node. A feed-forward neural network is an artificial neural network with no directed cycle of connections between the units. The information in this network only flows in one direction, forward, from the input nodes to the output nodes, passing through any hidden nodes (if any). In the network, there are no cycles or loops. The election custom sample dataset's survived attribute is predicted using a neural network. The split validation is Best wishes and all pray for Fair Pakistan Election 2018 Neutral Fig. 1 Proposed methodology for election prediction used to generate the training and testing datasets for model evaluation. The parameters of the neural networks are set to their default values. The input layer is the first layer, and it is used to provide data to the network as shown in Fig. 2. The output layer is responsible for the model's prediction. The hidden layers can be used to determine the network's depth. To understand more complex functions, more hidden layers can be used. When the model is trained with data, these hidden layers learn on their own. The only nondefault parameter is the hidden layer sizes, where convolutional, max-pooling and fully connected layers a (3 layers) are used. The neurons in the hidden layers will use the activation function (nonlinearity). To promote generalization, a portion of the inputs for each hidden layer will be excluded from training. The default dropout value for each hidden layer is 0.5.

Data extraction
The Twitter public application programming interface (API) collects tweets that allow a crawler to access the Twitter site. While helping a significant number of functions interact with Twitter, the API action is more relevant to acquiring a Twitter dataset. In this way, 2090 tweets, along with different party names and celebrities, are collected. These tweets were based on keywords/the top trend with the full name running on Twitter during/after the general election of Pakistan 2018.

Data review preparing
This framework's first stage involves manually labeling Twitter data using sentiment labels. Table 1 shows more details about these sources, tweets, and manual labels of sample tweets.

Data pre-processing
The data collected from the Tweeter API are in JSON (JavaScript Object Notation) format, where further processing can extract the text field. The classification can be performed only in the text section of the JSON format. It is essential to perform some pre-processing steps before training the model, which are listed below.

Data transformation
The data in the form of text retrieved from JSON are in lower and upper case. It is necessary to transform the entire data into the same case. Therefore, the collected data are converted into lower case, i.e., to small letters.

Data tokenization
Tokenization breaks the raw text into tiny parts of speech called tokens. It helps to understand the context of those tokens. By analyzing the sequence of words, tokenization allows to interpret the text. For example, the text ''It's raining'' can be labeled as 'It,' 'It's,' 'It's raining.'

Fig. 2 A neural network's structure for election prediction
Deep learning-based election results prediction using Twitter activity 7539

Data stemming
Stemming is used to clear the data from undesirable words or text with no meaning or information in a particular sentence (stop words). Most of the time, words such as, are, is, end, etc., are removed from the text data. Moreover, the suffixes and affixes are also withdrawn from the data, and only the word stem is left over.

Training of the proposed deep learning model
The proposed model uses deep learning for sentiments/ opinions analysis to skillfully predict general election results through Twitter data. We suggest three main steps, which are as follows.
Step 1 Prepare data files for the classifier.
• Create a tweet using provided sentiment (testing validation). • Create a tweet file of each sentiment analyzer in neutral, negative, and positive labels (training validation).
Step 2 Deep Learning model on RapidMiner.
• Create a model of each analyzer from the training set.
• Apply the string to word vector filter with following parameters: • Transform cases (all to one format: lower to upper case or upper to lower case) • Tokenization • Filter stop words (English) • Tokenize by length • Stem (Porter stemmer) Step 3: Execute the model on the testing set.
• Load the set file of testing.
• Apply the string to word vector filter with following parameters: • Transform cases (all to one format: lower to upper case or upper to lower case) • Tokenization • Filter stop word (English) • Tokenize by length • Stem (Porter) • On the testing set, execute the model. • In the output file, save results.

The flowchart diagram of the proposed work
In this section, we example the flowchart of the proposed work as shown in Fig. 3. The first step is start from data collection followed by data pre-processing for data purification. After data pre-processing, the word vector construction is used to make prepare data for deep learning.
Then select deep learning model to train the model and finally the performance evaluation is used to check the performance of the system (Table 2).

Data collection
In this paper, our main target is to gather general data for data collection and retrieving tweets from social media websites. Tweeter's Tweepy API was used to collect public opinions and to retrieve tweets from predefined Twitter accounts. Setting Tweepy API and support the Twitter account data from the retrieved tweets of the retrieved hashtags from Twitter accounts. A database is used to save the retrieved tweets. On Twitter, trend-getting hashtags representing people's political views were selected during the 2018 general election in Pakistan. In addition, more political hashtags have been sought to gather more people's feelings/opinions. The tweet against the hashtags, the retrieved tweets from Twitter provide almost real-time sentiment or opinion detection for tweets. Furthermore, a team of 5 people assigned labels such as positive, negative, and neutral to the retrieved tweets, and then a sixth person calculated average labels. Table 3 shows number of positive, negative and neutral tweets. The datasets used in the proposed sentiment analysis framework are the actual tweeter opinions/sentiments of people regarding Pakistan's general election, held on the 25th of July 2018. The dataset contains 3000 tweets. We extracted some of the tweets manually, then split them into two parts, 70% for training and 30% for testing, to validate the proposed technique.

Performance evaluation of the proposed system
Various types of experiments have been conducted to determine the accuracy of the proposed method on the three classes (positive, negative, and neutral). We compared the results with two sentiment analysis methods (Naïve Bayes and SVM) using our dataset. The results show that the proposed technique is helpful in terms of predictive labels compared to state-of-the-art methods, i.e., Naive Bayes and SVM (Razzaq et al. 2014;Hasan et al. 2018). Figure 4 shows performance evaluation of Naïve Bayes, SVM and proposed method for election prediction. In Fig. 4, the x-axis shows the predictive value, and the y-axis shows the performance parameter. A training set containing 2100 tweets (1314 positive, 588 negatives, and 192 neutral) was analyzed by each sentiment analyzer. In contrast, the testing set consisted of 900 tweets. During the comparison, we found that our proposed technique is better than state-of-the-art schemes in terms of predictive positive, predictive negative, and predictive neutral. The Naïve Bayes has predictive positive 54%, predictive negative 30%, and predictive neutral 15%, while SVM results in 57%, 33%, and 12%, respectively. The proposed technique results in 60% predictive positive, 30% predictive negative, and 20% predictive neutral, as shown in Fig. 4.
The precision of the Naïve Bayes and SVM's proposed method is 88.24% using Eq. (1), while Naïve Bayes and SVM are 78.43% and 80.72%, respectively. It measures the documents retrieved relevant to the user's information need, also referred to as positive predictive value (PPV). For the positive patterns, precision is used for accurate prediction in a positive class. The recall is the fraction of the relevant documents that have been successfully recovered. The recall specifies that the true positive retains the context and points out the positive fraction patterns that are classified correctly. The recall is calculated using Eq. (2), and the obtained value for the proposed method is 97.14% which is much better than Naïve Bayes and SVM, as shown in Table 4. F-score is the combination of recall and precision, which can be calculated using Eq. (3). The proposed method has better F-score of 92.47% than Naïve Bayes and SVM.
As shown in Fig. 5, the y-axis is the predictive value, and the x-axis is the predictive parameter instance. The proposed deep learning approach has the highest average accuracy and correct illustration compared to Naive Bayes and SVM. The results show that the proposed deep learning approach gained 71% of average accuracy, while Naive Bayes got 65% and SVM 67%, as shown in Fig. 5.

Conclusion and future directions
The use of social media for the outcomes of election results poses different challenges at different stages. This article first tackled the training data scarcity for text classification by providing a framework in two steps. Then, we proposed our model for predicting election results which uses the labeled data created using our framework. While to predict the results, our model alone may not be sufficient, it becomes a crucial component when combined with other statistical models and offline techniques. We focused on determining the approach to adopting different machine learning algorithms with the maximum accuracy learning rate about election sentiment. In documents, words of many semantic orientation sentences or phrases are calculated using lexicon-based sentiment analysis. However, to classify semantic orientation sentences for phrases or text, algorithms such as SVM, Naïve Bayes, and deep learning techniques are used. We investigated the predictive power of Twitter using sentiment analysis. Using different classifiers, we have achieved an average of 71% accuracy for proposed deep learning, 67% accuracy for SVM, and 65% accuracy for Naïve Bayes. The work of predicting fair elections for developing countries in this paper, especially Pakistan, is unique. After applying different classifiers, we conclude that our proposed deep learning classifier is best for the fair prediction of an election compared to two other existing schemes. In future works, we aim to explore large datasets using deep learning models for sentiment analysis and opinion mining.