Skip to main content

Standard Normal Distribution with examples using Python

How to define a Class in Python?

How to define a Class in Python?

What is self and __init__ in Python?

We will try to explain all this in this post. Before we explain about classes, let us make some assumptions. Imagine a scenario where you are tracking your household expenses. In partcular, milk, egg and newspaper expenses. There are 2 kinds of milk available. Let's call them blue and green. We have 2 kinds of egg available, small and large and 2 kinds of newspaper available Deccan Herald and TOI


class Expenses:
    pass
print(Expenses)
class __main__.Expenses

We just created a class called Expenses. The pass just tells you that we will revisit Expenses class later. Now, let's assume, I wanted to track my expenses. How do I use the Expenses class for my purpose? I need to create an instance of a class for myself. Let's do that below.


Rohit_expenses = Expenses()
print(Rohit_expenses)
__main__.Expenses object at 0x7f74483de950>

We can see that an Expenses object is created. Rohit_expenses is an object which is an instance of the class Expenses. We cannot do much with an empty class. So let's create some variables within this class.


class Expenses:
  num_days = 31
  blue_milk_rate = 18
  green_milk_rate = 21
  deccan_herald_rate = 6
  toi_rate = 5
  small_egg_rate= 8
  large_egg_rate = 12

We have defined number of days in a month as 31, defined different milk rates, newspaper rates and egg rates. Let's not forget that we are trying to track household expenses for any person. If I need to track my expenses, I need to give the class some initial set of information. This is where __init__ method is required to be defined in the class. A method is a function defined within a class.


class Expenses:
  num_days = 31
  blue_milk_rate = 18
  green_milk_rate = 21
  deccan_herald_rate = 6
  toi_rate = 5
  small_egg_rate= 8
  large_egg_rate = 12
  
  def __init__(self,name,milk_type,newspaper_type,egg_type):
    self.name = name
    self.milk_type= milk_type
    self.newspaper_type = newspaper_type
    self.egg_type = egg_type

self represents an instance of the class. When you create an instance of a class, if an __init__ method is defined, correponding variables for the instance are initialized. In this case, we are initializing the name of the person, the type of milk, newspaper, egg being purchased for that person. Let's see it in action.


Rohit_expenses = Expenses(name = "Rohit Rudrapatna" , milk_type = "green", newspaper_type = "deccan",  egg_type = "small")
Neil_expenses = Expenses(name = "Neil Neller" , milk_type = "blue", newspaper_type = "toi",  egg_type = "large")

To see if the __init__ method worked, let's try to access the variables attached to Rohit_expenses.


Rohit_expenses.name, Rohit_expenses.milk_type, Rohit_expenses.newspaper_type, Rohit_expenses.egg_type
---> ('Rohit Rudrapatna', 'green', 'deccan', 'small')

Worked like a charm!! What happened to those variables that we defined for the class Expenses. Are those variables created for the instance Rohit_expenses? We can find out now.


Rohit_expenses.num_days, Rohit_expenses.blue_milk_rate, Rohit_expenses.green_milk_rate, Rohit_expenses.small_egg_rate, Rohit_expenses.large_egg_rate, Rohit_expenses.deccan_herald_rate, Rohit_expenses.toi_rate
--->(31, 18, 21, 8, 12, 6, 5)

How different are those variables for Neil? Let's find out.


Neil_expenses.num_days, Neil_expenses.blue_milk_rate, Neil_expenses.green_milk_rate, Neil_expenses.small_egg_rate, Neil_expenses.large_egg_rate, Neil_expenses.deccan_herald_rate, Neil_expenses.toi_rate
--->(31, 18, 21, 8, 12, 6, 5)

The values are exactly the same for both Rohit and Neil. Variables like num_days , blue_milk_rate, toi_rate, small_egg_rate are called class variables. We call variables like name, milk_type, newspaper_type, egg_type as instance variables. What is the difference between them?

Understand that milk rates , newspaper rates , number of days in a month will be same across different instances of class Expenses. Milk rates nor newspaper rates cannot be specific to a person right? So we call such variables as class variables. Name of a person is specific to an instance of a class. Similarly, any person is free to choose the kind of milk, egg and newspaper. It becomes specific to that person or technically an instance of the class. We call such variables as instance variables. These variables are bound to the instance.

We will continue to develop these ideas in the next post. That's it for now

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