Trending March 2024 # Complete Guide To Moment Generating Functions In Statistics For Data Science # Suggested April 2024 # Top 5 Popular

You are reading the article Complete Guide To Moment Generating Functions In Statistics For Data Science updated in March 2024 on the website Katfastfood.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested April 2024 Complete Guide To Moment Generating Functions In Statistics For Data Science

This article was published as a part of the Data Science Blogathon

Introduction

While dealing with Statistical Moments (for a particular probability distribution either Continuous or Discrete) such as Mean, Variance, Skewness, etc, it becomes very important to have a good understanding of Moment Generating Functions (MGF).

So, In this article, we will be discussing the complete idea behind Moment Generating Functions including its applications with some examples.

 

Table of Contents

1. What are Statistical Moments?

2. What is Moment Generating Function (MGF)?

3. Properties of MGF

4. Why do we need MGF?

5. Some Important Results of MGF

6. Applications of MGF

7. Problem Solving related to MGF

What are Statistical Moments?

Let’s X be a random variable in which we are interested, then the moments are the expected values of X, 

For Example, E(X), E(X²), E(X³), … etc.

The first moment is defined as E(X),

The second moment is defined as E(X²),

The third moment is defined as E(X³),

The n-th moment is defined as E(Xn).

In Statistics, we are pretty familiar with the first two moments:

The Mean (μ) = E(X)

The Variance (σ2) = E(X2) – (E(X))2 = E(X²) − μ²

These are the important characteristics for any general random variable X.

The mean denotes the average value and the variance represents how the data points are spread wrt mean in the distribution. But there must be other characteristics as well that also helps in defining the probability distributions.

For Example, In the third moment E(X3), Skewness tells about the asymmetry of distribution, and in the fourth moment E(X4), kurtosis tells about how heavy the tails of a distribution are.

                                                     Image Source: Google Images

What is the Moment Generating Function?

The moment generating function (MGF) associated with a random variable X, is a function,

MX : R → [0,∞] defined by

 MX(t) = E [ etX ]

In general, t can be a complex number, but since we did not define the expectations for complex-valued random variables, so we will restrict ourselves only to real-valued t. And the point to note that t= 0 is always a point in the ROC for any random variable since MX (0) = 1.

As its name implies, MGF is the function that generates the moments —

E(X), E(X²), E(X³), …, E(Xn).

Cases:

If X is discrete with probability mass function(pmf) pX (x), then

MX (t) = Σ etx pX (x)

If X is continuous with probability density function (pdf) fX (x), then

 MX (t) = ∫ etx fX (x) dx

Properties of Moment Generating Functions

1. Condition for a Valid MGF:

MX(0) = 1 i.e, Whenever you compute an MGF, plug in t = 0 and see if you get 1.

2. Moment Generating Property:

By looking at the definition of MGF, we might think that how we formulate it in the form of E(Xn) instead of E(etx). 

So, to do this we take a derivative of MGF n times and plug t = 0 in. then, you will get E(Xn).

                                              Image Source: Google Images

Proof:

To prove the above property, we take the help of Taylor’s Series:

Step-1: Let’s see Taylor’s series Expansion of eX and then by using that expansion, we generate the expansion for etX which we will use in later steps.

Step-2: Take the expectation on both sides of the equation, we get:

Step-3: Now, take the derivative of the equation with respect to t and then we will reach our conclusion.

In this step, we take the first derivative of the equation only but similarly, we can prove that:

If you take another derivative on equation-3 (therefore total twice), you will get E(X²).

If you take the third derivative, you will get E(X³), and so on.

Note:

When you try to deeply understand the concept behind the Moment Generating Function, we couldn’t understand the role of t in the function, since t seemed like some arbitrary variable that we are not interested in. However, as you see, t is considered as a helper variable.

So, to be able to use calculus (derivatives) and make the terms (that we are not interested in) zero, we introduced the variable t.

Why do we need MGF?

We can calculate moments by using the definition of expected values but the question is that “Why do we need MGF exactly”?

                                                        Image Source: Google Images

For convenience,

To calculate the moments easily, we have to use the MGF. But

“Why is the calculation of moments using MGF easier than by using the definition of expected values”?

Let’s understand this concept with the help of the given below example that will cause a spark of joy in you — the clearest example where MGF is easier: 

Let’s try to find the MGF of the exponential distribution.

Step-1: Firstly, we will start our discussion by writing the PDF of Exponential Distribution.

Step-2: With the help of pdf calculated in previous steps, now we determine the MGF of the exponential distribution.

Now, for MGF to exist, the expected value E(etx) should exist.

Therefore, `t – λ < 0` becomes an important condition to meet, because if this condition doesn’t hold, then the integral won’t converge. This is known as the Divergence Test.

Once you have to find the MGF of the exponential distribution to be λ/(λ-t), then calculating moments becomes just a matter of taking derivatives, which is easier than the integrals to calculate the expected value directly.

                                                     Image Source: Google Images

Therefore, with the help of MGF, it is possible to find moments by taking derivatives rather than doing integrals! So, this makes our life easier when dealing with statistical moments.

Important Results Related to MGF

Result-1: Sum of Independent Random Variables

Suppose X1,…, Xn are n independent random variables, and the random variable Y is defined by

Y = X1 + … + Xn.

Then, the moment generating function of random variable Y is given as,

MY(t)=MX1 (t)·…·MXn (t)

Result-2:

Suppose for two random variables X and Y we have MX(t) = MY (t) < ∞ for all t in an interval, then X and Y have the same distribution.

Applications of MGF

1. Moments provide a way to specify a distribution:

We can completely specify the normal distribution by the first two moments, mean and variance. As we are going to know about multiple different moments of the distribution, then we will know more about that distribution.

For Example, If there is a person that you haven’t met, and you know about their height, weight, skin color, favorite hobby, etc., you still don’t necessarily fully know them but to getting more and more about them we can take the help of this.

2. Finding any n-th moment of a distribution:

We can get any n-th moment once you have MGF i.e, expected value exists. It encodes all the moments of a random variable into a single function from which we can be extracted again later.

3. Helps in determining Probability distribution uniquely:

Using MGF, we can uniquely determine a probability distribution. If two random variables have the same expression of MGF, then they must have the same probability distribution.

4. Risk Management in Finance: 

In this domain, one of the important characteristics of distribution is how heavy its tails are.

For Example, If you know about the 2009 financial crisis, in which we were failing to address the possibility of rare events happening. So, risk managers try to understate the kurtosis, the fourth moment of many financial securities underlying the fund’s trading positions. So, sometimes seemingly random distributions with the help of hypothetically smooth curves of risk can have hidden bulges in them. So, to detect these bulges we can use the MGF.

Problem Solving related to MGF

Numerical Example: 

Suppose that Y is a random variable with MGF H(t). Further, suppose that X is also a random variable with MGF M(t) which is given by, M(t) = 1/3 (2e3t +1) H(t). Given that the mean of random variable Y is 10 and its variance is 12, then find the mean and variance of random variable X.

Solution:

Keep in mind all the results which we described above, we can say that

E(Y) = 10 ⇒ H'(0) =10,

E(Y2) – (E(Y))2 = 12 ⇒ E(Y2) – 100 = 12 ⇒ E(Y2) = 112 ⇒ H”(0) = 112

M'(t) = 2e3t H(t) + 1/3 ( 2e3t +1 )H'(t)

M”(t) = 6e3tH(t) + 4e3tH'(t) + 1/3 ( 2e3t +1 )H”(t)

Now, E(X) = M'(0) = 2H(0) + H'(0) = 2+10 =12

E(X2) = M”(0) = 6H(0) + 4H'(0) + H”(0) = 6 + 40 +112 = 158

Therefore, Var(X) = E(X2) – (E(X))2 = 158 -144 = 14

So, the mean and variance of Random variable X are 12 and 14 respectively.

This ends today’s discussion!

 

Endnotes

Thanks for reading!

I hope you enjoyed the article and increased your knowledge about Moment Generating Functions in Statistics.

Please feel free to contact me on Email

For the remaining articles, refer to the link.

About the Author Aashi Goyal

Currently, I am pursuing my Bachelor of Technology (B.Tech) in Electronics and Communication Engineering from Guru Jambheshwar University(GJU), Hisar. I am very enthusiastic about Statistics, and Data Science.

The media shown in this article on Moment Generating Functions are not owned by Analytics Vidhya and are used at the Author’s discretion.

Related

You're reading Complete Guide To Moment Generating Functions In Statistics For Data Science

Top Data Science Jobs To Apply For In March 2023

The  

Top data science jobs in March 2023 Data scientist at IBM

Location: India IBM India Pvt. Limited has been present in the country since 1992. IBM India’s solutions and services span all major industries including financial services, healthcare, government, automotive, telecommunication and education, among others. As a trusted partner with wide-ranging service capabilities, IBM helps clients transform and succeed in challenging circumstances. IBM has been expanding its footprint in India and has a presence in over 200 cities and towns across the country, either directly or through its strong business partner network.   Role and responsibility: The expected candidate should be a Subject Matter Expert on building big data solutions with great scalability. Led by a solution architect, they will provide expertise and leadership to help design IBM Data Science and AI solutions that will help the company’s clients drive technology benefits and business outcomes across industries. The candidate will work with cutting edge technologies such as Watson, as well as open-source approaches with a passionate team of people who are driving the innovation and digital transformation to cross-industry enterprise clients with the adoption of IBM Data Science and AI.   Apply for the job  

Data Scientist at HCL

Location: Noida, Delhi, India HCL Engineering and R&D Services (ERS) partners with global enterprises in accelerating product development by leveraging the latest technologies, monetizing product services, and providing an immersive customer experience. The company is one of the most valued broad-based global engineering service providers (ESPs). With over four decades of experience in supporting customers in their digital transformation, HCL seamlessly integrates with and complements customers’ product engineering and research and development activities. Apply for the job  

Data Scientist (Banking Domain) at Capgemini

Location: Chennai, Tamil Nadu, India Capgemini is a global leader in consulting, digital transformation, technology and engineering services. The Group is at the forefront of innovation to address the entire breadth of clients’ opportunities in the evolving world of cloud, digital platforms. Building on its storing 50-year heritage and deep industry-specific expertise, Capgemini enables organizations to realize their business ambitions through an array of services from strategy to operations. Role and Responsibility: The candidates applying for the data science job profile at Capgemini is expected to have 5 to 9 years of experience in the banking or IT industry on data science projects involving quantitative statistical and predictive analysis of large data. He/she should have prior knowledge of mathematical programming platforms/packages like R, Python, MATLAB and basic programming skills in Python or C. Apply for the job  

T&O- OA Data Scientist (Behavioral Science) at Accenture

Location: Gurugram, Haryana, India Role and responsibility: As an Organisational Analytics (OA) scientist in Accenture’s Talent & Organisation (T&O) practice, the candidate should fetch information from various sources and analyze it to better understand people behaviour. He/she should fetch information from various sources related to Behavioural science/industry and Organisational Psychology/Social Sciences to create new frameworks. The candidate should also design a framework for the questionnaire and other assessment. Apply for the job  

Principal Data Scientist at DirecTech Labs

Location: Noida, India DirecTech Labs was created to tap an incredible opportunity for its clients by helping the industry grow through improving experience and satisfaction for consultants. The company uses data to uncover previously invisible patterns of seller and customer behaviour and unprecedented insights into direct sellers at an individual level. Role and Responsibility: As a principal data scientist, the candidate gets a chance to drive the behavioural intelligence that powers DirecTech Labs actionable analytics and global messaging platform with over a million users and 20 million lifecycles in its database. He/she will be a key member of the data science team working to improve current behavioural models, develop new ones, and prove the efficacy and business impact of the company’s global messaging and alerts. Apply for the job  

Data Science-Lead at Wipro

Location: Bengaluru, Karnataka, India Wipro Limited is a leading global information technology, consulting and business process service company. Wipro harnesses the power of cognitive computing, hyper-automation, robotics, cloud, analytics and emerging technologies to help the company’s clients adapt to the digital world ad make them successful. Role and responsibility: The profile demands mandatory knowledge in data science and Python. As a lead, he/she is responsible for managing a small team of analysts, developers, testers or engineering and drive delivery of a small module within a project. Apply for the job  

Data Analyst at Amazon

Location: Bengaluru, Karnataka, India Amazon is the world’s largest online retailer and a prominent cloud services provider. Amazon aims to be earth’s most customer-centric company. Amazon is guided by four principles: customer obsession rather than competitor focus, passion for invention, commitment to operational experience, and long-term thinking. The company is driven by the excitement of building technologies, inventing products, and providing services that change lives. Roles and responsibility: As an analyst, the candidate will work closely with program and product managers to analyze data, identify, areas to improve, define metrics to measure and monitor programs and build end-to-end reporting solutions which help leaders keep an eye on business areas. The candidate will also work closely with internal business teams to extract or mine information from Amazon’s existing systems to create new analysis, and expose data from its group to wider teams in intuitive ways.

40 Questions To Test Your Skill In Python For Data Science

Python is increasingly becoming popular among data science enthusiasts, and for right reasons. It brings the entire ecosystem of a general programming language. So you can not only transform and manipulate data, but you can also create strong pipelines and machine learning workflows in a single ecosystem.

At Analytics Vidhya, we love Python. Most of us use Python as our preferred tool for machine learning. Not only this, if you want to learn Deep Learning, Python clearly has the most mature ecosystem among all other languages.

Learning Python is the first step in your Data Science Journey. Want to know what are the milestones in Data Science Journey and how to achieve them? Check out the complete Data Science Roadmap!

If you are learning Python for Data Science, this test was created to help you assess your skill in Python. This test was conducted as part of DataFest 2023. Close to 1,300 people participated in the test with more than 300 people taking this test.

Below are the distribution scores of the people who took the test:

You can access the final scores here. Here are a few statistics about the distribution.

Mean Score: 14.16

Median Score: 15

Mode Score: 0

Questions & Answers

Question Context 1

Below is the subtitle sample script.

Note: Python regular expression library has been imported as re.

txt = '''450 Okay, but, um, thanks for being with us. 451 But, um, if there's any college kids watching, 452 But, um, but, um, but, um, but, um, but, um, 453 We have to drink, professor. 454 It's the rules. She said "But, um" 455 But, um, but, um, but, um... god help us all. '''

1) Which of the following codes would be appropriate for this task?

A) len(re.findall(‘But, um’, txt))

B) re.search(‘But, um’, txt).count()

C) len(re.findall(‘[B,b]ut, um’, txt))

D) re.search(‘[B,b]ut, um’, txt)).count()

Solution: (C)

You have to find both capital and small versions of “but” So option C is correct.

Question Context 2

Suppose you are given the below string

“””

In order to extract only the domain names from the email addresses from the above string (for eg. “aaa”, “bbb”..) you write the following code:

for i in re.finditer('([a-zA-Z]+)@([a-zA-Z]+).(com)', str):     print i.group(__)

2) What number should be mentioned instead of “__” to index only the domains?

Note: Python regular expression library has been imported as re.

A) 0

B) 1

C) 2

D) 3

Solution: (C)

Question Context 3

Your friend has a hypothesis – “All those people who have names ending with the sound of “y” (Eg: Hollie) are intelligent people.” Please note: The name should end with the sound of ‘y’ but not end with alphabet ‘y’.

Now you being a data freak, challenge the hypothesis by scraping data from your college’s website. Here’s data you have collected.

Name Marks

Andy 0

Mandi 10

Sandy 20

Hollie 18

Molly 19

Dollie 15

You want to make a list of all people who fall in this category. You write following code do to the same:

temp = [] for i in re.finditer(pattern, str):     temp.append(i.group(1))

3) What should be the value of “pattern” in regular expression?

Note: Python regular expression library has been imported as re.

D) None of these

Solution: (B)

You have to find the pattern the end in either “i” or “ie”. So option B is correct.

Question Context 4

Assume, you are given two lists:

a = [1,2,3,4,5]

b = [6,7,8,9]

The task is to create a list which has all the elements of a and b in one dimension.

Output:

a = [1,2,3,4,5,6,7,8,9]

4) Which of the following option would you choose?

A) a.append(b)B) a.extend(b)

C) Any of the above

D) None of these

Solution: (B)

Option B is correct

&nbsp

5) You have built a machine learning model which you wish to freeze now and use later. Which of the following command can perform this task for you?

A) push(model, “file”)B) save(model, “file”)C) dump(model, “file”)D) freeze(model, “file”)

Solution: (C)

Option C is correct

A) push(model, “file”)B) save(model, “file”)C) dump(model, “file”)D) freeze(model, “file”)

Question Context 6

We want to convert the below string in date-time value:

import time str = '21/01/2024' datetime_value = time.strptime(str,date_format)

6) To convert the above string, what should be written in place of date_format?A) “%d/%m/%y”

B) “%D/%M/%Y”

C) “%d/%M/%y”

D) “%d/%m/%Y”

Solution: (D)

Option D is correct

Question Context 7

I have built a simple neural network for an image recognition problem. Now, I want to test if I have assigned the weights & biases for the hidden layer correctly. To perform this action, I am giving an identity matrix as input. Below is my identity matrix:

0, 0, 1]7) How would you create this identity matrix in python?

Note: Library numpy has been imported as np.A) np.eye(3)

B) identity(3)

C) np.array([1, 0, 0], [0, 1, 0], [0, 0, 1])

D) All of these

Option B does not exist (it should be np.identity()). And option C is wrong, because the syntax is incorrect. So the answer is option A

8) To check whether the two arrays occupy same space, what would you do?

I have two numpy arrays “e” and “f”.You get the following output when you print “e” & “f”

print e [1, 2, 3, 2, 3, 4, 4, 5, 6] print f [[1, 2, 3], [2, 3, 4], [4, 5, 6]]

When you change the values of the first array, the values for the second array also changes. This creates a problem while processing the data.

For example, if you set the first 5 values of e as 0; i.e.

print e[:5] 0

the final values of e and f are

print e [0, 0, 0, 0, 0, 4, 4, 5, 6] print f [[0, 0, 0], [0, 0, 4], [4, 5, 6]]

You surmise that the two arrays must have the same space allocated.

A) Check memory of both arrays, if they match that means the arrays are same.

B) Do “np.array_equal(e, f)” and if the output is “True” then they both are same

C) Print flags of both arrays by e.flags and f.flags; check the flag “OWNDATA”. If one of them is False, then both the arrays have same space allocated.

D) None of these

Solution: (C)

Option C is correct

Question Context 9

Suppose you want to join train and test dataset (both are two numpy arrays train_set and test_set) into a resulting array (resulting_set) to do data processing on it simultaneously. This is as follows:

train_set = np.array([1, 2, 3]) test_set = np.array([[0, 1, 2], [1, 2, 3]])

9) How would you join the two arrays?

Note: Numpy library has been imported as npA) resulting_set = train_set.append(test_set)

B) resulting_set = np.concatenate([train_set, test_set])

C) resulting_set = np.vstack([train_set, test_set])

D) None of these

Solution: (C)

Both option A and B would do horizontal stacking, but we would like to have vertical stacking. So option C is correct

Question Context 10

Suppose you are tuning hyperparameters of a random forest classifier for the Iris dataset.

Sepal_length Sepal_width Petal_length Petal_width Species 4.6 3.2 1.4 0.2 Iris-setosa 5.3 3.7 1.5 0.2 Iris-setosa 5.0 3.3 1.4 0.2 Iris-setosa 7.0 3.2 4.7 1.4 Iris-versicolor 6.4 3.2 4.5 1.5 Iris-versicolor

10) What would be the best value for “random_state (Seed value)”?

A) np.random.seed(1)B) np.random.seed(40)

C) np.random.seed(32)

D) Can’t say

Solution: (D)

There is no best value for seed. It depends on the data.

Question 11

While reading a csv file with numpy, you want to automatically fill missing values of column “Date_Of_Joining” with date “01/01/2010”.

Name Age Date_Of_Joining Total_Experience Andy 20 01/02/2013 0 Mandy 30 01/05/2014 10 Sandy 10 0 Bandy 40 01/10/2009 20

11) Which command will be appropriate to fill missing value while reading the file with numpy? 

Note: numpy has been imported as np

temp = np.genfromtxt(filename, filling_values=filling_values)

temp = np.loadtxt(filename, filling_values=filling_values)

temp = np.gentxt(filename, filling_values=filling_values)

D) None of these

Solution: (A)

Option A is correct

12) How would you import a decision tree classifier in sklearn?

A) from sklearn.decision_tree import DecisionTreeClassifierB) from sklearn.ensemble import DecisionTreeClassifier

C) from chúng tôi import DecisionTreeClassifier

D) None of these

Solution: (C)

Option C is correct

Note: Library StringIO has been imported as StringIO.

A) data = pd.read_csv(source) B) data = pd.read_csv(source)

C)

data = pd.read_csv(source)

D) None of these

Solution: (A)

Option A is correct

Question Context 14

Imagine, you have a dataframe train file with 2 columns & 3 rows, which is loaded in pandas.

import pandas as pd

train  = pd.DataFrame({'id':[1,2,4],'features':[["A","B","C"],["A","D","E"],["C","D","F"]]})

Now you want to apply a lambda function on “features” column: 

train['features_t'] = train["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))

14) What will be the output of following print command?

print train['features_t']

A)

2    C D F

B)

0    AB

1    ADE

2    CDF

D) None of these

Solution: (A)

Option A is correct

Question Context 15

We have a multi-class classification problem for predicting quality of wine on the basis of its attributes. The data is loaded in a dataframe “df”

fixed acidity volatile acidity citric acid residual sugar chlorides free sulfur dioxide total sulfur dioxide density pH sulphates Alcohol quality

0 7.4 0.70 0.00 1.9 0.076 11 34 0.9978 3.51 0.56 9.4 5

1 7.8 0.88 0.00 2.6 0.098 25 67 0.9968 3.20 0.68 9.8 5

2 7.8 0.76 0.04 2.3 0.092 15 54 0.9970 3.26 0.65 9.8 5

3 11.2 0.28 0.56 1.9 0.075 17 60 0.9980 3.16 0.58 9.8 6

4 7.4 0.70 0.00 1.9 0.076 11 34 0.9978 3.51 0.56 9.4 5

The quality column currently has values 1 to 10, but we want to substitute this by a binary classification problem. You want to keep the threshold for classification to 5, such that if the class is greater than 5, the output should be 1, else output should be 0.

15) Which of the following codes would help you perform this task?

Note: Numpy has been imported as np and dataframe is set as df.A)

Y = df[quality].values

B)

Y = df[quality].values()

C)

Y = df[quality]

D)None of these

Solution: (A)

Option A is correct

Question Context 16

Suppose we make a dataframe as

df = pd.DataFrame(['ff', 'gg', 'hh', 'yy'],              [24, 12, 48, 30],               columns = ['Name', 'Age'])

16) What is the difference between the two data series given below?

df[‘Name’] and

df.loc[:, ‘Name’]

Note: Pandas has been imported as pd

A) 1 is view of original dataframe and 2 is a copy of original dataframe.

B) 2 is view of original dataframe and 1 is a copy of original dataframe.

C) Both are copies of original dataframe.

D) Both are views of original dataframe

Solution: (B)

Option B is correct. Refer the official docs of pandas library.

Question Context 17

Consider a function “fun” which is defined below:

def fun(x):     x[0] = 5     return x

Now you define a list which has three numbers in it.

g = [10,11,12]

17) Which of the following will be the output of the given print statement:

print  fun(g), g

A) [5, 11, 12] [5, 11, 12]B) [5, 11, 12] [10, 11, 12]

C) [10, 11, 12] [10, 11, 12]

D) [10, 11, 12] [5, 11, 12]

Solution: (A)

Option A is correct

Question Context 18

Sigmoid function is usually used for creating a neural network activation function. A sigmoid function is denoted as

def sigmoid(x):   return (1 / (1 + math.exp(-x)))

18) It is necessary to know how to find the derivatives of sigmoid, as it would be essential for backpropagation. Select the option for finding derivative?

A)

import scipy Dv = scipy.misc.derive(sigmoid)

 B)

from sympy import * x = symbol(x) y = sigmoid(x) Dv = y.differentiate(x)

C)

Dv = sigmoid(x) * (1 - sigmoid(x))

D) None of these

Solution: (C)

Option C is correct

Question Context 19

Suppose you are given a monthly data and you have to convert it to daily data.

For example,

For this, first you have to expand the data for every month (considering that every month has 30 days)

19) Which of the following code would do this?

Note: Numpy has been imported as np and dataframe is set as df.A) new_df = pd.concat([df]*30, index = False)

B) new_df = pd.concat([df]*30, ignore_index=True)

C) new_df = pd.concat([df]*30, ignore_index=False)

D) None of these

Solution: (B)

Option B is correct

Context: 20-22

Suppose you are given a dataframe df.

What will be the output of print statement below?

print df.columns

Note: Pandas library has been imported as pd.

C) Error

D) None of these

Solution: (B)

Option B is correct

Context: 20-22

Suppose you are given a data frame df.

C) We cannot perform this task since dataframe and dictionary are different data structures

D) None of these 

Solution: (A)

Option A is correct

22) In above dataframe df. Suppose you want to assign a df to df1, so that you can recover original content of df in future using df1 as below.

df1 = df

Now you want to change some values of “Count” column in df.

Which of the following will be the right output for the below print statement?

print df.Count.values,df1.Count.values

Note: Pandas library has been imported as pd.

A) [200 200 300 400 250] [200 200 300 400 250]

B) [100 200 300 400 250] [100 200 300 400 250]

C) [200 200 300 400 250] [100 200 300 400 250]

D) None of these

Solution: (A)

Option A is correct

You copy whole code in an Ipython / Jupyter notebook, with each code line as a separate block and write magic function %%timeit in each block

A) 1 & 2

B) 1,2 & 3

C) 1,2 & 4

D) All of the above 

Solution: (C)

Option C is correct

24) How would you read data from the file using pandas by skipping the first three lines?

Note: pandas library has been imported as pd In the given file (email.csv), the first three records are empty.

,,, ,,, ,,, Email_Address,Nickname,Group_Status,Join_Year [email protected],aa,Owner,2014 [email protected],bb,Member,2024 [email protected],cc,Member,2024 [email protected],dd,Member,2024

A) read_csv(‘email.csv’, skip_rows=3)

B) read_csv(‘email.csv’, skiprows=3)

C) read_csv(‘email.csv’, skip=3)

D) None of these

Solution: (B)

Option B is correct

25) What should be written in-place of “method” to produce the desired outcome?

Given below is dataframe “df”:

Now, you want to know whether BMI and Gender would influence the sales.

For this, you want to plot a bar graph as shown below:

The code for this is:

var = df.groupby(['BMI','Gender']).Sales.sum() var.unstack().plot(kind='bar', method,  color=['red','blue'], grid=False)

A) stacked=True

B) stacked=False

C) stack=False

D) None of these

Solution: (A)

It’s a stacked bar chart.

26) Suppose, you are given 2 list – City_A and City_B.

City_A = [‘1′,’2′,’3′,’4’]

City_B = [‘2′,’3′,’4′,’5’]

In both cities, some values are common. Which of the following code will find the name of all cities which are present in “City_A” but not in “City_B”.

A) [i for i in City_A if i not in City_B]B) [i for i in City_B if i not in City_A]

C) [i for i in City_A if i in City_B]

D) None of these

Solution: (A)

Option A is correct

Question Context 27

Suppose you are trying to read a file “temp.csv” using pandas and you get the following error.

Traceback (most recent call last): UnicodeEncodeError: 'ascii' codec can't encode character.

27) Which of the following would likely correct this error?

Note: pandas has been imported as pdA) pd.read_csv(“temp.csv”, compression=’gzip’)

B) pd.read_csv(“temp.csv”, dialect=’str’)

C) pd.read_csv(“temp.csv”, encoding=’utf-8′)

D) None of these

Solution: (C)

Option C is correct, because encoding should be ‘utf-8’

28) Suppose you are defining a tuple given below:

tup = (1, 2, 3, 4, 5 )

Now, you want to update the value of this tuple at 2nd index to 10. Which of the following option will you choose?

A) tup(2) = 10

B) tup[2] = 10

C) tup{2} = 10

D) None of these

Solution: (D)

A tuple cannot be updated.

C) Both A and B

D) None of these

Solution: (C)

Option C is correct

Question Context 30

Suppose you are given the below web page

html_doc = “”” ...  “””

30) To read the title of the webpage you are using BeautifulSoup. What is the code for this? 

Hint: You have to extract text in title tag

print soup.title.name

print soup.title.string

print soup.title.get_text

None of these

Solution: (B)

Question Context 31

Imagine, you are given a list of items in a DataFrame as below.

D = [‘A’,’B’,’C’,’D’,’E’,’AA’,’AB’]

Now, you want to apply label encoding on this list for importing and transforming, using LabelEncoder.

from sklearn.preprocessing import LabelEncoder le = LabelEncoder()

31) What will be the output of the print statement below ?

print le.fit_transform(D)

array([0, 2, 3, 4, 5, 6, 1])

array([0, 3, 4, 5, 6, 1, 2])

array([0, 2, 3, 4, 5, 1, 6])

Any of the above

Solution: (D)

Option D is correct

32) Which of the following will be the output of the below print statement?

print chúng tôi == np.nan

Assume, you have defined a data frame which has 2 columns.

import numpy as np df = pd.DataFrame({'Id':[1,2,3,4],'val':[2,5,np.nan,6]})

3    False

3    False

3    True

D) None of these

Solution: (A)

Option A is correct

33) Suppose the data is stored in HDFS format and you want to find how the data is structured. For this, which of the following command would help you find out the names of HDFS keys?

Note: HDFS file has been loaded by h5py as hf.

A) hf.key()

B) hf.key

C) hf.keys()

D) None of these

Solution: (C)

Option C is correct

Question Context 34

You are given reviews for movies below:

reviews = [‘movie is unwatchable no matter how decent the first half is  . ‘, ‘somewhat funny and well  paced action thriller that has jamie foxx as a hapless  fast  talking hoodlum who is chosen by an overly demanding’, ‘morse is okay as the agent who comes up with the ingenious plan to get whoever did it at all cost .’]

Your task is to find sentiments from the review above. For this, you first write a code to find count of individual words in all the sentences.

counts = Counter() for i in range(len(reviews)): for word in reviews[i].split(value): counts[word] += 1

34)What value should we split on to get individual words?

‘ ‘

‘,’

‘.’

None of these

Solution: (A)

Option A is correct

35) How to set a line width in the plot given below?

For the above graph, the code for producing the plot was

import matplotlib.pyplot as plt plt.plot([1,2,3,4]) plt.show()

In line two, write plt.plot([1,2,3,4], width=3)

In line two, write plt.plot([1,2,3,4], line_width=3

In line two, write plt.plot([1,2,3,4], lw=3)

None of these

Solution: (C)

Option C is correct

36) How would you reset the index of a dataframe to a given list? The new index is given as:

new_index=[‘Safari’,’Iceweasel’,’Comodo Dragon’,’IE10′,’Chrome’]

Note: df is a pandas dataframe

response_time

Firefox 200 0.04

Chrome 200 0.02

Safari 404 0.07

IE10 404 0.08

Konqueror 301 1.00

A) df.reset_index(new_index,)

B) df.reindex(new_index,)

C) df.reindex_like(new_index,)

D) None of these

Solution: (A)

Option A is correct

37) Determine the proportion of passengers survived based on their passenger class.

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked

0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S

1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th… female 38.0 1 0 PC 17599 71.2833 C85 C

2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S

3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S

4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S

crosstab(df_train[‘Pclass’], df_train[‘Survived’])

proportion(df_train[‘Pclass’], df_train[‘Survived’])

crosstab(df_train[‘Survived’], df_train[‘Pclass’])

None of these

Solution: (A)

Option A is correct

38) You want to write a generic code to calculate n-gram of the text. The 2-gram of this sentence would be [[“this, “is”], [“is”, “a”], [“a, “sample”], [“sample”, “text”]]

Which of the following code would be correct?

‘this is a sample text’.

output = [] return output

output = [] return output

output = [] return output

None of these

Solution: (B)

Option B is correct

39) Which of the following code will export dataframe (df) in CSV file, encoded in UTF-8 after hiding index & header labels.

df_1.to_csv(‘../data/file.csv’,encoding=’utf-8′,index=True,header=False)

df_1.to_csv(‘../data/file.csv’,encoding=’utf-8′,index=False,header=True)

df_1.to_csv(‘../data/file.csv’,encoding=’utf-8′,index=False,header=False)

None of these

Solution: (C)

Option C is correct

40) Which of the following is a correct implementation of mean squared error (MSE) metric?

Note: numpy library has been imported as np.

return np.mean((np.square(real_target) – np.square(predicted_target)))

return np.mean((real_target – predicted_target)**2)

return np.sqrt(np.mean((np.square(real_target) – np.square(predicted_target))))

None of the above

Solution: (B)

Option B is correct

End Notes

If you are learning Python, make sure you go through the test above. It will not only help you assess your skill. You can also see where you stand among other people in the community. If you have any questions or doubts, feel free to post them below.

Related

The Complete Guide To Successful Branding In 2023

Successful branding never just happens. Give the best experience to your consumers when they make (or consider) a purchase. Learn the following tips to get there.

The Complete Guide to Successful Branding in 2023

Products represent a lot more than simply something that customers buy to satisfy needs and desires. Oftentimes, they are also a means for businesses to promote particular expertise to shoppers. That is precisely why firms go out of the methods to cultivate a particular new image.

According to one study, less than 35 percent of consumers actually trust the brands that they buy from.

1. Branding in a Nutshell

Before you build a brand, you want to choose what a brand actually is. As you may believe that it’s hardly more than the title and emblem of a business, in addition, it comprises the company’s voice and attitude. Consumers interact with brands if they buy into them or not, so they are building an experience every time they have some contact with you.

Branding is the procedure of employing a particular attribute to a company in the hopes that customers will associate a specific attribute with said firm. A high degree of brand awareness results in an organization’s image being viewed as popular. Consumers can not look at buying goods and services out of a particular business if they don’t know it is present.

Because of this, you’re going to want to concentrate on building brand awareness before you do anything else. As you may believe that a massive publicity stunt would be the very best means to accomplish this, that may not be the situation.

According to specialists from DesignBro, working with a company that may help you build a new identity from the ground up could possibly be important. Consider the number of companies you are able to recall-based only on their comparatively straightforward glossy logos. Probably, you are able to visualize quite a couple.

Remember that visual design is simply some of everything you want. You will want to set up a target market so that you understand what your group is assumed to be aiming for.

2. Honing in On a Single Target

Since branding can potentially lead to trust, you’ll want to better understand who your brand will speak to. Take some time to figure out what kind of consumer needs your product and figure out what other problems they might need solutions to.

Some specialists construct a research-based outline of a prototypical client they predict a buyer character. As you do not have to go this way, it will help to have a better grasp of what your clients need before you proceed any further.

Set a mission statement that spells out the reason you established your small business. Yet more, you do not have to go to this extreme. You do, but you wish to construct a brand that you genuinely believe in and use your personal beliefs to form the messages which you feed your prospective customers.

Hard amounts, if you’re able to find them, would be the ideal method to have a better picture of that may eventually purchase into your own brand. Startup companies frequently make the mistake of dismissing market evaluation reports and dip themselves deep into debt in the procedure.

Pay careful attention to whatever information you may find about your possible customer. If you can not say explicitly what type of person may want to purchase your goods, then it’s easy to envision that nobody could.

That may sound harsh, but it is true. Your brand is designed to give aid to individuals no matter if they understand they need assistance in some manner. Consider all the many surfer-themed fashion brands which you have seen come into vogue during the past 10-15 decades.

Just how a lot of those ever brought a marketplace of hardcore surfers? The solution is probably not one of these, but it does not matter since they could supply a laid-back image to folks who desired a means to escape from their everyday lives. You will want to locate a hidden want such as that and tap it to make sure that your brand reaches the best number of people possible.

Also read: Top 10 Helpful GitHub Storage For Web Developers

3. Defining Your Company’s Values

As customer confidence in important companies has been eroding, you have to work out a method to make people wish to think in the picture which you are promoting. That is hard if you do not think about your brand. Be certain that you clearly outline what you believe in and the way you believe that your brand stands apart from everybody else in the business section you cope with.

They are in a position to place themselves as a much healthier alternative to mass-market businesses. The ones that sell products which promise to be better for the environment also has done well to place themselves apart from the rest.

Considering the increased emphasis on solitude, you may wish to think about boosting your institution’s dedication to protecting your customers. This is particularly true when you are running a committed social networking marketing effort.

4. Deploying Your Finished Brand Image

Maybe the first thing to do if you finalize your new image is accomplished that no manufacturer is ever really finished. The general public perception of your organization will probably continue to evolve in the long term. This means you are going to want to utilize your branding substances on everything your business puts out. Ensure that your packaging and goods are all branded suitably.

On the flip side, you are going to want to go all out by means of your brand image. Each and every profile photograph and piece of this cover artwork on your website and societal networking accounts must reflect your own brand. Perhaps you will wish to set your logo as your own profile picture because this will make it much easier for your clients to recognize your own firm.

All your articles and captions must represent the exceptional voice you’ve came up with for your own brand. If your manufacturer is snarky, then you will want your articles to reflect that. As you do not wish to be argumentative for debate’s sake, there is no reason why you can not distinguish your brand by showcasing your distinctive sense of humor.

Also read: Top 10 IoT Mobile App Development Trends to Expect in 2023

5. Framing Advertisements Through Your Brand’s Image

Those beginning a committed email marketing campaign are going to want to concentrate on producing a first impression, for example, so they can be certain any follow-ups will probably be well received.

Ask yourself the way your manufacturer would present itself and then use the response to that question for a framework of reference when replying to your potential customers. Some business experts have used this approach to think of private brands, which ought to help illustrate exactly how successful these techniques are.

Think about what someone might say about your company after they met your brand for the first time. You may want them to describe it using some specific words. As soon as you know what kind of adjectives you’d like to hear consumers using to talk about your brand, you’ll be in a much better position to figure out the best way to reach them.

The Complete Guide To Linkedin Ads In 2023

Among the platform’s 690 million+ members, four out of five members have the power to impact business decisions. These movers and shakers also have 2x the buying power of typical online audiences.

Sponsored Content

Source: LinkedIn

Sponsored Messaging

While 89% of consumers prefer that businesses stay in touch via messaging, only 48% of companies currently interact with customers and prospects this way.

Text Ads

Considering 58% of marketers say that improving lead generation is one of their top digital marketing goals, LinkedIn Text Ads can be a way to cast a wide net on a budget.

Dynamic Ads

Dynamic Ads run in the right rail of LinkedIn and speak to audiences directly through personalization. When a Dynamic Ad pops up in a member’s feed, their own personal details, such as their photo, employer’s name and job title, are reflected back to them.

Source: LinkedIn

LinkedIn ad objectives

Businesses can work through all three stages of a sales funnel, from awareness to conversion.

The three main types of objectives are broken down below.

Through these impression-based campaigns, you can also gain more followers, increase views, and spark greater engagement.

Website visits: Get more eyeballs on your website and landing pages.

Video views: Share your business story, your latest product, or a day-in-the-life via video.

They can help meet these three objectives:

Website conversions: Inspire more website visitors to download an ebook, sign up for a newsletter, or purchase a product.

Job applicants: Spread the word about your company’s latest job opening with a job post.

LinkedIn ad formats

To help meet your ad objectives, LinkedIn has 10 different ad formats to choose from.

This section will break down each ad format and explain what goals each ad can help you achieve. We’ll also share LinkedIn ad examples and ad specs.

Goals: Brand awareness, website visits, engagement, website conversions, and lead generation.

LinkedIn carousel ad specs:

Name of ad: Up to 255 characters

Introductory text: Up to 150 characters to avoid shortening on some devices (255 total character limit)

Cards: Between two and 10 cards.

Max file size: 10 MB

Max image dimension: 6012 x 6012px

Rich media formats: JPG, PNG, GIF (non-animated only)

No more than two lines in each card’s headline text

Source: LinkedIn

Once you start a conversation, your audience can select a response that speaks most to them. This type of ad lets you showcase products and services while also encouraging event or webinar signups.

Goals: Brand awareness, website visits, engagement, website conversions, and lead generation.

LinkedIn conversation ad specs:

Banner creative (optional and for desktop only): Up to 300 x 250px. JPEG or PNG.

Custom footer and terms and conditions (only): Up to 2,500 characters

Introductory message: Up to 500 characters

Image (optional): 250 x 250px using either JPEG or PNG

CTA text: Up to 25 characters

CTA buttons per message: Up to five buttons

Message text: Up to 500 characters

Source: LinkedIn

Goals: Brand awareness, website visits, and engagement.

LinkedIn follower ad specs:

Ad description: Up to 70 characters

Ad headline: Choose a pre-set option or write up to 50 characters

Company name: Up to 25 characters

Ad image: Preferably 100 x 100px for JPG or PNG

Source: LinkedIn

Goals: Brand awareness, website visits, engagement, lead generation, and job applicants.

LinkedIn spotlight ad specs:

Ad description: Up to 70 characters

Ad headline: Up to 50 characters

Company name: Up to 25 characters

Image: Preferred size is 100 x 100px for JPG or PNG

CTA: Up to 18 characters

Custom background (optional): Must be exactly 300 x 250px and 2MB or less

Source: LinkedIn

Goals: Job applicants and website visits.

LinkedIn job ad specs:

Company name: Up to 25 characters

Company logo: 100 x 100px is recommended

Ad headline: Up to 70 characters or the option to choose a pre-set headline

CTA: Up to 44 characters if custom text; pre-set options available

Source: LinkedIn

Lead gen forms

You can learn more about lead gen forms here:

Goals: Lead generation

LinkedIn lead gen form specs:

Form name: Up to 256 characters

Headline: Up to 60 characters

Details: Up to 70 characters to avoid truncation (Up to 160 characters total)

Privacy policy text (optional): Up to 2,000 characters

Sources: LinkedIn

This type of ad lets you send a direct message to your audiences’ inbox, complete with a CTA.

Goals: Website visits, website conversions, lead generation.

LinkedIn message ad specs:

Message subject: Up to 60 characters

CTA button copy: Up to 20 characters

Message text: Up to 1,500 characters

Custom terms and conditions: Up to 2,500 characters

Banner creative: JPEG, PNG, GIF (non animated). Size: 300 x 250px

Source: LinkedIn

Goals: Brand awareness, website visits, engagement, website conversions, lead generation and job applicants

LinkedIn single image ad specs:

Name of ad (optional): Up to 225 characters

Introductory text: Up to 150 characters

Destination URL: Up to 2,000 characters for the destination link.

Ad image: A JPG, GIF or PNG file 5MB or smaller; the maximum image size is 7680 x 7680 pixels.

Headline: Up to 70 characters to avoid shortening (but can use up to 200 characters)

Description: Up to 100 characters to avoid shortening (but can use up to 300 characters)

Source: LinkedIn

Goals: Job applications

LinkedIn job ad specs:

Name of ad: Up to 255 characters

Introductory text: Up to 150 characters to avoid shortening of text (desktop max of 600 characters); any legally required language must go here

Source: LinkedIn

Goals: Brand awareness, website visits and website conversions.

LinkedIn ad specs:

Image: 100 x 100px with a JPG or PNG 2MB or less

Headline: Up to 25 characters

Description: Up to 75 characters

Source: LinkedIn

Goals: Video views

LinkedIn video ad specs:

Name of ad (optional): Up to 225 characters

Introductory text (optional): Up to 600 characters

File size: 75KB to 200MB

Frame rate: Less than 30 frames per seconds

Width: 640 to 1920 pixels

Height: 360 to 1920 pixels

Aspect ratio: 1.778 to 0.5652

Source: LinkedIn

How to create a LinkedIn ad in 9 steps

To create your own LinkedIn ad, follow the steps below:

Step 1: Create a LinkedIn Page if you don’t have one already

This is required to create Sponsored Content and Sponsored Messaging Ads. If you need help setting one up, read our guide on LinkedIn for business.

Source: LinkedIn

Step 2: Log in to Campaign Manager or create an account.

Source: LinkedIn

Step 3: Select your ad objective

Think about what type of action you want to inspire among your audience.

Source: LinkedIn

Step 4: Choose your target audience

First, you must choose a location, and then you have the option of adding job title, company name, industry type and personal or professional interests.

If it’s your first campaign, LinkedIn recommends a target audience of at least 50,000 for Sponsored Content and Text Ads. For Message Ads, 15,000 is best.

Source: LinkedIn

You also have the option of connecting with people you already know through Matched Audiences. You can do this by retargeting people who’ve visited your website or uploading a list of email contacts.

Learn more about Matched Audiences here:

Step 5: Select an ad format

Source: LinkedIn

Step 6: Create your budget and schedule

Campaign Manager will provide a budget range based on other competing bids for your ideal audience.

The initial 2-4 weeks are typically considered a learning experience to figure out what works (or doesn’t). For testing, LinkedIn recommends a daily budget of at least $100 or a monthly budget of $5,000.

Source: LinkedIn

Step 7: Start building your ad

If you opt for Sponsored Content or Text Ads, the Campaign Manager will share previews so you can get a sense of the final look of your ad. In the case of Message Ads, you’ll be able to send yourself a test message.

Step 8: Provide payment information

Before you can debut your ad to the world, you’ll have to provide payment information. Once that’s done, you’re ready to launch!

Source: LinkedIn

Step 9: Measure performance

Source: LinkedIn

Last but certainly not least, here’s the criteria LinkedIn itself says are vital to crafting a successful ad campaign on the platform.

Figure out your target audience

You can then further refine your target audience with company details (e.g. industry or company size), demographics, education, job experience and interests.

You can also A/B test campaigns with different targeting criteria, such as skills versus job titles, to learn which audiences connect better with your brand.

Craft your ad copy around a succinct, clear call to action

Your readers are busy. They need someone to spell out exactly what they should do next, otherwise, they might miss out on signing up for that career-boosting webinar or purchasing a new product that could simplify their life. Just make sure that your CTA matches the objective you initially selected.

Some effective CTA’s include “Register Now” or “Sign Up Today!”

Read Hootsuite’s blog to learn more tips about creating captivating CTAs.

Choose the right content

LinkedIn can boost your content so it finds the right audience, but that won’t keep people glued to the screen.

Try the techniques below to keep audiences hanging onto every word you say.

Sponsored Content:

Repurpose content from your blog, website and social media channels.

Use video, audio or other rich media elements.

Develop an emotional connection by sharing human interest stories.

Do more than just share trending news. Add your insights into the mix to show off your brand’s thought leadership.

Sponsored Messaging:

If encouraging brand consideration, share blog posts, webinars, or industry trends and analysis.

Text Ads:

Instead of including an object or logo, opt for a profile image when possible.

Video Ads:

According to LinkedIn, videos under 30 seconds saw a 200% lift in view completion rates, so keep them short and sweet.

Design videos for sound-off viewing and add subtitles.

Don’t save the best for last. Viewers drop off after the first 10 seconds.

Carousel Ads:

Use 3-5 cards to start, and test adding more cards later.

Create a carousel of content that speaks to a similar theme or break down a large piece of content into carousel cards.

Use visual storytelling to pique your audience’s interest.

Each carousel card description should include a CTA and clear, direct messaging.

Dynamic Ads:

Skip the brevity and be descriptive as possible in the main ad headline and text.

Include one clear message and CTA in each ad.

Promote organic posts as sponsored content

When time is of the essence, hop on Hootsuite to promote organic posts as sponsored content. You can target audiences based on their location, interests, or professional information.

Source: Hootsuite

Request a Demo

Easily plan, manage and analyze organic and paid campaigns from one place with Hootsuite Social Advertising. See it in action.

Complete Guide To Python Stopiteration

Introduction to Python StopIteration

The following article outlines Python StopIteration as we know the topic ‘iterator’ and ‘iterable’ in Python. The basic idea of what the ‘iterator’ is? An iterator is an object that holds a value (generally a countable number) that is iterated upon. Iterator in Python uses the __next__() method to traverse to the next value. To tell that no more deals need to be traversed by the __next__() process, a StopIteration statement is used. Programmers usually write a terminating condition inside the __next__() method to stop it after reaching the specified state.

Syntax of Python StopIteration

When the method used for iterators and generators completes a specified number of iterations, it raises the StopIteration exception. It’s important to note that Python treats raising StopIteration as an exception rather than a mistake. Like how Python handles other exceptions, this exception can be handled by catching it. This active handling of the StopIteration exception allows for proper control and management of the iteration process, ensuring that the code can gracefully handle the termination of the iteration when required.

The general syntax of using StopIteration in if and else of next() method is as follows:

class classname: def __iter__(self): … … #set of statements return self; def __next__(self): if …. #condition till the loop needs to be executed …. #set of statements that needs to be performed till the traversing needs to be done return … else raise StopIteration #it will get raised when all the values of iterator are traversed How StopIteration works in Python?

It is raised by the method next() or __next__(), a built-in Python method to stop the iterations or to show that no more items are left to be iterated upon.

We can catch the StopIteration exception by writing the code inside the try block, catching the exception using the ‘except’ keyword, and printing it on screen using the ‘print’ keyword.

The following () method in both generators and iterators raises it when no more elements are present in the loop or any iterable object.

Examples of Python StopIteration

Given below are the examples mentioned:

Example #1

Stop the printing of numbers after 20 or printing numbers incrementing by 2 till 20 in the case of Iterators.

Code:

class printNum: def __iter__(self): self.z = 2 return self def __next__(self): if self.z <= 20: #performing the action like printing the value on console till the value reaches 20 y = self.z self.z += 2 return y else: raise StopIteration #raising the StopIteration exception once the value gets increased from 20 obj = printNum() value_passed = iter(obj) for u in value_passed: print(u)

Output:

Explanation:

In the above example, we use two methods, namely iter() and next(), to iterate through the values. The next() method utilizes if and else statements to check for the termination condition of the iteration actively.

If the iterable value is less than or equal to 20, it continues to print those values at the increment of 2. Once the value exceeds 20, the next() method raises a StopIteration exception.

Example #2

Finding the cubes of number and stop executing once the value becomes equal to the value passed using StopIteration in the case of generators.

Code:

def values(): #list of integer values with no limits x = 1 #initializing the value of integer to 1 while True: yield x x+= 1 def findingcubes(): for x in values(): yield x * x *x #finding the cubes of value ‘x’ def func(y, sequence): sequence = iter(sequence) output = [ ] #creating an output blank array try: for x in range(y): #using the range function of python to use for loop output.append(next(sequence)) #appending the output in the array except StopIteration: #catching the exception pass return output print(func(5, findingcubes())) #passing the value in the method ‘func’

Output:

Explanation:

In the above example, we find the cubes of numbers from 1 to the number passed in the function. We generate multiple values at a time using generators in Python, and to stop the execution once the value reaches the one passed in the function, we raise a StopIteration exception.

We create different methods serving their respective purposes, such as generating the values, finding the cubes, and printing the values by storing them in the output array. The program uses basic Python functions like range and append, which should be clear to the programmer in the initial stages of learning.

How to Avoid StopIteration Exception in Python?

As seen above StopIteration is not an error in Python but an exception and is used to run the next() method for the specified number of iterations. Iterator in Python uses two methods, i.e. iter() and next().

The next() method raises a StopIteration exception when the next() method is called manually.

The best way to avoid this exception in Python is to use normal looping or use it as a normal iterator instead of writing the next() method repeatedly.

Otherwise, if not able to avoid StopIteration exception in Python, we can simply raise the exception in the next() method and catch the exception like a normal exception in Python using the except keyword.

Conclusion

As discussed above in the article, it must be clear to you what is the StopIteration exception and in which condition it is raised in Python. StopIteration exception could be an issue to deal with for the new programmers as it can be raised in many situations.

Recommended Articles

This is a guide to Python StopIteration. Here we discuss how StopIteration works in Python and how to avoid StopIteration exceptions with programming examples. You may also have a look at the following articles to learn more –

Update the detailed information about Complete Guide To Moment Generating Functions In Statistics For Data Science on the Katfastfood.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!