You are on page 1of 93

The Big Book of Python Scripts

Ryan Wells
wellsr.com
Table of Contents
Contents
Introduction: Getting Started with Python ........................................................................................................... 5
Python Data Types ............................................................................................................................................ 6
Python Data Structures...................................................................................................................................... 7
Using Python Functions and Defining New Functions ........................................................................................ 8
Creating and Using Python Class Objects and Iterators .................................................................................... 9
Python I/O: Input and Output Examples and Tools .......................................................................................... 10
Python String Operations and String Formatting .............................................................................................. 11
Pandas DataFrame from Dictionary, List, and List of Dicts .............................................................................. 12
Pandas read_csv Examples for Importing Files ............................................................................................... 13
Python with Pandas: Comparing Two DataFrames .......................................................................................... 14
Python with Pandas: Convert String to Float and other Numeric Types ........................................................... 15
Using Pandas Concat to Merge DataFrames................................................................................................... 16
Create Histograms from Pandas DataFrames ................................................................................................. 17
Create Pandas Density Plots from DataFrames............................................................................................... 18
Create Pandas Boxplots with DataFrames ...................................................................................................... 19
Python Comprehension and Generator Expressions ....................................................................................... 20
Reading CSV Files with the Python CSV Module ............................................................................................ 21
Writing CSV Files with the Python CSV Module .............................................................................................. 23
How to Add and Read Database Data with Python sqlite3............................................................................... 24
Copy Data from Excel to an sqlite3 Database with Python .............................................................................. 25
Adapting and Converting SQLite Data Types for Python ................................................................................. 26
Understanding Python Regex Matching........................................................................................................... 27
Using Python Regex Groups to Capture Substrings ........................................................................................ 28
Grouping DataFrame Data with the Pandas groupby Operation ...................................................................... 29
Using Command Line Arguments with Python sys.argv ................................................................................... 30
Using Python stdin, stdout, and stderr with the sys Module ............................................................................. 31
Create a Python Voronoi Diagram with GeoPandas and Geoplot .................................................................... 32
Creating Interactive Python Choropleth Maps with Plotly ................................................................................. 33
Introduction to Seaborn Plots for Python Data Visualization ............................................................................ 34
Seaborn Barplot Tutorial for Python ................................................................................................................. 35
Seaborn Line Plot Data Visualization ............................................................................................................... 36
How to Make Seaborn Boxplots in Python ....................................................................................................... 37
The Basics of NumPy Arrays and How to Use Them ....................................................................................... 38
Page 2 of 93 wellsr.com
Table of Contents
Transposing a Matrix with Numpy .................................................................................................................... 39
Python Sentiment Analysis with scikit-learn ..................................................................................................... 40
Making Seaborn Scatter Plots with sns.scatterplot .......................................................................................... 42
Solving a System of Linear Equations with Python's NumPy ........................................................................... 43
Using Python TensorFlow 2.0 for Classification Tasks..................................................................................... 44
Using Python TensorFlow 2.0 for Regression Tasks ....................................................................................... 45
Seaborn Histogram DistPlot Tutorial for Python ............................................................................................... 47
Python Machine Learning Examples with scikit-learn ...................................................................................... 48
Joining DataFrames with Python Pandas Join ................................................................................................. 49
Using Pandas Apply on Dataframes and Series .............................................................................................. 50
Python Default Arguments and Function Overloading ..................................................................................... 51
Python Lemmatization with NLTK .................................................................................................................... 52
How to Perform Python NLTK Tokenization..................................................................................................... 53
Remove Stop Words with Python NLTK .......................................................................................................... 54
Stemming in Python with NLTK Examples ....................................................................................................... 55
Python Named Entity Recognition with NLTK & spaCy .................................................................................... 56
Understanding Inheritance in Python with Examples ....................................................................................... 57
Performing CRUD operations with Python and MySQL ................................................................................... 58
Web Scraping with Python Scrapy ................................................................................................................... 60
Python Speech Recognition and Audio Transcription ...................................................................................... 61
14 Examples To Help You Understand Python List Slicing .............................................................................. 62
Zipping and Unzipping Files and Folders with Python ...................................................................................... 63
Sending Emails through Gmail with Python ..................................................................................................... 64
Read Gmail Emails With Python ...................................................................................................................... 65
Python Machine Learning for Spam Email Detection ....................................................................................... 66
How to Extend Functions with Python Decorators ........................................................................................... 68
Python unittest Examples for Testing Python Code ......................................................................................... 69
Multithreading in Python: Running Functions in Parallel .................................................................................. 70
3 Ways to Calculate Python Execution Time ................................................................................................... 71
Understanding Python if __name__ == '__main__' statements........................................................................ 72
How to Read PDF Files with Python using PyPDF2 ........................................................................................ 73
How to use the Python pdb debugger .............................................................................................................. 74
Python Face Detection for Beginners with OpenCV ......................................................................................... 75
Convert Image to String with Python Pytesseract OCR ................................................................................... 76
Python Image Manipulation with Pillow Library ................................................................................................ 77
Page 3 of 93 wellsr.com
Table of Contents
Drawing Shapes on Images with the Python OpenCV Library ......................................................................... 78
Python chdir and getcwd to Set Working Directory .......................................................................................... 79
List Files in a Folder With Python OS listdir and walk ...................................................................................... 80
How to Open Files with Python OS .................................................................................................................. 81
Python OS Delete Files and Create Folders .................................................................................................... 82
Logging Info and Errors with the Python Logging Module ................................................................................ 83
Random Number Generation in Python with Random Module ......................................................................... 84
Python Delete All Files in a Folder ................................................................................................................... 85
Python Map, Filter and Reduce for Lambda Functions .................................................................................... 86
Sorting items in Python using Sort and Sorted Methods .................................................................................. 87
NumPy Eigenvalues and Eigenvectors with Python ......................................................................................... 88
Outer Product of Two Vectors or Matrices with Python NumPy ....................................................................... 89
Python k-means clustering with scikit-learn ..................................................................................................... 90
Asynchronous Programming in Python with Asyncio ....................................................................................... 91
Python Agglomerative Clustering with sklearn ................................................................................................. 92
Python PCA Examples with sklearn ................................................................................................................. 93

Page 4 of 93 wellsr.com
Introduction: Getting Started with Python
More Info

Introduction: Getting Started with Python


Getting started with python is easy with this guide, which provides an introduction to Python and
useful IDEs such and IDLE and Anaconda. This guide will also go over the basics of Python
exceptions and basic commands.
print("Hello World") # Python 3

while True: # This is an infinite loop. We will cover loops in a future tutorial
x = 0
# Ctrl + c
> KeyboardInterrupt

Page 5 of 93 wellsr.com
Python Data Types
More Info

Python Data Types


This introduction to Python Data Types will teach you everything you need to know about working
with different data types in Python, including integers, floats, complex, booleans, none, and strings.
x = 1 #Defining x to be the integer 1
print(type(x)) #Verifying x to be of type integer
> <class 'int'>
y = x + x # Adding an integer to an integer creates an integer
print(type(y))
> <class 'int'>

print(int("409"))
> 409

x = ("Student " + "\t" + "Country " + "\t" + "GDP (billion USD)" + "\t" "Population" + "\t" "In Africa?" +
"\n"
+"Mary " + "\t" + "Luxembourg" + "\t" + "64.2 " + "\t" "602005 " + "\t" "False" + "\n"
+"Matthew " + "\t" + "Eritrea " + "\t" + "6.856 " + "\t" "4954645 " + "\t" "True" + "\n"
+"Marie " + "\t" + "None " + "\t" + "None " + "\t" "None " + "\t" "None" + "\n"
+"Manuel " + "\t" + "Lesotho " + "\t" + "2.721 " + "\t" "2203821 " + "\t" "True" + "\n"
+"Malala " + "\t" + "Poland " + "\t" + "614.190 " + "\t" "38433600 " + "\t" "False" +
"\n" )
print(x)
> Student Country GDP (billion USD) Population In Africa?
> Mary Luxembourg 64.2 602005 False
> Matthew Eritrea 6.856 4954645 True
> Marie None None None None
> Manuel Lesotho 2.721 2203821 True
> Malala Poland 614.190 38433600 False

Page 6 of 93 wellsr.com
Python Data Structures
More Info

Python Data Structures


Tutorial on Python's data structures, including lists, dictionaries, sets, and tuples. Includes list and
dictionary operations, such as len(), sum(), list(), and sorted().
# Python Lists
[1, 2, 3, 4, 5]
[1.0, 1.1, 1.2, 1.3,]
["a", "b", "c"]
[True, False]
[None, None]
[1, 1.0, "a", True, None]
x = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
print(x[0]) #Note Python begins indexing at 0
> a
print(x[8]) #Last element will be length - 1
> i

# Python Dictionaries
d = {"a":1, "b":2, "c":3, "d":4}
d
> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
type(d)
> dict

d["c"]
> 3

# Python Sets
s = {1, 2.3, "apple", False, None}
s
> {1, 2.3, False, None, 'apple'}
type(s)
> set

s = {1, 2.3, "apple", False, None}


"apple" in s
> True
"cherry" in s
> False

# Python Tuples
t = 1, 2, 3, 4, 5, 6
t
> (1, 2, 3, 4, 5, 6)
type(t)
> tuple

t = 1, 2, 3, 4, 5, 6
t[2]
> 3
t[2:5]
> (3, 4, 5)

Page 7 of 93 wellsr.com
Using Python Functions and Defining New Functions
More Info

Using Python Functions and Defining New Functions


This tutorial covers Python functions, user-defined functions, the map function, lambda functions,
functions versus methods, and function objects.
[y for y in map(lambda x: x**2, [x for x in range(1,11)])]

Page 8 of 93 wellsr.com
Creating and Using Python Class Objects and Iterators
More Info

Creating and Using Python Class Objects and Iterators


This tutorial discusses Python class objects, class constructors, Python methods, underscore naming
convention, attribute name mangling, and class object inheritance.
class geographyStudent(Student):
def __init__(self, input):
self.name = input["Name"]
self.homework = input["HW"]
self.project1 = input["P1"]
self.midterm = input["M"]
self.project2 = input["P2"]
self.final = input["F"]
def finalGrade(self):
self.grade = (self.homework*0.2
+ self.project1*0.1
+ self.midterm*0.2
+ self.project2*0.1
+ self.final*0.4)
return(self.grade)
def PassFail(self):
grade = self.finalGrade()
if grade >= 65.0:
return("Pass")
else:
return("Fail")
students = []
for s in geographyClass: # Assumes the list of dictionaries in the above example exists
students.append(geographyStudent(s))
print(students[1].finalGrade())
> 76.5
print(students[1].PassFail())
> Pass

Page 9 of 93 wellsr.com
Python I/O: Input and Output Examples and Tools
More Info

Python I/O: Input and Output Examples and Tools


This tutorial describes Python input and output (I/O) from the terminal, using the os module, manually
importing text files, using with statements, and the csv module.
import csv
with open("RawData.csv", mode="r", newline="") as x:
with open("RawData-Spaced.txt", mode="w", newline="") as y:
reader = csv.reader(x)
writer = csv.writer(y, delimiter=" ")
for line in reader:
writer.writerows([line]) # Remember writerows expects a list of lists

Page 10 of 93 wellsr.com
Python String Operations and String Formatting
More Info

Python String Operations and String Formatting


This tutorial explains all the important Python string operations and Python string formatting methods.
We'll discuss operations on the strings themselves, and methods of formatting output strings.
n = 42
m = -22
s = "The secret number is {0}, not {1}".format(n, m) # Simple positional reference
print(s)
> The secret number is 42, not -22
s = "The secret number is {}, not {}".format(n, m) # Numbers are not needed for ordered input
print(s)
> The secret number is 42, not -22
s = "The secret number is {isthis}, not {isnot}".format(isthis=n, isnot=m) # Reference by name (no dictionary
needed)
print(s)
> The secret number is 42, not -22
s = "The secret number is {:+}, not {:+}".format(n, m) # Force use of signs
print(s)
> The secret number is +42, not -22

n = 42.2222222222222
m = -22.0
s = "The secret number is {:07.2f}, not {:07.2f}".format(n, m) # Pad with 0's, force length of 7, with 2
decimals of precision for floating point
print(s)
> The secret number is 0042.22, not -022.00
s = "The secret number is {:.2e}, not {:.2E}".format(n, m) # Use 2 decimals of precision, in two types of
scientific notation
print(s)
> The secret number is 4.22e+01, not -2.20E+01

Page 11 of 93 wellsr.com
Pandas DataFrame from Dictionary, List, and List of Dicts
More Info

Pandas DataFrame from Dictionary, List, and List of Dicts


This tutorial will show you how to convert dictionaries and lists to Pandas DataFrames. We'll also
explain how to create a Pandas DataFrame from a list of dicts and a list of lists.
NewDict = {} # Initialize a new dictionary
for listItem in ListDictOne:
for key, value in listItem.items(): # Loop through all dictionary elements in the list
if key in list(NewDict): # if the key already exists, append to new
for entry in value:
NewDict[key].append(entry)
else: # if it's a new key, simply add to the new dictionary
NewDict[key] = value
df = pd.DataFrame.from_dict(NewDict) # Finally, create the DataFrame from the dictionary
print(df)
> Column A Column B Column C
> 0 1 4 7
> 1 2 5 8
> 2 3 6 9

Page 12 of 93 wellsr.com
Pandas read_csv Examples for Importing Files
More Info

Pandas read_csv Examples for Importing Files


The Pandas read_csv function lets you import data from CSV and plain-text files into DataFrames.
This tutorial provides several Pandas read_csv examples to show you how.
import pandas as pd
df = pd.read_csv('Sample.txt', index_col=0, delim_whitespace=True, quotechar="\"")
print(df)
> Column A Column B Column C
> Row Name
> Row 1 1 2 3
> Row 2 4 5 6
> Row 3 7 8 9

Page 13 of 93 wellsr.com
Python with Pandas: Comparing Two DataFrames
More Info

Python with Pandas: Comparing Two DataFrames


This tutorial explains how to use Pandas to compare two DataFrames and identify their differences.
Marking differences between DataFrames is valuable when analyzing data in Python.
import pandas as pd # Make sure import aliases are consistent
def dfDiff(oldFrame, newFrame):
dfBool = (oldFrame != newFrame).stack()
diff = pd.concat([oldFrame.stack()[dfBool],
newFrame.stack()[dfBool]], axis=1)
diff.columns=["Old", "New"]
return diff
print(dfDiff(grades1, grades2))
> Old New
> StudentID
> 5540 Midterm 90 92
> 6817 Homework 65 66
> Final 89 88

Page 14 of 93 wellsr.com
Python with Pandas: Convert String to Float and other
Numeric Types
More Info

Python with Pandas: Convert String to Float and other Numeric Types
This tutorial will show you how to convert Pandas DataFrame strings into floats or ints. Converting
Pandas string data to numeric types is required before performing numeric calculations.
import pandas as pd
import numpy as np # To use the int64 dtype, we will need to import numpy
grades["StudentID"] = grades["StudentID"].astype(dtype=np.int64)
grades.info()
> <class 'pandas.core.frame.DataFrame'>
> RangeIndex: 4 entries, 0 to 3
> Data columns (total 5 columns):
> StudentID 4 non-null int64
> Homework 4 non-null object
> Midterm 4 non-null object
> Project 4 non-null object
> Final 4 non-null object
> dtypes: int64(1), object(4)
> memory usage: 240.0+ bytes

Page 15 of 93 wellsr.com
Using Pandas Concat to Merge DataFrames
More Info

Using Pandas Concat to Merge DataFrames


Learn how the Pandas Concat function is used to merge dataframes. The Pandas Concat function
can merge dataframes by columns and rows. This Python tutorial contains examples of both.
hdf = pd.concat([df3, df2])
print(hdf)
> A B C D E F
> Index
> 1 NaN NaN NaN D1 E1 F1
> 2 NaN NaN NaN D2 E2 F2
> 3 NaN NaN NaN D3 E3 F3
> 4 NaN NaN NaN D4 E4 F4
> 5 NaN NaN NaN D5 E5 F5
> 6 NaN NaN NaN D6 E6 F6
> 7 A7 B7 C7 NaN NaN NaN
> 8 A8 B8 C8 NaN NaN NaN
> 9 A9 B9 C9 NaN NaN NaN
> 10 A10 B10 C10 NaN NaN NaN
> 11 A11 B11 C11 NaN NaN NaN
> 12 A12 B12 C12 NaN NaN NaN

Page 16 of 93 wellsr.com
Create Histograms from Pandas DataFrames
More Info

Create Histograms from Pandas DataFrames


This tutorial will teach you how to create histogram plots from Pandas DataFrames. Histograms are
an important tool in data analysis and Python Pandas makes it easy.
df["Test_1"].plot.hist(normed=True)

Page 17 of 93 wellsr.com
Create Pandas Density Plots from DataFrames
More Info

Create Pandas Density Plots from DataFrames


This tutorial will teach you how to create density plots from Pandas DataFrames. Density plots, also
called Kernel Density Estimation (KDE) plots are an important tool in data analysis.
df["Test_1"].plot.kde()
df["Test_1"].plot.hist(normed=True) # Histogram will now be normalized

Page 18 of 93 wellsr.com
Create Pandas Boxplots with DataFrames
More Info

Create Pandas Boxplots with DataFrames


This tutorial will teach you how to make a Pandas boxplot from a DataFrame. Boxplots, or box-and-
whisker plots, help you approximate the distribution of your Pandas data.
df.boxplot(by="Type", column=["Test_1", "Test_2"])

Page 19 of 93 wellsr.com
Python Comprehension and Generator Expressions
More Info

Python Comprehension and Generator Expressions


Python comprehension expressions and generator expressions have a lot of similarities. This tutorial
will discuss both comprehension and generator expressions in Python and explain how to use them.
( i for i in (lambda x, y : range(x, y, 2))(3, 15) )

Page 20 of 93 wellsr.com
Reading CSV Files with the Python CSV Module
More Info

Reading CSV Files with the Python CSV Module


Learn how to use Python to read CSV files with this tutorial describing the Python CSV reader
function, the CSV DictReader class, and troubleshooting encoding errors.
import csv
import sys
import traceback

def handle_system_error(error):
""" Handler for I/O errors. """
print("System error: %s" % traceback.format_exc())

def handle_decoding_error(error):
""" Handler for decoding strings with UTF-8. """
print("Decoding error: %s" % traceback.format_exc())

def handle_reader_error(error, reader):


""" Handler for `csv.reader()` exceptions. """
print("CSV reader error at line %d: %s" %
(reader.line_num, traceback.format_exc()))

def handle_other_errors(error):
""" Catch-all handler. """
print("Extrema ratio.")
sys.exit(1)

def convert_row(row):
""" Convert a CSV record in a list of Python objects. """
return row

def finalize_rows(rows):
""" Perform some operations on rows before exiting. """
return rows

def process_csv_data(path: str):


""" Filtering exceptions from the `csv` module. """
rows = []

try:
with open(path, 'rt', encoding='utf-8') as src:
reader = csv.reader(src, strict=True)
for row in reader:
rows.append(convert_row(row))
except EnvironmentError as error:
handle_system_error(error)
except UnicodeDecodeError as error:
handle_decoding_error(error)
except csv.Error as error:
handle_reader_error(error, reader)
except:
handle_other_errors(error)
else:
rows = finalize_rows(rows)

return rows

Page 21 of 93 wellsr.com
Reading CSV Files with the Python CSV Module
More Info

def test_exception():
""" Raising different kind of exceptionss. """
# Wrong file path
process_csv_data('nowhere.csv')

# Decoding error
process_csv_data('decode.csv')

# Reader error
process_csv_data('faulty-sample.csv')

# Finally, we got it right


rows = process_csv_data('sample.csv')
print("%d records read from 'sample.csv'" % len(rows))

Page 22 of 93 wellsr.com
Writing CSV Files with the Python CSV Module
More Info

Writing CSV Files with the Python CSV Module


This tutorial explains how to write a CSV file using Python. It shows several Python CSV writing
examples, including how to export data from a database to a CSV file.
import sqlite3
import csv

def write_to_csv_file():
""" Write all album released after 2014 to a CSV file. """
with sqlite3.connect('sample.db3') as connection, open(
'albums.csv', 'w', encoding='utf-8', newline='') as dump:
cursor = connection.cursor()
writer = csv.writer(dump)

query = "SELECT band, album, year FROM symphonic WHERE year>2014;"


for record in cursor.execute(query).fetchall():
writer.writerow(record)

Page 23 of 93 wellsr.com
How to Add and Read Database Data with Python sqlite3
More Info

How to Add and Read Database Data with Python sqlite3


This tutorials describes the native SQLite data types, and explains the core features of the Python
sqlite3 module, including how to add data to and read data from a SQL Database.
def test_row_class():
""" Fetching data with the `sqlite3.Row` class. """
with sqlite3.connect('sample.db3') as conn:
conn.row_factory = sqlite3.Row

query = "SELECT * FROM symphonic;"


for row in conn.execute(query).fetchall():
print(", ".join((row['Band'], row[2], row['COUNTRY'])))

Page 24 of 93 wellsr.com
Copy Data from Excel to an sqlite3 Database with Python
More Info

Copy Data from Excel to an sqlite3 Database with Python


This tutorials describes how to read data from an Excel sheet and transfer the data to an SQLite
database using the pandas, xlrd and sqlite3 Python libraries.
import pandas
from exceldoc import *

def case_study_3():
""" Test for case study 3. """
with ExcelDocument('pivot.xls') as src:
table = pandas.pivot_table(
src['pivot-data'].data_frame(),
columns = 'month',
values = 'income',
index = 'seller',
aggfunc = {'income' : sum},
fill_value = 0 )
print(table)

Page 25 of 93 wellsr.com
Adapting and Converting SQLite Data Types for Python
More Info

Adapting and Converting SQLite Data Types for Python


This tutorial will explain all the methods the sqlite3 module provides for converting and adapting
Python data types into SQLite types, and the other way around.
import sqlite3

def test_date_by_colnames():
""" Match default date converters by column names. """
with sqlite3.connect(
':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
date, type_1, timestamp, type_2 = conn.execute(
'SELECT CURRENT_DATE AS "d [date]", typeof(CURRENT_DATE), ' \
'CURRENT_TIMESTAMP AS "ts [timestamp]", ' \
'typeof(CURRENT_TIMESTAMP);').fetchone()
print('current date is %s (from %s to %s)\n' \
'current timestamp is %s (from %s to %s)\n' \
% (date, type_1.upper(), type(date),
timestamp, type_2.upper(), type(timestamp)))

test_date_by_colnames()

Page 26 of 93 wellsr.com
Understanding Python Regex Matching
More Info

Understanding Python Regex Matching


Understanding Python Regex (concatenation, alternation, and repetition) and how to use the Python
re module to match strings and byte sequences to a regular expression.
import re

def test_nonsense_repetitions():
""" Nonsense uses of the repetition operator. """
# Omitting both parameters, but without comma
print(re.match('a{}' , 'a{}').group() == 'a{}')
# Non-numeric parameters for the repetition operator
print(re.match('a{x,y}' , 'a{x,y}').group() == 'a{x,y}')
# Space within the repetition operator
print(re.match('a{1, 2}', 'a{1, 2}').group() == 'a{1, 2}')
# Space are not allowed even in VERBOSE mode
print(re.match('a{1, 2}', 'a{1,2}',
re.VERBOSE).group() == 'a{1,2}')
# Lower bound is greater than the upper bound
try:
re.match('a{4,2}', 'aa').group()
except re.error as err:
print(err)

test_nonsense_repetitions()

Page 27 of 93 wellsr.com
Using Python Regex Groups to Capture Substrings
More Info

Using Python Regex Groups to Capture Substrings


This tutorial describes how to use Python Regex Groups to capture a substring or submatch, and it
describes how to refer to a group's substring inside a regex.
import re

def test_match_tags():
""" Matching HTML tags. """
sample = r'''
<b>Bold</b><i>Italics</i>
<mod>Mod</mod><em>Emphasis</em>
<h2>Level 2 Header</h2>
<code>Bad code match</cod>
<h1>Bad header match</h3>
'''

regex_noref = re.compile(r'''
<(?P<otag>\w+)> # opening tag
(?P<text>[^<]*) # contents
</(?P<ctag>\w+)> # closing tag
''', re.VERBOSE)

print('Without references:\notag ctag text')


for match in regex_noref.finditer(sample):
print('{otag:8}{ctag:8}{text:12}'.format(
**match.groupdict()))

regex = re.compile(r'''
<(?P<otag>\w+)> # opening tag
(?P<text>[^<]*) # contents
</(?P<ctag>(?P=otag))> # closing tag
''', re.VERBOSE)

print('\nUsing references:\notag ctag text')


for match in regex.finditer(sample):
print('{otag:8}{ctag:8}{text:12}'.format(
**match.groupdict()))

test_match_tags()

Page 28 of 93 wellsr.com
Grouping DataFrame Data with the Pandas groupby
Operation
More Info

Grouping DataFrame Data with the Pandas groupby Operation


Use the Python Pandas groupby operation to group and aggregate data in a DataFrame. The Pandas
groupby operation can group data by a single or multiple columns.
def Cumulate2(x):
return(sum(x) + 2)
TypeGroup.aggregate(Cumulate2)
> Test_1 Test_2 AVG_Grade
> Type
> 1 21505.564099 17745.965485 19625.764792
> 2 18510.150955 15100.758677 16805.454816
> 3 19414.184990 16081.269563 17747.727276

Page 29 of 93 wellsr.com
Using Command Line Arguments with Python sys.argv
More Info

Using Command Line Arguments with Python sys.argv


This tutorial describes how to use command line arguments passed to a Python script execution with
the sys module's argv list.
import sys
print(sys.argv[0]) # Now indexing at 0

Page 30 of 93 wellsr.com
Using Python stdin, stdout, and stderr with the sys Module
More Info

Using Python stdin, stdout, and stderr with the sys Module
This tutorial will explain how to use shell standard input (stdin), standard output (stdout), and standard
error (stderr) in Python programs using the sys module.
import sys
for line in sys.stdin:
print(line + "to stdout")
print(line + "to sterr", file=sys.stderr)

Page 31 of 93 wellsr.com
Create a Python Voronoi Diagram with GeoPandas and
Geoplot
More Info

Create a Python Voronoi Diagram with GeoPandas and Geoplot


This tutorial will teach you how to create a Python Voronoi Diagram over a map of the United States
using a Pandas DataFrame with GeoPandas and Geoplot.
# Render the plot with a base map
geoplot.polyplot(USA, # Base Map
ax=ax, # Axis attribute we created above
extent=USA.total_bounds, # Set plotting boundaries to base map boundaries
edgecolor='black', # Color of base map's edges
linewidth=1, # Width of base map's edge lines
zorder=1 # Plot base map edges above the voronoi regions
)

Page 32 of 93 wellsr.com
Creating Interactive Python Choropleth Maps with Plotly
More Info

Creating Interactive Python Choropleth Maps with Plotly


This tutorial will show you how to create an interactive choropleth map of US States using Python and
Plotly. A choropleth map is a geographic color heatmap.
import plotly.express as px # Be sure to import express
fig = px.choropleth(state_df, # Input Pandas DataFrame
locations="state", # DataFrame column with locations
color="value", # DataFrame column with color values
hover_name="state", # DataFrame column hover info
locationmode = 'USA-states') # Set to plot as US States
fig.update_layout(
title_text = 'State Rankings', # Create a Title
geo_scope='usa', # Plot only the USA instead of globe
)
fig.show() # Output the plot to the screen

Page 33 of 93 wellsr.com
Introduction to Seaborn Plots for Python Data Visualization
More Info

Introduction to Seaborn Plots for Python Data Visualization


This tutorial introduces the Python Seaborn library for data visualization and includes Seaborn plot
examples so you can see how it helps visualize Python data.
sns.pairplot(tips_dataset)

Page 34 of 93 wellsr.com
Seaborn Barplot Tutorial for Python
More Info

Seaborn Barplot Tutorial for Python


This tutorial explains how to use the Seaborn barplot function in Python, including how to make
grouped bar plots, bar plots with values and barplot titles.
sns.catplot(x="sex", y="survived", hue="class", col="alone", data=titanic_dataset, kind="bar");

Page 35 of 93 wellsr.com
Seaborn Line Plot Data Visualization
More Info

Seaborn Line Plot Data Visualization


This tutorial explains how to draw different line plots using the Python Seaborn library. It includes
examples for setting line plot labels, markers and more.
sns.lineplot(x='size',y='total_bill', hue = 'time', data=dataset, style ='time',
palette = 'hot', dashes= False, markers= ["o","<"])
plt.xlabel("Number of Persons", fontsize= 12)
plt.ylabel("Total Bill", fontsize= 12)
plt.title("Persons vs Bill", fontsize= 15)

Page 36 of 93 wellsr.com
How to Make Seaborn Boxplots in Python
More Info

How to Make Seaborn Boxplots in Python


This tutorial draws different Seaborn Boxplots using the Python Seaborn library. It includes examples
for editing the colors, columns and labels of a box plot.
sns.boxplot(x='alive', y='age', hue='sex', data=titanic_dataset, palette="Oranges")

plt.xlabel("Passenger Alive", fontsize= 12)


plt.ylabel("Passenger Age", fontsize= 12)
plt.title("Age vs Alive (Gender Included)", fontsize= 15)

Page 37 of 93 wellsr.com
The Basics of NumPy Arrays and How to Use Them
More Info

The Basics of NumPy Arrays and How to Use Them


This tutorial introduces the Python NumPy Library and explains how to use it to create arrays and
perform arithmetic and matrix operations on NumPy arrays.
nums = [[10,20,30], [40,50,60], [70,80,90]]
m1 = np.array(nums, dtype = 'int')
result = np.linalg.det(m1)
print(result)

Page 38 of 93 wellsr.com
Transposing a Matrix with Numpy
More Info

Transposing a Matrix with Numpy


This tutorial explains how to transpose a matrix using NumPy in Python and includes practical
examples illustrating when you might need to transpose a matrix.
import numpy as np

M2 = [[5, 24, 76, 19],


[6, 45, 19, 56],
[9, 62, 25, 82]]
print("Shape of the original matrix ", np.array(M2).shape)

M2T = np.transpose(M2)
print(f'Transposed Matrix:\n{M2T}')
print("Shape of the transposed matrix ", np.array(M2T).shape)

Page 39 of 93 wellsr.com
Python Sentiment Analysis with scikit-learn
More Info

Python Sentiment Analysis with scikit-learn


This tutorial performs sentiment analysis using Python's Scikit-Learn library for machine learning. We
use the sklearn library to analyze the sentiment of movie reviews.
import numpy as np
import re
import nltk
import pandas as pd
nltk.download('stopwords')
from nltk.corpus import stopwords
import matplotlib.pyplot as plt
%matplotlib inline

imdb_reviews = pd.read_csv(r"E:/Datasets/imdb_reviews.csv", encoding = "ISO-8859-1")


imdb_reviews.head()

plt.rcParams["figure.figsize"] = [10, 8]
imdb_reviews.Sentiment.value_counts().plot(kind='pie', autopct='%1.0f%%')

reviews = imdb_reviews["SentimentText"].tolist()
labels = imdb_reviews["Sentiment"].values

processed_reviews = []

for text in range(0, len(reviews)):


text = re.sub(r'\W', ' ', str(reviews[text]))

text = re.sub(r'\s+[a-zA-Z]\s+', ' ', text)

text = re.sub(r'\^[a-zA-Z]\s+', ' ', text)

text = re.sub(r'\s+', ' ', text, flags=re.I)

text = re.sub(r'^b\s+', '', text)

processed_reviews.append(text)

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(processed_reviews, labels, test_size=0.2,
random_state=0)

from sklearn.feature_extraction.text import TfidfVectorizer


vectorizer = TfidfVectorizer(max_features=2000, min_df=5, max_df=0.75,
stop_words=stopwords.words('english'))
X_train = vectorizer.fit_transform(X_train).toarray()
X_test = vectorizer.transform(X_test).toarray()

from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_estimators=200, random_state=42)


rfc.fit(X_train, y_train)
y_pred = rfc.predict(X_test)

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

print(confusion_matrix(y_test,y_pred))

Page 40 of 93 wellsr.com
Python Sentiment Analysis with scikit-learn
More Info
print(classification_report(y_test,y_pred))
print(accuracy_score(y_test, y_pred))

Page 41 of 93 wellsr.com
Making Seaborn Scatter Plots with sns.scatterplot
More Info

Making Seaborn Scatter Plots with sns.scatterplot


The tutorial explains how to make different scatter plots using the Python Seaborn library. Several
code examples demonstrate how to use sns.scatterplot.
sns.scatterplot(x="total_bill", y="tip", data=tips_dataset, hue = 'smoker', style = 'sex' , marker = 'o')
plt.xlabel("Total Bill", fontsize= 12)
plt.ylabel("Tip", fontsize= 12)
plt.title("Bill vs Tip", fontsize= 15)

Page 42 of 93 wellsr.com
Solving a System of Linear Equations with Python's NumPy
More Info

Solving a System of Linear Equations with Python's NumPy


This tutorial uses examples to explain how to solve a system of linear questions using Python's
NumPy library and its linalg.solve and linalg.inv methods.
A = np.array([[12, 6], [10, 10]])

B = np.array([102, 110])

X = np.linalg.solve(A,B)

print(X)

Page 43 of 93 wellsr.com
Using Python TensorFlow 2.0 for Classification Tasks
More Info

Using Python TensorFlow 2.0 for Classification Tasks


This tutorial develops an Iris plant classification tool to explain how to perform classification tasks
using Python's TensorFlow 2.0 library for deep learning.
import seaborn as sns
import pandas as pd
import numpy as np

from tensorflow.keras.layers import Input, Dense, Dropout, Activation


from tensorflow.keras.models import Model

dataset = sns.load_dataset('iris')
dataset.head()

X = dataset.drop(['species'], axis=1)
y = pd.get_dummies(dataset.species, prefix='output')

X.head()

y.head()

X = X.values
y = y.values

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=39)

from sklearn.preprocessing import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

input_1 = Input(shape=(X_train.shape[1],))
l1 = Dense(100, activation='relu')(input_1)
l2 = Dense(50, activation='relu')(l1)
l3 = Dense(25, activation='relu')(l2)
output_1 = Dense(y_train.shape[1], activation='softmax')(l3)

model = Model(inputs = input_1, outputs = output_1)


model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

print(model.summary())

history = model.fit(X_train, y_train, batch_size=4, epochs=20, verbose=1, validation_split=0.20)

score = model.evaluate(X_test, y_test, verbose=1)

print("Test Accuracy:", score[1])

Page 44 of 93 wellsr.com
Using Python TensorFlow 2.0 for Regression Tasks
More Info

Using Python TensorFlow 2.0 for Regression Tasks


This tutorial develops a diamond price prediction tool to explain how to perform regression tasks
using Python's TensorFlow 2.0 library for deep learning.
import seaborn as sns
import pandas as pd
import numpy as np
import tensorflow as tf

from tensorflow.keras.layers import Input, Dense, Dropout, Activation


from tensorflow.keras.models import ModelM

tf.__version__

reg_dataset = sns.load_dataset('diamonds')
reg_dataset.head()

reg_dataset.shape

reg_dataset.describe()

list(reg_dataset.cut.unique())

numeric_data= reg_dataset.drop(['cut','color', 'clarity'], axis=1)

cut_onehot = pd.get_dummies(reg_dataset.cut).iloc[:,1:]
color_onehot = pd.get_dummies(reg_dataset.color).iloc[:,1:]
clarity_onehot = pd.get_dummies(reg_dataset.clarity).iloc[:,1:]

cut_onehot.head()

reg_dataset = pd.concat([numeric_data,cut_onehot, color_onehot,clarity_onehot], axis=1)

reg_dataset.head()

features = reg_dataset.drop(['price'], axis=1).values

labels = reg_dataset['price'].values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=40)

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

input_layer = Input(shape=(features.shape[1],))
l1 = Dense(100, activation='relu')(input_layer)
l2 = Dense(50, activation='relu')(l1)
l3 = Dense(25, activation='relu')(l2)
output = Dense(1)(l3)

model = Model(inputs=input_layer, outputs=output)


model.compile(loss="mean_absolute_error" , optimizer="adam", metrics=["mean_absolute_error"])

Page 45 of 93 wellsr.com
Using Python TensorFlow 2.0 for Regression Tasks
More Info
print(model.summary())

history = model.fit(X_train, y_train, batch_size=2, epochs=20, verbose=1, validation_split=0.2)

from sklearn.metrics import mean_absolute_error


from math import sqrt

pred_train = model.predict(X_train)
print(mean_absolute_error(y_train,pred_train))

pred = model.predict(X_test)
print(mean_absolute_error(y_test,pred))

Page 46 of 93 wellsr.com
Seaborn Histogram DistPlot Tutorial for Python
More Info

Seaborn Histogram DistPlot Tutorial for Python


Learn how to plot different types of histograms using the seaborn library for Python. This tutorial
creates Seaborn histograms and edits the way they look.
sns.set_color_codes()
sns.distplot(titanic_dataset["age"], kde = False, color = "g")

plt.xlabel("Age of Passengers", fontsize= 12)


plt.title("Histogram for Passenger Age", fontsize= 15)

Page 47 of 93 wellsr.com
Python Machine Learning Examples with scikit-learn
More Info

Python Machine Learning Examples with scikit-learn


This tutorial is full of Python machine learning examples to teach you how to solve classification tasks
using the scikit-learn library for machine learning.
from sklearn.metrics import classification_report, accuracy_score
print(classification_report(y_test,y_pred))
print(accuracy_score(y_test, y_pred ))

Page 48 of 93 wellsr.com
Joining DataFrames with Python Pandas Join
More Info

Joining DataFrames with Python Pandas Join


This tutorial teaches how to perform SQL-like join operations in Python using the pandas library. The
pandas join method lets you combine DataFrame table rows.
df1.join(df2, on='key', how = 'inner')

Page 49 of 93 wellsr.com
Using Pandas Apply on Dataframes and Series
More Info

Using Pandas Apply on Dataframes and Series


This tutorial explains how to use the Pandas Apply function to perform operations on each row or
column of a Pandas dataframe or series with the help of examples.
bank_data["Credit_Balance_Avg"] = bank_data[["CreditScore", "Balance"]].apply(get_mean, axis = 1)
bank_data[["CreditScore", "Balance", "Credit_Balance_Avg"]].head()

Page 50 of 93 wellsr.com
Python Default Arguments and Function Overloading
More Info

Python Default Arguments and Function Overloading


This tutorial explains default arguments in Python functions and how these default arguments are
used for function overloading.
File "<ipython-input-6-2ad3394c94dc>", line 1
def multiply(a, b = 10, c):
^
SyntaxError: non-default argument follows default argument

Page 51 of 93 wellsr.com
Python Lemmatization with NLTK
More Info

Python Lemmatization with NLTK


We'll teach you how to perform Python lemmatization with the NLTK library. Lemmatization reduces
words to a single form, which is important for NLP in Python.
token = "quickest"

lemma = wn_lemma.lemmatize(token, 'a')


print(token, "=>", lemma)

Page 52 of 93 wellsr.com
How to Perform Python NLTK Tokenization
More Info

How to Perform Python NLTK Tokenization


Here's how to perform Python tokenization with the Natural Language Toolkit (NLTK). NLTK
tokenization is the process of dividing text into sentences and words.
- Recent research has increasingly focused on unsupervised and semi-supervised learning algorithms.
- Such algorithms can learn from data that has not been hand-annotated with the desired answers or using a
combination of annotated and non-annotated data.
- Generally, this task is much more difficult than supervised learning, and typically produces less accurate
results for a given amount of input data.
- However, there is an enormous amount of non-annotated data available (including, among other things, the
entire content of the World Wide Web), which can often make up for the inferior results if the algorithm used
has a low enough time complexity to be practical.

Page 53 of 93 wellsr.com
Remove Stop Words with Python NLTK
More Info

Remove Stop Words with Python NLTK


Learn how to remove stop words from a string with Python NLTK. Because NLTK stores stop words
as a list, you can customize your list of stop words, too.
In Python , need end statement with semicolon .

Page 54 of 93 wellsr.com
Stemming in Python with NLTK Examples
More Info

Stemming in Python with NLTK Examples


This tutorial explains how to perform stemming in Python using the NLTK library. Porter stemmer and
snowball stemmer are two ways to perform stemming in Python.
from nltk.stem import SnowballStemmer
import nltk
sentence = "Computers are used for computing"

tokens = nltk.word_tokenize(sentence )

sb_stem =SnowballStemmer("english")
for token in tokens :
stem = sb_stem.stem(token)
print(token, "=>", stem)

Page 55 of 93 wellsr.com
Python Named Entity Recognition with NLTK & spaCy
More Info

Python Named Entity Recognition with NLTK & spaCy


Let's learn how to perform named entity recognition in Python using NLTK and spaCy. Named entities
are real world objects like people, products, and places.
import spacy
import en_core_web_sm
spacy_model = en_core_web_sm.load()

sentence = """Born and raised in Madeira, Ronaldo began his senior club career playing for Sporting CP,
before signing with Manchester United in 2003, aged 18. After winning the FA Cup in his first season,
he helped United win three successive Premier League titles, the UEFA Champions League, and the FIFA Club World
Cup"""

entity_doc = spacy_model(sentence)
entity_doc.ents

print([(entity.text, entity .label_) for entity in entity_doc.ents])

Page 56 of 93 wellsr.com
Understanding Inheritance in Python with Examples
More Info

Understanding Inheritance in Python with Examples


This article explains how to implement inheritance in Python. The process of single and multiple
inheritance and abstract classes is also explained.
class X:
def methodA(self):
print("This is method A of class X")

class Y:
def methodB(self):
print("This is method B of class Y")

class Z(X,Y):
def methodC(self):
print("This is method C of child class Z")

Page 57 of 93 wellsr.com
Performing CRUD operations with Python and MySQL
More Info

Performing CRUD operations with Python and MySQL


This article explains how to create a database and tables in MySQL, and how to perform Create,
Read, Update and Delete (CRUD) operations with MySQL using Python.
# pip install mysql-connector-python

# Creating a Connection with MySQL Server


import mysql.connector

host_name = "localhost"
user_name = "root"
password = ""

my_con = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=password
)

# Creating a Database inside MySQL Server

cursor = my_con.cursor()
query = "CREATE DATABASE patient_db"
cursor.execute(query)

# Connecting to the newly created database


host_name = "localhost"
user_name = "root"
password = ""
db_name = "patient_db"

my_db_con = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=password,
database=db_name
)

# Creating a table inside the database

create_table_query = """
CREATE TABLE IF NOT EXISTS patients (
id INT AUTO_INCREMENT,
name TEXT NOT NULL,
age INT,
sex TEXT,
PRIMARY KEY (id)
) ENGINE = InnoDB
"""

cursor = my_db_con.cursor()
cursor.execute(create_table_query)
my_db_con.commit()

Page 58 of 93 wellsr.com
Performing CRUD operations with Python and MySQL
More Info
# Inserting Records in a Table

create_patients_query = """
INSERT INTO
`patients` (`name`, `age`, `sex`)
VALUES
('Laura', 24, 'female'),
('Jospeh', 41, 'male'),
('Angel', 33, 'female'),
('Elisabeth', 37, 'female'),
('Joel', 19, 'male');
"""

cursor = my_db_con.cursor()
cursor.execute(create_patients_query)
my_db_con.commit()

# Selecing Records from a Table

select_patients_query = "SELECT * FROM patients"


cursor.execute(select_patients_query)

patients = cursor.fetchall()

for patient in patients:


print(patient)

# Updating Records in a Table

update_patients_query = """
UPDATE
patients
SET
sex = 'f'
WHERE
sex = 'female'

"""

cursor.execute(update_patients_query)
my_db_con.commit()

# Deleting Records from a Table

delete_patients_query = "DELETE FROM patients WHERE sex = 'male'"

cursor.execute(delete_patients_query)
my_db_con.commit()

Page 59 of 93 wellsr.com
Web Scraping with Python Scrapy
More Info

Web Scraping with Python Scrapy


Python is one of the best programming languages you can use for web scraping, and the Python
Scrapy library makes it exceptionally easy.
import scrapy

class ImdbSpiderSpider(scrapy.Spider):
name = 'imdb_spider'
allowed_domains = ['imdb.com']
start_urls = ["https://www.imdb.com/search/title/?groups=top_250"]

def parse(self, response):

movies = response.xpath("//div[@class='lister-item-content']")

for movie in movies:


movie_name = movie.xpath(".//h3//a//text()").get()
movie_link = "https://www.imdb.com" + movie.xpath(".//h3//a//@href").get()
movie_rating = movie.xpath(".//div[@class='ratings-bar']//strong//text()").get()

yield{
'movie name':movie_name,
'movie link':movie_link,
'movie_rating':movie_rating
}

Page 60 of 93 wellsr.com
Python Speech Recognition and Audio Transcription
More Info

Python Speech Recognition and Audio Transcription


This tutorial shows you how to use Python speech recognition libraries, like PyAudio and
SpeechRecognition, to recognize speech and transcribe audio from microphones and audio files.
import speech_recognition as sr
audio_data = sr.AudioFile('E:/audio_file.wav')
recognizer = sr.Recognizer()
with audio_data as file_audio:
print("Start transcribing file ...")

recognizer.adjust_for_ambient_noise(file_audio)

file_audio = recognizer.record(file_audio, offset = 5, duration = 10)

print( recognizer.recognize_google(file_audio))

Page 61 of 93 wellsr.com
14 Examples To Help You Understand Python List Slicing
More Info

14 Examples To Help You Understand Python List Slicing


This tutorial explains what Python list slicing is and how to create subsets of one-dimensional and two
dimensional lists in Python using list slicing.
import numpy
my_list2 = [["A", "B", "C", "D"],
["E", "F", "G", "H"],
["I", "J", "K", "L"],
["M", "N", "O", "P"]]
my_list2 = numpy.array(my_list2)
my_slice2 = my_list2[-3:,-3:]
print(my_slice2)

Page 62 of 93 wellsr.com
Zipping and Unzipping Files and Folders with Python
More Info

Zipping and Unzipping Files and Folders with Python


Learn how to zip and unzip files and folders in Python using zipfile and shutil. This tutorial includes
examples to zip individual files and entire directories.
import shutil
shutil.unpack_archive("E:\my_docs_folder2.zip", "E:\my_extracted_docs2", "zip")

Page 63 of 93 wellsr.com
Sending Emails through Gmail with Python
More Info

Sending Emails through Gmail with Python


Sending an email through Gmail with Python is easy. Simply reference the proper Gmail SMTP
server and configure your Python script using the smtplib library.
smtp_obj.sendmail(email_from_address, email_to_address, email_content.as_string())

Page 64 of 93 wellsr.com
Read Gmail Emails With Python
More Info

Read Gmail Emails With Python


This tutorial teaches you how to access your Gmail account through Python and even read emails in
your inbox using Python's imap_tools library.
from imap_tools import MailBox
import getpass
import os
import io
import PIL.Image as Image
my_email = getpass.getpass("Enter your email:")
my_pass = getpass.getpass("Enter your app password:")
mailbox = MailBox('imap.gmail.com').login(my_email, my_pass )
for msg in mailbox.fetch('SUBJECT "mobile_phones_list"', charset='utf8'):
for att in msg.attachments:
print(att.filename)
bytes = att.payload
image = Image.open(io.BytesIO(bytes))
image.save(r"C:/"+att.filename)

Page 65 of 93 wellsr.com
Python Machine Learning for Spam Email Detection
More Info

Python Machine Learning for Spam Email Detection


This tutorial shows you how to create a spam email detector based on machine learning techniques
using Python's scikit-learn library for machine learning.
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt


import seaborn as sns

%matplotlib inline #comment this line out if not using Jupyter notebook

dataset = pd.read_csv(r"C:\Datasets\email_dataset.csv")
dataset.head(10)

print("Data Visualization")

dataset.shape

dataset.spam.value_counts()

sns.countplot(x='spam', data=dataset)

print("Data Preprocessing")

messages = dataset["text"].tolist()
output_labels = dataset["spam"].values

import re

processed_messages = []

for message in messages:


message = re.sub(r'\W', ' ', message)
message = re.sub(r'\s+[a-zA-Z]\s+', ' ', message)
message = re.sub(r'\^[a-zA-Z]\s+', ' ', message)
message = re.sub(r'\s+', ' ', message, flags=re.I)
message = re.sub(r'^b\s+', '', message)
processed_messages.append(message)

print("Converting text to numbers")

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(processed_messages, output_labels, test_size=0.2,
random_state=0)

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

from sklearn.feature_extraction.text import TfidfVectorizer


vectorizer = TfidfVectorizer(max_features=2000, min_df=5, max_df=0.75, stop_words=stopwords.words('english'))
X_train = vectorizer.fit_transform(X_train).toarray()
X_test = vectorizer.transform(X_test).toarray()

print("Training and Evaluating the Spam Classifier Model")

Page 66 of 93 wellsr.com
Python Machine Learning for Spam Email Detection
More Info

from sklearn.ensemble import RandomForestClassifier


spam_classifier = RandomForestClassifier(n_estimators=200, random_state=42)
spam_classifier.fit(X_train, y_train)

y_pred = spam_classifier .predict(X_test)

from sklearn.metrics import accuracy_score


print(accuracy_score(y_test, y_pred))

Page 67 of 93 wellsr.com
How to Extend Functions with Python Decorators
More Info

How to Extend Functions with Python Decorators


Python decorators let you extend the behavior of a function in Python without explicitly modifying the
original function. Let's show you how to build your own.
def my_decorator(function_to_be_decorated):

def wrapper_function():
print("This is some code before the decorated function")

function_to_be_decorated()

print("This is some code after the decorated function")

return wrapper_function

@my_decorator
def my_func():
print("This is the function to be decorated")

@my_decorator
def my_func2():
print("This is another function to be decorated")

my_func2()

Page 68 of 93 wellsr.com
Python unittest Examples for Testing Python Code
More Info

Python unittest Examples for Testing Python Code


The Python unittest module is used to perform unit testing in Python. We'll explain unit testing and
show examples to help you get started with the unittest module.
import unittest
import arithmetic

class TestArithmetic (unittest.TestCase):

def test_square(self):
num = 5
result = arithmetic.take_square(num)
self.assertEqual(result, 25)

def test_cube(self):
num = 3
result = arithmetic.take_cube(num)
self.assertEqual(result, 27)

def test_power(self):
num1 = 2
num2 = 6

result = arithmetic.take_power(num1, num2)


self.assertEqual(result, 64)

if __name__ == '__main__':
unittest.main()

Page 69 of 93 wellsr.com
Multithreading in Python: Running Functions in Parallel
More Info

Multithreading in Python: Running Functions in Parallel


This tutorial shows how to implement multithreading in Python. Multithreading lets you run functions
in parallel, which we'll demonstrate with several examples.
from threading import *
from time import *

class App1(Thread):
def my_function(self, name):

for i in range(5):
print("thread "+ str(name))
sleep(1)

app1 = App1()

t1 = Thread(target=app1.my_function, args=("alpha",))
t1.start()

t2 = Thread(target=app1.my_function, args=("beta",))
t2.start()

Page 70 of 93 wellsr.com
3 Ways to Calculate Python Execution Time
More Info

3 Ways to Calculate Python Execution Time


This article shows you three ways to calculate the execution time of a Python script using the default
time module, the timeit module, and the %%timeit command.
print(timeit.timeit(statement2, setup2, number = 1000000))

Page 71 of 93 wellsr.com
Understanding Python if __name__ == '__main__'
statements
More Info

Understanding Python if __name__ == '__main__' statements


Today, we'll explain Python's if __name__ == '__main__' statement, which is used to check whether a
Python file was run directly or imported by another file.
This is file one
File one is imported in another file
This is file two
This is a function in file 1
File two was run directly

Page 72 of 93 wellsr.com
How to Read PDF Files with Python using PyPDF2
More Info

How to Read PDF Files with Python using PyPDF2


This article shows you how to read PDF files in Python using the PyPDF2 library. You can use this
library to extract data from PDFs stored on your computer or online.
import urllib.request
import PyPDF2
import io

URL = 'http://www.africau.edu/images/default/sample.pdf'
req = urllib.request.Request(URL, headers={'User-Agent' : "Magic Browser"})
remote_file = urllib.request.urlopen(req).read()
remote_file_bytes = io.BytesIO(remote_file)
pdfdoc_remote = PyPDF2.PdfFileReader(remote_file_bytes)

for i in range(pdfdoc.numPages):
current_page = pdfdoc.getPage(i)
print("===================")
print("Content on page:" + str(i + 1))
print("===================")
print(current_page.extractText())

Page 73 of 93 wellsr.com
How to use the Python pdb debugger
More Info

How to use the Python pdb debugger


We want to show you how to debug a Python script using the Python pdb debugger module. Pdb is a
built-in Python utility specifically for code debugging.
quantity = 10
sales = {'Oranges':100, 'Apples':200, 'Bananas': 150, 'Mango': 0, 'Peaches':200}

import pdb

for item, sale in sales.items():


try:
unit_price = (quantity/sale)*100
print(item +":" + str(unit_price))
except ZeroDivisionError:
pdb.set_trace()

Page 74 of 93 wellsr.com
Python Face Detection for Beginners with OpenCV
More Info

Python Face Detection for Beginners with OpenCV


Python face detection is easy with the OpenCV library. We'll walk you through the entire process,
including how to detect eyes and smiles using Python OpenCV.
import cv2
import matplotlib.pyplot as plt
face1 = cv2.imread(r"C:/Datasets/faces/aj6.jpg", 0)
fd = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

def get_detected_face (face):


face_img = face.copy()
fr = fd.detectMultiScale(face_img)
for (x,y,width,height) in fr:
cv2.rectangle(face_img, (x,y), (x + width, y+height), (255,255,77), 10)
return face_img

result = get_detected_face(face1)
plt.imshow(result, cmap = "gray")
plt.show()

Page 75 of 93 wellsr.com
Convert Image to String with Python Pytesseract OCR
More Info

Convert Image to String with Python Pytesseract OCR


This tutorial shows you how to read text from images using the Python Pytesseract wrapper for
Google's Tesseract engine. OCR lets you convert images to strings.
Test Receipt for USB Printer 1

Mar 17, 2018


10:12 PM

Ticket: 01

Item $0,00

Total $0.00

Page 76 of 93 wellsr.com
Python Image Manipulation with Pillow Library
More Info

Python Image Manipulation with Pillow Library


This tutorial explains how to perform Python image manipulation tasks, like resizing, blurring, and
cropping, using the Python Pillow library.
from PIL import Image, ImageFilter
import os

source_path = r'C:/Datasets/Pillow Images/'


destination_path = r'C:/Datasets/Pillow Images/thumbnails/'

all_images = os.listdir(source_path)

for image in all_images[:-1]:


image_path = (source_path + image)
image1 = Image.open(image_path)
image1=image1.convert('RGB')
image1.thumbnail((100,100))
image1 = image1.filter(ImageFilter.SHARPEN)
image1.save(destination_path+image)

Page 77 of 93 wellsr.com
Drawing Shapes on Images with the Python OpenCV Library
More Info

Drawing Shapes on Images with the Python OpenCV Library


This tutorial shows you how to add text and draw shapes, like lines, rectangle, circles, and polygons,
on images using the Python OpenCV library.
image_path = r'C:/Datasets/Pillow Images/my_car.jpg'
image1 = cv2.imread(image_path)
image_correct = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

cv2.rectangle(
image_correct,
pt1 = (300,100), pt2 = (600,300),
color = (255,0,255),
thickness = 10
)
plt.imshow(image_correct)

Page 78 of 93 wellsr.com
Python chdir and getcwd to Set Working Directory
More Info

Python chdir and getcwd to Set Working Directory


Let's introduce the Python OS module by demonstrating how to get and set your current working
directory using the Python chdir and getcwd methods.
import os
os.chdir(r"C:\main directory")
wd = os.getcwd()

Page 79 of 93 wellsr.com
List Files in a Folder With Python OS listdir and walk
More Info

List Files in a Folder With Python OS listdir and walk


This week, we're going to use the Python OS module to show you how to list all files in a folder using
the Python OS listdir and walk methods.
import os
wd = os.getcwd()
directories = os.walk(wd)
dirpath, dirnames, filenames = next(directories)
print("Directory Path:", dirpath)
print("Directory Names:", dirnames)
print("File Names:", filenames)

Page 80 of 93 wellsr.com
How to Open Files with Python OS
More Info

How to Open Files with Python OS


This tutorial explains how to open files using Python OS. The Python OS module makes it easy to
launch files or applications using built-in system commands.
import os
os.system(r"C:\Users\Public\Documents\SampleFile.txt")
print("File opened")

Page 81 of 93 wellsr.com
Python OS Delete Files and Create Folders
More Info

Python OS Delete Files and Create Folders


With Python OS, you can create and delete files and folders. We're going to demonstrate how to
control your files and folders in this Python tutorial.
import os
os.rmdir("games2")
print(os.listdir())

Page 82 of 93 wellsr.com
Logging Info and Errors with the Python Logging Module
More Info

Logging Info and Errors with the Python Logging Module


Logging info is not enabled by default. This tutorial teaches you how to use the Python logging
module to log info, errors and exceptions in your Python script.
import logging
format_string = "%(levelname)s - %(asctime)s : %(message)s"
logging.basicConfig(filename = r"C:\Datasets\mylogger.log",
level = logging.DEBUG, force=True,
format = format_string,
filemode = 'w')
my_logger = logging.getLogger()

i = 0
def list_divide(nums1, nums2):

global i
i = i + 1
my_logger.info("Function called " + str(i) + " times")
my_logger.info("Numerator list" + str(nums1))
my_logger.info("Denominator list" + str(nums2))

try:
result = []
for num,den in zip(nums1, nums2):

result.append(num/den)

return result
except Exception as e:
print("an error occured")
my_logger.error(e)

Page 83 of 93 wellsr.com
Random Number Generation in Python with Random Module
More Info

Random Number Generation in Python with Random Module


This tutorial shows how to generate random numbers in Python using the Random module. We'll use
this to randomly choose items from a Python list.
import random
colors = ["Red", "Green", "Blue", "Yellow"]
print(random.choice(colors))

Page 84 of 93 wellsr.com
Python Delete All Files in a Folder
More Info

Python Delete All Files in a Folder


This tutorial shows you how to delete all files in a folder using different Python libraries. We'll also
demonstrate how to only delete files with a specific extension.
import shutil, os
shutil.rmtree(r"C:\main directory")
os.mkdir(r"C:\main directory")
os.listdir(r"C:\main directory")

Page 85 of 93 wellsr.com
Python Map, Filter and Reduce for Lambda Functions
More Info

Python Map, Filter and Reduce for Lambda Functions


This article explains how to perform different operations on individual items in Python lists using the
Map, Filter and Reduce functions with Lambda functions.
from functools import reduce
items = list(range(1,6))
fact = reduce(lambda x,y: x * y, items)
print(fact)

Page 86 of 93 wellsr.com
Sorting items in Python using Sort and Sorted Methods
More Info

Sorting items in Python using Sort and Sorted Methods


This article explains sorting in Python using the Sort and Sorted methods. The former sorts items in-
place while the latter create copies of sorted collections, like lists and dictionaries.
students = [
("John", 25, True, 10500),
("Sally", 30, True, 7900),
("Elizabeth", 21, False, 7250),
("Nick", 19, False,8520),
("Rose", 27, True, 2600)
]

amount = lambda student:student[3]


students_sorted = sorted(students, key = amount, reverse = True)

print(students)
print(students_sorted)

Page 87 of 93 wellsr.com
NumPy Eigenvalues and Eigenvectors with Python
More Info

NumPy Eigenvalues and Eigenvectors with Python


Let's learn how to calculate eigenvalues and eigenvectors for a given matrix using the NumPy eigvals
and eig methods in Python.
import numpy as np

A2 = np.array([[4, 3, 9], [8, 2, 9], [1, 8, 3]]) #3x3 NumPy array


print(A2)

E2 = np.linalg.eigvals(A2) #calculate eigenvalues


print(E2)

Eval, Evec = np.linalg.eig(A2) #calculate eigenvalues and eigenvectors


print(Eval)
print(Evec)

Page 88 of 93 wellsr.com
Outer Product of Two Vectors or Matrices with Python
NumPy
More Info

Outer Product of Two Vectors or Matrices with Python NumPy


Today we're going to show you examples of how to calculate the outer product of two vectors or
matrices using the NumPy library in Python.
import numpy as np

m1 = np.array([[4,1,8],
[3,5,7],
[7,2,6]])

m2 = np.array([[7,2,9],
[3,1,2],
[4,2,9]])

print("Original Vectors:")
print(m1)
print(m2)

print("Outer product:")
op = np.outer(m1, m2)
print(op)

Page 89 of 93 wellsr.com
Python k-means clustering with scikit-learn
More Info

Python k-means clustering with scikit-learn


Let's walk through a real-world example of how to perform data clustering using the Python scikit-
learn k-means clustering algorithm.
#import dependencies
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt

import numpy as np
import pandas as pd

import seaborn as sns


sns.set_style("darkgrid")

#load dataset
X = pd.read_csv('https://raw.githubusercontent.com/krishnaik06/DBSCAN-Algorithm/master/Mall_Customers.csv')
X.head()

#filter the columns


X = X.filter(["Annual Income (k$)", "Spending Score (1-100)" ], axis = 1)

#plot the raw data


sns.scatterplot(data = X, x="Annual Income (k$)", y= "Spending Score (1-100)", c = ["green"])
plt.show()

#apply scikit-learn kmeans clustering


model = KMeans(n_clusters= 5)
model.fit(X)

#print the centroids


print(model.cluster_centers_)

#plot the clustered data with centroids overlaid


sns.scatterplot(data = X, x="Annual Income (k$)", y= "Spending Score (1-100)", c= model.labels_, cmap= 'rainbow'
)
sns.scatterplot(x=model.cluster_centers_[:, 0], y=model.cluster_centers_[:, 1], c=['black'])

#create dataframe with clustered labels


segmented_data = pd.DataFrame()
segmented_data['Customer Number'] = X.index.values
segmented_data['Segment'] = model.labels_

#Save segment 3 as a dataframe and display head


segmented_data = segmented_data[segmented_data.Segment==3]
segmented_data.head()

Page 90 of 93 wellsr.com
Asynchronous Programming in Python with Asyncio
More Info

Asynchronous Programming in Python with Asyncio


Let's talk about how to do asynchronous programming in Python using the asyncio module.
Asynchronous functions are functions that execute in parallel.
import asyncio

async def parent_func():

#function to call if current function


#is waiting for something
task = asyncio.create_task(child_func1())

print("Item 1")
#add sleep to add a dummy wait
await asyncio.sleep(2)
print("item 2")

#wait for asynchronous function return


value = await task
print(value)

async def child_func1():

print("Price 1")
print("Price 2")

return "Price 3"

asyncio.run(parent_func())

Page 91 of 93 wellsr.com
Python Agglomerative Clustering with sklearn
More Info

Python Agglomerative Clustering with sklearn


We're going to walk through a real-world example of how to perform Python hierarchical clustering in
sklearn with the agglomerative clustering algorithm.
plt.scatter(x=X["Annual Income (k$)"], y=X["Spending Score (1-100)"], c= model.labels_, cmap='rainbow' )
plt.xlabel("Annual Income (k$)")
plt.ylabel("Spending Score (1-100)")

Page 92 of 93 wellsr.com
Python PCA Examples with sklearn
More Info

Python PCA Examples with sklearn


Let's take a look at some examples showing how to use principal component analysis (PCA) for
dimensionality reduction with the Python scikit-learn library.
from sklearn.decomposition import PCA

pca_model = PCA(n_components=2)
X_train_pca = pca_model.fit_transform(X_train)
X_test_pca = pca_model.transform(X_test)

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=50, random_state=0)


model.fit(X_train_pca, y_train)
y_pred = model.predict(X_test_pca)

print(accuracy_score(y_test, y_pred)

Page 93 of 93 wellsr.com

You might also like