<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pius Kariuki</title>
    <description>The latest articles on DEV Community by Pius Kariuki (@piushopkins).</description>
    <link>https://dev.to/piushopkins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F850962%2F91f912af-c57d-4c3a-ac3d-625f748863b2.png</url>
      <title>DEV Community: Pius Kariuki</title>
      <link>https://dev.to/piushopkins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/piushopkins"/>
    <language>en</language>
    <item>
      <title>Detecting Fake News with Python and Machine Learning</title>
      <dc:creator>Pius Kariuki</dc:creator>
      <pubDate>Sun, 01 May 2022 10:32:10 +0000</pubDate>
      <link>https://dev.to/piushopkins/detecting-fake-news-with-python-and-machine-learning-4mh5</link>
      <guid>https://dev.to/piushopkins/detecting-fake-news-with-python-and-machine-learning-4mh5</guid>
      <description>&lt;p&gt;Are all news real? Should we trust all the news presented to us? Apparently, No! Fake news exists and they tend to become viral reducing the impact of real news.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fake news is one of the most significant new disturbing trend that must be resolved, otherwise the internet cannot truly serve and benefit humanity ~ Tim Berners-Lee&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then how can you detect and fight fake news? Python and Machine learning is the way to go. By practicing this python module for detecting fake news, you will easily distinguish real from fake news.&lt;/p&gt;

&lt;h1&gt;
  
  
  Fake News:
&lt;/h1&gt;

&lt;p&gt;Fake news refers to anything from intentional fabrication reporting on a controversial topic or misleading information presented as news. Its generally spread through social media and other online media. Fake news aims at damaging the reputation of a person or an entity by imposing certain ideas with harmful intent. Types of fake news may include satire or parody, false connection, misleading content, false content, impostor content, manipulated content and fabricated content. such news may contain false claims and may end up being virialized by algorithms. &lt;/p&gt;

&lt;h2&gt;
  
  
  TfidfVectorizer:
&lt;/h2&gt;

&lt;p&gt;The TfidfVectorizer converts a collection of raw documents into a matrix of TF-IDF features.&lt;br&gt;
&lt;strong&gt;Term Frequency(TF):&lt;/strong&gt; TF is the number of times a word appears in a document. a higher value suggests a term appears more often than others. Presumably, the documents are a good match when the term is part of the search.&lt;br&gt;
&lt;strong&gt;Inverse Document Frequency(IDF):&lt;/strong&gt; IDF is the measure of how significant a term is in the entire corpus.&lt;/p&gt;
&lt;h3&gt;
  
  
  PassiveAggressiveClassifier:
&lt;/h3&gt;

&lt;p&gt;PassiveAggresssive Algorithms are online learning algorithms. The algorithms remain passive for a correct classification outcome and turn aggressive in case of miscalculation, updating and adjusting. It does not converge. Its purpose is to make updates correcting losses, causing little change in the norm of the weight vector.&lt;/p&gt;
&lt;h4&gt;
  
  
  Detecting Fake News with Python
&lt;/h4&gt;

&lt;p&gt;This Python project module for detecting fake and real news makes use of sklearn. We will build a TfidfVectorizer on our dataset, initialize a PassiveAggressive Classifier and fit the model to accurately classify a piece of news as either real or fake.&lt;br&gt;
&lt;strong&gt;Project Prerequisites:&lt;/strong&gt;&lt;br&gt;
You will need the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fake News Dataset 
The data set has a filename.csv. The dataset that we will use will call it news.csv. Dataset has a shape and columns identifying the news, text, title, and labels denoting whether it's fake or real.&lt;/li&gt;
&lt;li&gt;The Libraries 
To perform this classification, you will need the basic data science pack. You will need to install the libraries; sklearn, numpy, and pandas plus some more libraries like transformers and Pycarets.
For us we'll install the following libraries with pip: 
&lt;code&gt;pip install numpy pandas sklearn&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Jupyter Lab:
You will need to install jupyter Lab to run your codes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;STEPS FOR DETECTING FAKE NEWS WITH PYTHON&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below are steps to help you detect fake news using Python:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the Libraries:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
import pandas as pd
import itertools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score, confusion_matrix

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Load the data:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's read the data into a DataFrame, and get the shape and columns. Next, get the labels from the DataFrame and finally split the dataset into training and testing sets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
#Read the data
df=pd.read_csv

#Get the shape and head
df.shape 
df.head()

#Get the labels
labels=df.labels
labels.head()

#Split the dataset
x_train,x_test,y_train,y_test=train_test_split(df['text'], labels, test_size=0.2, random_state=7)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initializing TfidfVectorizer:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll initialize TfidfVecorizer with stop words and a maximum document frequency of o.7. Stop words are simply the useless words in a language or the most common words that are to be filtered out before processing the natural language data. &lt;br&gt;
TfidfVectorizer turns a collection of raw data documents into a matrix of TF-IDF features.&lt;br&gt;
We have to fit and transform the vectorizer on the train set and transform it on the test set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Initialize a TfidfVectorizer
tfidf_vectorizer=TfidfVectorizer(stop_words='english', max_df=0.7)

#Fit and transform train set, transform test set
tfidf_train=tfidf_vectorizer.fit_transform(x_train) 
tfidf_test=tfidf_vectorizer.transform(x_test)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Finally initialize a PasiveAggressiveClassifier:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will initialize the PasiveAggressiveClassifier and then fit it on the tfidf_train and y_train.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Initialize a PassiveAggressiveClassifier
pac=PassiveAggressiveClassifier(max_iter=50)
pac.fit(tfidf_train,y_train)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will predict the test set from TfidfVectorizer and calculate the accuracy of the project module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Predict on the test set and calculate accuracy
y_pred=pac.predict(tfidf_test)
score=accuracy_score(y_test,y_pred)
print(f'Accuracy: {round(score*100,2)}%

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we can print out the confusion matrix to gain insight into the number of Real and False. after which we can have our results.&lt;br&gt;
&lt;code&gt;confusion_matrix(y_test,y_pred, labels=['FAKE','REAL'])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion.&lt;/strong&gt;&lt;br&gt;
To this point, we've effectively learned how to easily detect fake news with Python. We learned how to load the Fake news dataset, initialize and implement TfidfVectorizer and PasiveAggressiveClassifier to fit our model theoretically. I hope once you get to code you will figure it out.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

&lt;p&gt;Bibliography:&lt;br&gt;
Dataflair team: &lt;a href="https://data-flair.training/blogs/advanced-python-project-detecting-fake-news/"&gt;https://data-flair.training/blogs/advanced-python-project-detecting-fake-news/&lt;/a&gt; &lt;br&gt;
Fake News Wikipedia.&lt;a href="https://en.wikipedia.org/wiki/Fake_news"&gt;https://en.wikipedia.org/wiki/Fake_news&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>news</category>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python 101: The Ultimate Python Tutorial For Beginners</title>
      <dc:creator>Pius Kariuki</dc:creator>
      <pubDate>Sun, 24 Apr 2022 02:57:15 +0000</pubDate>
      <link>https://dev.to/piushopkins/python-101-the-ultimate-python-tutorial-for-beginners-p3o</link>
      <guid>https://dev.to/piushopkins/python-101-the-ultimate-python-tutorial-for-beginners-p3o</guid>
      <description>&lt;h1&gt;
  
  
  INTRODUCTION
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt; is a high-level, general purpose programming language. Designed philosophically emphasizes code readability with the use of significant indentation. Its language construct and object oriented approach aims to help programmers write a clear and logical codes. Python program have an extension &lt;strong&gt;.py&lt;/strong&gt;. It is a general purpose programming language due to range of application such as: Machine Learning &amp;amp; AI, automation, image processing, scientific computing, web development and data base to mention a few. &lt;br&gt;
Python has developed  a lot from the first time it was created. It was conceived in late 1990s by &lt;strong&gt;Guido Van Rossum&lt;/strong&gt; as a successor to to ABC programming language. It was first released in 1991 as python 0.9.0, then python 2.0 introducing features like; list comprehension, garbage collection, reference counting and unicode support. Later python 3.0 was released. Python has remained one of the most popular programming language to date as: It is very flexible, increases productivity, supportive community and it is easy to learn. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have this hope that there is a better way. Higher-level tools that actually let you see the structure of the software more clearly will be of tremendous value, Guido Van Rossum&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  PYTHON ENVIRONMENT
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Setting up Python&lt;/strong&gt;&lt;br&gt;
Python comes with two main distribution that include: Python Software Foundation(PSF) and Anaconda Distribution.&lt;br&gt;
as with any programming language the first step is to learn how to install it.&lt;br&gt;
&lt;strong&gt;Python Installation For windows&lt;/strong&gt;&lt;br&gt;
step 1: Open any  browser on your windows&lt;br&gt;
Step 2: Use the link; &lt;a href="https://www.python.org"&gt;https://www.python.org&lt;/a&gt;&lt;br&gt;
step 3: click for the latest Python3 release&lt;br&gt;
step 4: Download and Run the executable &lt;strong&gt;.exe&lt;/strong&gt; installer&lt;br&gt;
step 5: Verify python was Installed on windows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O5UVRHJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajd3ywgeg4f3qmxce2op.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O5UVRHJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajd3ywgeg4f3qmxce2op.jpg" alt="Image2" width="600" height="124"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  PYTHON IDEs AND CODE EDITORS
&lt;/h3&gt;

&lt;p&gt;Code editors are light weight tools used to write and edit code. However, complex and larger programs need testing and debugging. IDES compared to editors come in handy in such instances. They help you understand your code better than a text editor making it easier to code by allowing you to write, test and debug at the same time. Examples of such environments: Sublime Text 3, Atom, Thonny, Pycharm, Visual Studio Code and vim.&lt;br&gt;
Download any and run the installer like any other program and you are ready to code.&lt;br&gt;
&lt;strong&gt;LETS CODE&lt;/strong&gt;&lt;br&gt;
We will write "Hello, world!" program.&lt;br&gt;
To print out something in python we use the &lt;em&gt;print()&lt;/em&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Running our first program

print("Hello, world!")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before running the code, we need to save it. Save as &lt;em&gt;Hello.py&lt;/em&gt; the .py extension tells our interpretor that its running a python code. After saving run the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Output:
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you get this? If yes, Kudos! You have executed your first program.&lt;/p&gt;

&lt;h4&gt;
  
  
  FUNDAMENTALS OF PHYTHON
&lt;/h4&gt;

&lt;p&gt;This are some of the python basic that a beginner ought to understand.&lt;/p&gt;

&lt;p&gt;Syntax &amp;amp; Semantics:&lt;/p&gt;

&lt;p&gt;Python is meant to be an easily readable language. Its formatting usually uncluttered and often uses English keywords where other languages use punctuations. Such keywords include; and, as, assert, async, await, break, class, def etc.it does not use curly brackets to delimit blocks and semicolons after statements.&lt;/p&gt;

&lt;p&gt;Variables:&lt;/p&gt;

&lt;p&gt;A variable is used to store data(values) in programming.&lt;br&gt;
For Example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 10
b = 0.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a and b are variables with values 10 and 0.5 respectively.&lt;br&gt;
&lt;strong&gt;Python Literal&lt;/strong&gt; is a raw data for representing fixed values example, 10 and 0.5 are literals.&lt;/p&gt;

&lt;p&gt;Data Types:&lt;/p&gt;

&lt;p&gt;Data types make Python programming easier. A data type is a set of values and a set of operations defined on data. Data types in python:&lt;br&gt;
Numeric Types: int, float, complex&lt;br&gt;
Sequence Types: list, tuples,rangs&lt;br&gt;
Set types: frozen, set&lt;br&gt;
Mapping Types: dict.&lt;br&gt;
Boolean Type: bool&lt;/p&gt;

&lt;p&gt;Python Comments:&lt;/p&gt;

&lt;p&gt;A comment is used to describe what's going on inside a program. they make the code understandable. The Python interpreter ignores such comments. &lt;strong&gt;#&lt;/strong&gt;symbol is used to write a single-line comment.&lt;br&gt;
Other Types comments: &lt;br&gt;
-&amp;gt;Multi-line comment&lt;br&gt;
-&amp;gt;doc-staring comments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#This is a comment, not executed
a = "Hello World" # a contain "hello world" A string literal 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OPERATORS:&lt;/p&gt;

&lt;p&gt;An operator is a special symbol that carries out arithmetic or Logical operations, such multiplication and addition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 10
b = 20
sum = a + b
print(sum)
# Output: 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Types of operators include:&lt;br&gt;
1: Arithmetic Operators: Perform mathematics operations.&lt;br&gt;
Examples of arithmetic operators: + operator used to sum operands, * operator used to multiply operands, - operator used to subtract operands, / operator used to divide left operand by the right, // operator used to find the quotient, % operator used to used to give the reminder of the division,** operator raises the right operand to the power of the left. Example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 2
b = 5 
result =  a**b # 2 raised to the power of 2
print(results)

#output: 32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;= assignment operator.&lt;br&gt;
2: Logic operators: Logic AND, Logic OR and Logic NOT.&lt;br&gt;
3: Comparison Operators: Used to compare two values.&lt;/p&gt;

&lt;p&gt;Indentation:&lt;/p&gt;

&lt;p&gt;Python relies on indentation, white spaces, to define scope in a code. Other programming languages often use curly-brackets for this purpose.&lt;/p&gt;

&lt;p&gt;Loop:&lt;/p&gt;

&lt;p&gt;loops are statements used to iterate over an object. Types of loops include: while loops and for loops.&lt;br&gt;
A for loop used to iterate through  collection of items.&lt;br&gt;
For Example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;languages = ['Python', 'C++', 'PHP']
for item in languages:
 print(item)

#output: 
Python
C++
PHP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While loop allows you to execute a set of instruction untill the given condition is true.&lt;br&gt;
Example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 3
n = 1
while n &amp;lt;= i:
  print("Python is easy")
  n = n + 1 # n is increased by 1

# output:
Python is easy
Python is easy
Python is easy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Constant:&lt;br&gt;
Constant is a type of variable holds values, whose values cannot be changed. Rarely do we use constants in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION.
&lt;/h2&gt;

&lt;p&gt;This tutorial is meant to help you familiarize with Python programming language. We started with knowing what Python is, installation process, writing our first program and finally some basics fundamentals of Python. There is more than that in Python programming.&lt;br&gt;
Thanks for reading.  &lt;/p&gt;

&lt;p&gt;Bibliography:&lt;br&gt;
Guido Van Rossum.(n.d.).AZQuotes.com. Retrieved April 23, 2022, from AZQuotes.com Web site: &lt;a href="https://www.azquotes.com/author/46455-Guido_van_Rossum"&gt;https://www.azquotes.com/author/46455-Guido_van_Rossum&lt;/a&gt; &lt;br&gt;
Python(programming language) - Wikipedia: &lt;a href="https://en.m.wikipedia.org"&gt;https://en.m.wikipedia.org&lt;/a&gt;&lt;br&gt;
Python.Net Tutorial For beginners.&lt;br&gt;
Zen of Python: &lt;a href="https://peps.python.org"&gt;https://peps.python.org&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
