Skip to main content

Standard Normal Distribution with examples using Python

Iterables and Iterators- Part 2

In continuation to my previous post on Iterables, we should now get familiar with something new. Let's again create a list.


lst = [1,2,3,4]
dir(lst)

--->['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', 
'__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 
'insert', 'pop', 'remove', 'reverse', 'sort']

The functions with "__" in front and behind ('__add__') are called dunder methods. More on this in later posts. You can see a method called __iter__ . All iterables will contain this method. Generally, __iter__ method returns an iterator. So if the object is an iterator, inside the __iter__ method, you will generally see return self . We will talk more about classes and methods in later posts. Ignore this if you do not understand it for now. Python gives you the flexibility to create an object so as to iterate over a Python data structure. We call this an Iterator . An iterator can be created using the iter keyword. For the list "lst" that we created before, this is how we create an iterator.


it =  iter(lst)
for element in it:
    print(element)
--->   1
       2
       3
       4

Let's inspect what happens when we create an iterator


dir(it)
---> ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__']

The iterator object internally creates two methods __iter__ and __next__. When some one new to Python reads this, the first thing which comes to mind is how is this even useful? I already have a "for" loop which can help me iterate through an iterable. So why do I need to create an iterator now? Very valid question and the answer in this context is that, the iterator is not so useful. We will work on scenarios later which will make iterators useful. For now, all we need to understand is that in order to create an iterator, we need to have two methods in place . __iter__ and __next__. Let's create the iterator again.


it =  iter(lst)
 

Let's see the effect of using next() on the iterator object at each step

next(it)
---> 1
next(it
---> 2
next(it)
---> 3
next(it)
---> 4
next(it)
-----------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
 in 
----> 1 next(it)

StopIteration: 

The moment we have traversed all elements in the list using the it iterator , you will receive a StopIteration error to indicate that there are no more elements to be traversed. At this stage, if you run the below command, it turns out, you will get no output as well.


for i in it:
  print(i)

--->  

If you want to see results from your iterator, you need to recreate the iterator as done before.


it =  iter(lst)
 

We will use these concepts as building blocks to understand more complex ideas in later posts.

Comments

Popular posts from this blog

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 2

In the previous post , we set up the context to utilize embeddings for categorical features. In this post, we will figure out how to create these embeddings and combine them with other continuous features to build a neural network model. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also available in Kaggle . Metadata about this dataset is available on the respective websites. To follow this post, it is recommended to download the dataset from Kaggle. Most of the features are self explanatory. Embedding Creation A few definitions first. Levels in a categorical feature represent unique values available for that categorical feature. For e.g. MARRIAGE has levels 0,1,2,3. Each level of a categorical feature is represented by a vector of numbers. So, if you stack up all the levels together and all the vectors together, you can imagine levels to be a colum

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1 In this post, we will talk about using embeddings for categorical features using PyTorch. This post will be broken down into following parts. Dataset Download Data Understanding Data Preprocessing Embedding Creation Define Dataset and Dataloaders in PyTorch Neural Network definition in PyTorch The Training Loop Model Validation The idea about using Embeddings from Categorical Features was first mooted during a Kaggle contest and a paper was also published on this. In the context of NLP and word embeddings, we represent each word in an n dimesnional vector space. In a similar way, we can represent any categorical feature in an n dimesnional vector space as well. 1. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also av

Standard Normal Distribution with examples using Python

Standard Normal Distribution with examples In our previous post, we talked about Normal Distribution and its properties . In this post, we extend those ideas and discuss about Standard Normal Distribution in detail. What is a Standard Normal Distribution? A Normal Distribution with mean 0 and standard deviation 1 is called a Standard Normal Distribution . Mathematicallty, it is given as below. Fig 1:Standard Normal Probability Distribution Function For comparison, have a look at the Normal Probability Distribution Function. If you substitute mean as 0 ,standard deviation as 1, you derive the standard normal probability distribution function Fig 2: Normal Probability Distribution Function Need for a standard normal probability distribution function We need to extract probability information about events that we are interested in. For this, first we need to convert any normal random variable