Day 9: 30 days of Natural Language Processing Series with Projects
Powerful Python…
Welcome back peeps. In the last post we saw NLTK introduction and continuing on the same lines, I’ll cover some Python constructs/one liners that we shall be using( indirectly)in the NLP projects going forward. Writing eloquent and concise code is an art.
Projects Videos —
All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).
Subscribe today!
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
To learn the basics and advanced level of Python you can go through the posts below —
Lets dive in —
Python’s Powerful One -liners —
Lambda Functions —
In python, Lambda is used to create small anonymous functions using “lambda” keyword and can be used wherever function objects are needed.It can any number of arguments but only one expression
Syntax :
lambda argument(s): expression
- It can be used inside another function
- In python normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword
- Whenever we require a nameless function for a short period of time, we use lambda functions
Example :
var = lambda x: x * 5
Implementation —
#A lambda function that adds 10 to the number passed in as an #argument, and print the result
x = lambda a, b, c : a * b + c
print(x(5, 6, 8))Output —
38
Generator Expressions and List Comprehensions —
In Python, Generator functions act just like regular functions with just one difference that they use the Python yield keyword instead of return . A generator function is a function that returns an iterator A generator expression is an expression that also returns an iterator
- Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop.
- A return statement terminates a function entirely but a yield statement pauses the function saving all its states and later continues from there on successive calls.
- Generator expressions can be used as the function arguments. Just like list comprehensions, generator expressions allow you to quickly create a generator object within minutes with just a few lines of code.
- The major difference between a list comprehension and a generator expression is that a list comprehension produces the entire list while the generator expression produces one item at a time as lazy evaluation. For this reason, compared to a list comprehension, a generator expression is much more memory efficient
Example —
def generator():
yield “x”
yield “y”
for i in generator():
print(i)
Implementation —
def test_sequence():
num = 0
while num<10:
yield num
num += 1
for i in test_sequence():
print(i, end=",")Output —
0,1,2,3,4,5,6,7,8,9,Implementation —
# Python generator with Loop#Reverse a string
def reverse_str(test_str):
length = len(test_str)
for i in range(length - 1, -1, -1):
yield test_str[i]
for char in reverse_str("Trojan"):
print(char,end =" ")Output —
n a j o r TImplementation —
# Generator Expression
# Initialize the list
test_list = [1, 3, 6, 10]
# list comprehension
list_comprehension = [x**3 for x in test_list]
# generator expression
test_generator = (x**3 for x in test_list)
print(list_comprehension)
print(type(test_generator))
print(tuple(test_generator))Output —
[1, 27, 216, 1000]
<class 'generator'>
(1, 27, 216, 1000)Swapping
To swap two variables without third variable, Python makes it quite easy —
n, p= p, nReverse a List —
- One of the most versatile data type in Python, Lists are used to store multiple items ( homogeneous or non-homogeneous) in a single variable.
- Place the items inside the square brackets[]
- Items can be of any data type
- Lists are defined as objects with the data type ‘list’
- Items are ordered, changeable, and allow duplicate values
- list() constructor can be used when creating a new list
- To access values in lists, use the square brackets for slicing along with the index to obtain item value available at a particular index
- Items inside list are indexed, the first item has index [0], the second item has index [1] etc
l = [1, 2, 3]r = lis[::-1] # reverse a list using slicingPatterns
The most pythonic ( one liner) way to build patterns —
p = 10print('\n'.join('# ' * i for i in range(1, p+ 1)))Reduce and Lambda Functions —
Reduce ( reduce()) function applies the function to the elements of the sequence, from left to right, starting with the first two elements in the sequence. Reduce is called with a lambda function and an iterable and a new reduced result is returned.
Syntax —
reduce(func, iterable[, initial])
import functools
z = 5functools.reduce(lambda p, n: p + n, range(1, z+ 2)))Files in Python —
To read a file in the most pythonic way, use the code below :
fl = [t.strip() for t in open('sample.txt', 'r')]Calculate Factorial
reduce(lambda c, d: c* d, range(1, n+1))Set from subsets
s = lambda a: [[d for c, d in enumerate(set(a)) if (p >> c) & 1] for i in range(2**len(set(a)))]Zip
zip() function basically takes iterables and aggregates them into a single iterable.
f = [dict(zip(c, r)) for r in f_rows]Regular Expressions
Regular Expressions are expressions/patterns used to find or match character combinations in text/strings.
These are text-matching tool embedded in Python which are very useful in creating string searches/performing any modifications in Strings.
p = [a[0] for ain re.findall('(\$[0-9]+(\.[0-9]*)?)', doc)]Day 10 : Coming soon!
For Complete Data Science and Machine Learning with projects series —
Follow for more updates, stay tuned and of-course let me end this post with a quote by Steve Jobs ;)
“Your time is limited, so don’t waste it living someone else’s life.”
For other projects, tune to —
Build Machine Learning Pipelines( With Code)
Recurrent Neural Network with Keras
Clustering Geolocation Data in Python using DBSCAN and K-Means
Facial Expression Recognition using Keras
Hyperparameter Tuning with Keras Tuner
Custom Layers in Keras





