- Hands-On Data Science and Python Machine Learning
- Frank Kane
- 362字
- 2021-07-15 17:15:08
Calculating mean using the NumPy package
What we're going to do is create some fake income data, getting back to our example from the previous section. We're going to create some fake data where the typical American makes around $27,000 a year in this example, we're going to say that's distributed with a normal distribution and a standard deviation of 15,000. All numbers are completely made up, and if you don't know what normal distribution and standard deviation means yet, don't worry. I'm going to cover that a little later in the chapter, but I just want you to know what these different parameters represent in this example. It will make sense later on.
In our Python notebook, remember to import the NumPy package into Python, which makes computing mean, median, and mode really easy. We're going to use the import numpy as np directive, which means we can use np as a shorthand to call numpy from now on.
Then we're going to create a list of numbers called incomes using the np.random.normal function.
import numpy as np incomes = np.random.normal(27000, 15000, 10000) np.mean(incomes)
The three parameters of the np.random.normal function mean I want the data centered around 27000, with a standard deviation of 15000, and I want python to make 10000 data points in this list.
Once I do that, I compute the average of those data points, or the mean by just calling np.mean on incomes which is my list of data. It's just that simple.
Let's go ahead and run that. Make sure you selected that code block and then you can hit the play button to actually execute it, and since there is a random component to these income numbers, every time I run it, I'm going to get a slightly different result, but it should always be pretty close to 27000.
Out[1]: 27173.098561362742
OK, so that's all there is to computing the mean in Python, just using NumPy (np.mean) makes it super easy. You don't have to write a bunch of code or actually add up everything and count up how many items you had and do the division. NumPy mean, does all that for you.
- JBoss Weld CDI for Java Platform
- 微服務(wù)設(shè)計(jì)(第2版)
- Python編程自學(xué)手冊(cè)
- Mastering Ubuntu Server
- Mastering Drupal 8 Views
- Mastering JavaScript High Performance
- 組態(tài)軟件技術(shù)與應(yīng)用
- Python Interviews
- Android嵌入式系統(tǒng)程序開發(fā):基于Cortex-A8(第2版)
- Maker基地嘉年華:玩轉(zhuǎn)樂動(dòng)魔盒學(xué)Scratch
- 零基礎(chǔ)學(xué)C語言(升級(jí)版)
- Django Design Patterns and Best Practices
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)
- INSTANT Premium Drupal Themes
- Puppet 5 Beginner's Guide(Third Edition)