electronics Article Sentiment Analysis of before and after Elections: Twitter Data of U.S. Election 2020 Hassan Nazeer Chaudhry 1, *, Yasir Javed 2 , Farzana Kulsoom 3 , Zahid Mehmood 4, * , Zafar Iqbal Khan 2 , Umar Shoaib 5 and Sadaf Hussain Janjua 6   Citation: Chaudhry, H.N.; Javed, Y.; Kulsoom, F.; Mehmood, Z.; Khan, Z.I.; Shoaib, U.; Janjua, S.H. Sentiment Analysis of before and after Elections: Twitter Data of U.S. Election 2020. Electronics 2021, 10, 2082. https:// doi.org/10.3390/electronics10172082 Academic Editors: Amir H. Gandomi, Fang Chen and Laith Abualigah Received: 29 July 2021 Accepted: 24 August 2021 Published: 27 August 2021 Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affil- iations. Copyright: © 2021 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https:// creativecommons.org/licenses/by/ 4.0/). 1 Dipartimento di Elettronica, Informazione e Bioingegneria (DEIB), Politecnico di Milano, 20133 Milano, Italy 2 Department of Computer and Information Sciences, CCIS, Prince Sultan University, Riyadh 66833, Saudi Arabia; yjaved@psu.edu.sa (Y.J.); zkhan@psu.edu.sa (Z.I.K.) 3 Department of Telecommunication Engineering, University of Engineering and Technology Taxila, Taxila 47080, Pakistan; farzana.hassan@uettaxila.edu.pk 4 Department of Computer Engineering, University of Engineering and Technology Taxila, Taxila 47080, Pakistan 5 Department of Computer Science, University of Gujrat, Gujrat 50781, Pakistan; umar.shoaib@uog.edu.pk 6 Department of Computer Sciences, Quaid-i-Azam University, Islamabad 45320, Pakistan; shussain@cs.qau.edu.pk * Correspondence: hassannazeer.chaudhry@polimi.it (H.N.C.); zahid.mehmood@uettaxila.edu.pk (Z.M.) Abstract: U.S. President Joe Biden took his oath after being victorious in the controversial U.S. elections of 2020. The polls were conducted over postal ballot due to the coronavirus pandemic following delays of the announcement of the election’s results. Donald J. Trump claimed that there was potential rigging against him and refused to accept the results of the polls. The sentiment analysis captures the opinions of the masses over social media for global events. In this work, we analyzed Twitter sentiment to determine public views before, during, and after elections and compared them with actual election results. We also compared opinions from the 2016 election in which Donald J. Trump was victorious with the 2020 election. We created a dataset using tweets’ API, pre-processed the data, extracted the right features using TF-IDF, and applied the Naive Bayes Classifier to obtain public opinions. As a result, we identified outliers, analyzed controversial and swing states, and cross-validated election results against sentiments expressed over social media. The results reveal that the election outcomes coincide with the sentiment expressed on social media in most cases. The pre and post-election sentiment analysis results demonstrate the sentimental drift in outliers. Our sentiment classifier shows an accuracy of 94.58% and a precision of 93.19%. Keywords: sentiment analysis; Twitter; presidential election; prediction; natural language processing 1. Introduction The U.S. election, 2020 was a significant global event, as the Republican Party’s Donald Trump was striving to secure his second term while Joe Biden of the Democratic Party expected to turn it around. The pre-election polls assessed the U.S. public’s sentiments to evaluate the likelihoods for each candidate. The BBC poll suggested that Joe Biden was ahead of Donald Trump and marked the elections’ battleground [1]. However, among these states, the margin of victory was very close, and it could have swung in favor of either candidate. Other two-way and four-way online polls such as 270 to win and Real clear politics showed the narrow dominance of Joe Biden. The nationwide polls such as Ipsos/Reuters [2], CNBC, Yahoo News [3], NBC/WSJ [4], Fox News [5], CNN/WSJ [6], ABC/Washington Post [7], and others reported public sentiment in favor of Joe Biden. However, since U.S. elections are decided by the Electoral College rather than on casted votes, predicting elections on public sentiment is not straightforward. It might reflect the public opinion in one sense; however, it could sway in favor of any candidate with such a narrow margin. The 2020 U.S. election took place on 3 November 2020; the final results of the election declared Joe Biden Electronics 2021, 10, 2082. https://doi.org/10.3390/electronics10172082 https://www.mdpi.com/journal/electronics