官术网_书友最值得收藏!

Calculating mode using the SciPy package

Finally, let's look at mode. We will just generate a bunch of random integers, 500 of them to be precise, that range between 18 and 90. We're going to create a bunch of fake ages for people.

ages = np.random.randint(18, high=90, size=500) 
ages 

Your output will be random, but should look something like the following screenshot:

Now, SciPy, kind of like NumPy, is a bunch of like handy-dandy statistics functions, so we can import stats from SciPy using the following syntax. It's a little bit different than what we saw before.

from scipy import stats 
stats.mode(ages) 

The code means, from the scipy package import stats, and I'm just going to refer to the package as stats, Tha means that I don't need to have an alias like I did before with NumPy, just different way of doing it. Both ways work. Then, I used the stats.mode function on ages, which is our list of random ages. When we execute the above code, we get the following output:

Out[11]: ModeResult(mode=array([39]), count=array([12])) 

So in this case, the actual mode is 39 that turned out to be the most common value in that array. It actually occurred 12 times.

Now if I actually create a new distribution, I would expect a completely different answer because this data really is completely random what these numbers are. Let's execute the above code blocks again to create a new distribution.

ages = np.random.randint(18, high=90, size=500) 
ages 
from scipy import stats 
stats.mode(ages) 

The output for randomizing the equation is as distribution is as follows:

Make sure you selected that code block and then you can hit the play button to actually execute it.

In this case, the mode ended up being the number 29, which occurred 14 times.

Out[11]: ModeResult(mode=array([29]), count=array([14])) 

So, it's a very simple concept. You can do it a few more times just for fun. It's kind of like rolling the roulette wheel. We'll create a new distribution again.

There you have it, mean, median, and mode in a nutshell. It's very simple to do using the SciPy and NumPy packages.

主站蜘蛛池模板: 高安市| 察隅县| 霞浦县| 陆良县| 田阳县| 新巴尔虎右旗| 望谟县| 九龙县| 渝中区| 上饶市| 张家川| 英吉沙县| 海南省| 手游| 长葛市| 丹寨县| 新民市| 讷河市| 安仁县| 府谷县| 赤城县| 辽阳市| 万荣县| 浏阳市| 大冶市| 唐河县| 开平市| 秦皇岛市| 错那县| 仙桃市| 新民市| 岳阳市| 无为县| 石家庄市| 措美县| 锡林郭勒盟| 河东区| 阳信县| 绥江县| 阳原县| 肇源县|