FAQ: Mode - Mode

This community-built FAQ covers the “Mode” exercise from the lesson “Mode”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Statistics With Python

FAQs on the exercise Mode

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

import numpy as np
from scipy import stats

Set first_ten_authors equal to their ages

first_ten_authors = np.array([29, 49, 42, 43, 32, 38, 37, 41, 27, 27])

Save the mode value to mode_age

mode_age = stats.mode(first_ten_authors)
print(“mode_age =”,mode_age[0][0])
print(“The type of mode_age=”,type(mode_age))

Save the count of authors with the mode age

mode_count = list(mode_age[1])[0]
print(“the type of mode_count =”, type(mode_count))

Print the sorted array and median value

print("The ages of the first ten authors is: " + str(first_ten_authors))
print("The mode of the first ten authors is: " + str(mode_age[0][0]))
print("The number of authors who were " + str(mode_age[0][0]) + " when their book was published is " + str(mode_count))
##############################333
mode_age = 27 The type of mode_age= <class ‘scipy.stats.stats.ModeResult’> the type of mode_count = <class ‘numpy.int64’> The ages of the first ten authors is: [29 49 42 43 32 38 37 41 27 27] The mode of the first ten authors is: 27 The number of authors who were 27 when their book was published is 2
##################
I get the correct answer! But I am marked wrong and not allowed to proceed!

I wanted to ask about this too…I got a Proceed/Check-mark, but I wanted to ask about whether we were just supposed to enter the value that we could see by inspection (some questions have us do that), to compute the mode programmatically, or to call into another library besides numpy.

For the record, I used this:

Save the mode value to mode_age

modehash = {}
for age in first_ten_authors:
modehash[age] = modehash.get(age, 0) + 1
mode_count = 0
for (key, val) in modehash.items():
if (val > mode_count):
mode_count = val
mode_age = key

(the first loop loads the hash. the second counts the winner)