diff --git a/Jonas_Solutions/Task_02_JonasWeinz.ipynb b/Jonas_Solutions/Task_02_JonasWeinz.ipynb index 4caca7b..2cfca3e 100644 --- a/Jonas_Solutions/Task_02_JonasWeinz.ipynb +++ b/Jonas_Solutions/Task_02_JonasWeinz.ipynb @@ -14,17 +14,21 @@ "* Tutorial on Datacamp: https://www.datacamp.com/community/tutorials/scikit-learn-fake-news\n", "\n", "* liar dataset paper: https://www.cs.ucsb.edu/~william/papers/acl2017.pdf\n", - " * dataset: https://www.cs.ucsb.edu/~william/data/liar_dataset.zip\n", - "\n", + " * dataset: https://www.cs.ucsb.edu/~william/data/liar_dataset.zip" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Dependencies for this Notebook:\n", "* library [rdflib](https://github.com/RDFLib/rdflib)\n", - " * install: `pip3 install rdflib`\n", - "* " + " * install: `pip3 install rdflib`\n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -41,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -49,9 +53,65 @@ "import numpy as np\n", "import itertools\n", "import sklearn.utils as sku\n", + "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer, HashingVectorizer\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.linear_model import PassiveAggressiveClassifier\n", + "from sklearn.naive_bayes import MultinomialNB\n", + "from sklearn import metrics\n", + "import matplotlib.pyplot as plt\n", + "from pprint import pprint as pp\n", "import os" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tools used later" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "def plot_confusion_matrix(cm, classes,\n", + " title,\n", + " normalize=False,\n", + " cmap=plt.cm.Blues):\n", + " fig_1, ax_1 = plt.subplots()\n", + " \"\"\"\n", + " See full source and example: \n", + " http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html\n", + " \n", + " This function prints and plots the confusion matrix.\n", + " Normalization can be applied by setting `normalize=True`.\n", + " \"\"\"\n", + " plt.imshow(cm, interpolation='nearest', cmap=cmap)\n", + " plt.title('Confusion Matrix for: ' + title)\n", + " plt.colorbar()\n", + " tick_marks = np.arange(len(classes))\n", + " plt.xticks(tick_marks, classes, rotation=45)\n", + " plt.yticks(tick_marks, classes)\n", + "\n", + " if normalize:\n", + " cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n", + " print(\"Normalized confusion matrix\")\n", + " else:\n", + " print('Confusion matrix, without normalization')\n", + "\n", + " thresh = cm.max() / 2.\n", + " for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n", + " plt.text(j, i, cm[i, j],\n", + " horizontalalignment=\"center\",\n", + " color=\"white\" if cm[i, j] > thresh else \"black\")\n", + "\n", + " plt.tight_layout()\n", + " plt.ylabel('True label')\n", + " plt.xlabel('Predicted label')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -63,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -103,37 +163,16 @@ " UNZIPOPT: [none]\n", " ZIPINFO: [none]\n", " ZIPINFOOPT: [none]\n", - "Archive: liar_dataset.zip\n", - " inflating: README \n", - " inflating: test.tsv \n", - " inflating: train.tsv \n", - " inflating: valid.tsv \n", "================================================================================\n", "successfully finished action: downloading and unpacking https://www.cs.ucsb.edu/~william/data/liar_dataset.zip if not already existing\n", "================================================================================\n", "================================================================================\n", "downloading and unpacking https://raw.githubusercontent.com/GeorgeMcIntire/fake_real_news_dataset/master/fake_or_real_news.csv.zip if not already existing\n", "================================================================================\n", - "Archive: fake_or_real_news.csv.zip\n", - " inflating: fake_or_real_news.csv \n", - " creating: __MACOSX/\n", - " inflating: __MACOSX/._fake_or_real_news.csv \n", "================================================================================\n", "successfully finished action: downloading and unpacking https://raw.githubusercontent.com/GeorgeMcIntire/fake_real_news_dataset/master/fake_or_real_news.csv.zip if not already existing\n", "================================================================================\n" ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 989k 100 989k 0 0 366k 0 0:00:02 0:00:02 --:--:-- 366k\n", - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 11.3M 100 11.3M 0 0 2590k 0 0:00:04 0:00:04 --:--:-- 2751k\n" - ] } ], "source": [ @@ -150,7 +189,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -166,7 +205,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -321,28 +360,274 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "def create_test_dset(dset, cutoff=0.7):\n", - " shuffled = sku.shuffle(dset)\n", + "def create_training_and_test_set(dset, cutoff=0.7):\n", + " shuffled = sku.shuffle(dset) # shuffle dataset\n", " y = shuffled.label\n", - " df_1 = shuffled.drop('label', axis=1)\n", + " shuffled = shuffled.drop('label', axis=1)['text']\n", + " size = int(cutoff * shuffled.shape[0])\n", + " return shuffled[:size], y[:size], shuffled[size:], y[size:]\n", " " ] }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "X,y, Xt,yt = create_training_and_test_set(df_1)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "#X2,y2, Xt2,yt2 = train_test_split(df_1['text'],df_1.label,test_size=0.3)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Unnamed: 0\n", + "5507 Anthony Weiner Sends Apology Sext To Entire Cl...\n", + "1440 John Kasich was killing it with these Iowa vot...\n", + "3172 Yeah, yeah, with the rise of Der Trumper and t...\n", + "6962 Vermont fights the opioid epidemic by limiting...\n", + "6970 — Adam Baldwin (@AdamBaldwin) October 28, 2016...\n", + "7209 Militias prepare for election unrest while Chr...\n", + "4781 Raleigh, North Carolina (CNN) As soon as the f...\n", + "6137 Yemen’s Hudaydah suffering from dire humanitar...\n", + "8799 Subscribe \\nIn politics, the Third Way is a po...\n", + "4374 Hanging in the balance is Obama’s vision of Am...\n", + "9465 Under the Surface - Naomi Klein and the Great ...\n", + "10409 October 28, 2016 at 9:00 PM \\nWhy would Putin ...\n", + "2822 HAMPTON, N.H. -- Hillary Rodham Clinton said F...\n", + "5190 (CNN) Donald Trump on Wednesday refused to say...\n", + "2286 Gay rights won't fade as a political issue. Th...\n", + "4308 Fox Business’s Tuesday night Republican debate...\n", + "7305 Yes, There Are Paid Government Trolls On Socia...\n", + "6716 Vladimir Putin: The United States continues to...\n", + "6871 By wmw_admin on October 31, 2016 Sanchez Manni...\n", + "8527 Banana Republic Election in the United States?...\n", + "3658 Why are Americans expected to die sooner than ...\n", + "9721 Comments \\nFamous techno musician Moby tore in...\n", + "10001 Behind the headlines - conspiracies, cover-ups...\n", + "8920 Massachusetts +11% New York +10% \\nWhen home p...\n", + "8488 Ivana Says Young Donald Trump Was A Cry-Baby –...\n", + "8474 Recipient Email => \\nEnemies of the United S...\n", + "5789 Theresa May refuses to withdraw support for Sa...\n", + "1324 Even as Hillary Clinton has stepped up her rhe...\n", + "1744 Sally Kohn is an activist, columnist and telev...\n", + "3887 George Washington was the first President of t...\n", + " ... \n", + "643 Is anyone qualified to judge The Donald?\\n\\nDo...\n", + "6795 [Graphic: Calais street scene by Harriet Paint...\n", + "10444 By wmw_admin on October 30, 2016 By Timothy Fi...\n", + "8696 Unprecedented Surge In Election Fraud Incident...\n", + "5103 The Democratic National Committee is offering ...\n", + "8018 Comments \\nFOX News star Megyn Kelly has final...\n", + "2896 President Obama sent a draft Authorization for...\n", + "9188 Heseltine strangled dog as part of Thatcher ca...\n", + "6 It's been a big week for abortion news.\\n\\nCar...\n", + "9778 20 Foods That Naturally Unclog Arteries and Pr...\n", + "10062 FBI Releases Files on Bill Clinton's Cash for ...\n", + "8556 NTEB Ads Privacy Policy Black Americans Going ...\n", + "9757 10-27-1 6 The first Bill and Hillary Clinton c...\n", + "2524 President Obama's executive action preventing ...\n", + "8995 Rights? In The New America You Don’t Get Any R...\n", + "8602 By Amanda Froelich For a long time, green juic...\n", + "2886 W hen trying to explain the current unrest in ...\n", + "3456 WASHINGTON, June 21 (Reuters) - Tensions are b...\n", + "6564 This is how it works in the Clinton Cabal...or...\n", + "8406 posted by Eddie Angelina Jolie’s father, Jon V...\n", + "1151 As the Democratic presidential contest reaches...\n", + "3233 With the Department of Homeland Security’s fun...\n", + "4601 Enough Is Enough\\n\\nIf we really want to do so...\n", + "305 Top Dems want White House to call off Part B d...\n", + "842 Texas Sen. Ted Cruz’s popularity among Republi...\n", + "7085 Email \\nNot all invasions are hot — not all in...\n", + "530 President Obama's 2016 budget seeks higher spe...\n", + "5766 \\nThe decision of FBI Director Comey to go p...\n", + "976 Republican presidential candidate Ted Cruz is ...\n", + "2241 The simmering dispute over media access to Hil...\n", + "Name: text, Length: 4434, dtype: object" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "count_vectorizer = CountVectorizer(stop_words='english')\n", + "count_train = count_vectorizer.fit_transform(X)\n", + "count_test = count_vectorizer.transform(Xt)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)\n", + "tfidf_train = tfidf_vectorizer.fit_transform(X)\n", + "tfidf_test = tfidf_vectorizer.transform(Xt)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['00',\n", + " '000',\n", + " '0000',\n", + " '00000031',\n", + " '000035',\n", + " '00006',\n", + " '0001',\n", + " '0001pt',\n", + " '0002',\n", + " '000billion']\n", + "['حلب', 'عربي', 'عن', 'لم', 'ما', 'محاولات', 'من', 'هذا', 'والمرضى', 'ยงade']\n" + ] + } + ], + "source": [ + "pp(count_vectorizer.get_feature_names()[0:10])\n", + "pp(count_vectorizer.get_feature_names()[-10:])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['00',\n", + " '000',\n", + " '0000',\n", + " '00000031',\n", + " '000035',\n", + " '00006',\n", + " '0001',\n", + " '0001pt',\n", + " '0002',\n", + " '000billion']\n", + "['حلب', 'عربي', 'عن', 'لم', 'ما', 'محاولات', 'من', 'هذا', 'والمرضى', 'ยงade']\n" + ] + } + ], + "source": [ + "pp(tfidf_vectorizer.get_feature_names()[:10])\n", + "pp(tfidf_vectorizer.get_feature_names()[-10:])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "#count_df = pd.DataFrame(count_train.A, columns=count_vectorizer.get_feature_names())\n", + "#tfidf_df = pd.DataFrame(count_train.A, columns=tfidf_vectorizer.get_feature_names())\n", + "#diff = set(count_df.columns) - set(tfidf_df.columns)\n", + "#pp(count_df.equals(tfidf_df))" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'score: 0.8527091004734351'\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "074a58de5e904298a7c212800fdc812b", + "version_major": 2, + "version_minor": 0 + }, + "text/html": [ + "

Failed to display Jupyter Widget of type FigureCanvasNbAgg.

\n", + "

\n", + " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", + " that the widgets JavaScript is still loading. If this message persists, it\n", + " likely means that the widgets JavaScript library is either not installed or\n", + " not enabled. See the Jupyter\n", + " Widgets Documentation for setup instructions.\n", + "

\n", + "

\n", + " If you're reading this message in another frontend (for example, a static\n", + " rendering on GitHub or NBViewer),\n", + " it may mean that your frontend doesn't currently support widgets.\n", + "

\n" + ], + "text/plain": [ + "FigureCanvasNbAgg()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Confusion matrix, without normalization\n" + ] + } + ], + "source": [ + "clf = MultinomialNB()\n", + "clf.fit(tfidf_train, y)\n", + "pred = clf.predict(tfidf_test)\n", + "score = metrics.accuracy_score(yt, pred)\n", + "pp(\"score: \" + str(score))\n", + "cm = metrics.confusion_matrix(yt, pred, labels=[\"FAKE\", \"REAL\"])\n", + "plot_confusion_matrix(cm, classes=[\"FAKE\", \"REAL\"], title= \"tfidf\")" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "cut = 0.7\n", - "\n", - "y = df_1.label\n", - "df_1 = df_1.drop('label', axis=1)\n" - ] + "source": [] } ], "metadata": {