这一节开始练习numpy

Your First NumPy Array

In this chapter, we’re going to dive into the world of baseball. Along the way, you’ll get comfortable with the basics of numpy, a powerful package to do data science.

A list baseball has already been defined in the Python script, representing the height of some baseball players in centimeters. Can you add some code here and there to create a numpy array from it?

Instructions

Import the numpy package as np, so that you can refer to numpy with np.
Use np.array() to create a numpy array from baseball. Name this array np_baseball.
Print out the type of np_baseball to check that you got it right.

Exercise

# Create list baseball
baseball = [180, 215, 210, 210, 188, 176, 209, 200]# Import the numpy package as npimport numpy as np
# Create a numpy array from baseball: np_baseball
np_baseball = np.array(baseball)# Print out type of np_baseball
print(type(np_baseball))

Baseball players’ height

You are a huge baseball fan. You decide to call the MLB (Major League Baseball) and ask around for some more statistics on the height of the main players. They pass along data on more than a thousand players, which is stored as a regular Python list: height_in. The height is expressed in inches. Can you make a numpy array out of it and convert the units to meters?

height_in is already available and the numpy package is loaded, so you can start straight away (Source: stat.ucla.edu).

Instructions

Create a numpy array from height_in. Name this new array np_height_in.
Print np_height_in.
Multiply np_height_in with 0.0254 to convert all height measurements from inches to meters. Store the new values in a new array, np_height_m.
Print out np_height_m and check if the output makes sense.

Exercise

# height_in is available as a regular list# Import numpy
import numpy as np# Create a numpy array from height_in: np_height_in
np_height_in = np.array(height_in)# Print out np_height_in
print(np_height_in)# Convert np_height_in to m: np_height_m
np_height_m = np_height_in * 0.0254# Print np_height_m
print(np_height_m)

Baseball player’s BMI

The MLB also offers to let you analyze their weight data. Again, both are available as regular Python lists: height_in and weight_lb. height_in is in inches and weight_lb is in pounds.

It’s now possible to calculate the BMI of each baseball player. Python code to convert height_in to a numpy array with the correct units is already available in the workspace. Follow the instructions step by step and finish the game!

Instructions

Create a numpy array from the weight_lb list with the correct units. Multiply by 0.453592 to go from pounds to kilograms. Store the resulting numpy array as np_weight_kg.
Use np_height_m and np_weight_kg to calculate the BMI of each player. Use the following equation:

Save the resulting numpy array as bmi.
Print out bmi.

Exercise

# height_in and weight_lb are available as regular lists# Import numpy
import numpy as np# Create array from height_in with metric units: np_height_m
np_height_m = np.array(height_in) * 0.0254# Create array from weight_lb with metric units: np_weight_kgnp_weight_kg = np.array(weight_lb) * 0.453592
# Calculate the BMI: bmibmi = np_weight_kg/(np_height_m ** 2)# Print out bmi
print(bmi)

Lightweight baseball players

To subset both regular Python lists and numpy arrays, you can use square brackets:

x = [4 , 9 , 6, 3, 1]
x[1]
import numpy as np
y = np.array(x)
y[1]

For numpy specifically, you can also use boolean numpy arrays:

high = y > 5
y[high]

The code that calculates the BMI of all baseball players is already included. Follow the instructions and reveal interesting things from the data!

Instructions

Create a boolean numpy array: the element of the array should be True if the corresponding baseball player’s BMI is below 21. You can use the < operator for this. Name the array light.
Print the array light.
Print out a numpy array with the BMIs of all baseball players whose BMI is below 21. Use light inside square brackets to do a selection on the bmi array.

Exercise

# height_in and weight_lb are available as a regular lists# Import numpy
import numpy as np# Calculate the BMI: bmi
np_height_m = np.array(height_in) * 0.0254
np_weight_kg = np.array(weight_lb) * 0.453592
bmi = np_weight_kg / np_height_m ** 2# Create the light array
light = bmi<21# Print out light
print(light)# Print out BMIs of all baseball players whose BMI is below 21
print(bmi[light])

NumPy Side Effects

As Hugo explained before, numpy is great for doing vector arithmetic. If you compare its functionality with regular Python lists, however, some things have changed.

First of all, numpy arrays cannot contain elements with different types. If you try to build such a list, some of the elements’ types are changed to end up with a homogeneous list. This is known as type coercion.

Second, the typical arithmetic operators, such as +, -, * and / have a different meaning for regular Python lists and numpy arrays.

Have a look at this line of code:

np.array([True, 1, 2]) + np.array([3, 4, False])
Can you tell which code chunk builds the exact same Python object? The numpy package is already imported as np, so you can start experimenting in the IPython Shell straight away!

Instructions

np.array([4, 3, 0]) + np.array([0, 2, 2])

Subsetting NumPy Arrays

You’ve seen it with your own eyes: Python lists and numpy arrays sometimes behave differently. Luckily, there are still certainties in this world. For example, subsetting (using the square bracket notation on lists or arrays) works exactly the same. To see this for yourself, try the following lines of code in the IPython Shell:

x = ["a", "b", "c"]
x[1]np_x = np.array(x)
np_x[1]

The script in the editor already contains code that imports numpy as np, and stores both the height and weight of the MLB players as numpy arrays.

Instructions

Subset np_weight_lb by printing out the element at index 50.
Print out a sub-array of np_height_in that contains the elements at index 100 up to and including index 110.

Exercise

# height_in and weight_lb are available as a regular lists# Import numpy
import numpy as np# Store weight and height lists as numpy arrays
np_weight_lb = np.array(weight_lb)
np_height_in = np.array(height_in)# Print out the weight at index 50
print(np_weight_lb[50])# Print out sub-array of np_height_in: index 100 up to and including index 110
print(np_height_in[100:111])

Your First 2D NumPy Array

Before working on the actual MLB data, let’s try to create a 2D numpy array from a small list of lists.

In this exercise, baseball is a list of lists. The main list contains 4 elements. Each of these elements is a list containing the height and the weight of 4 baseball players, in this order. baseball is already coded for you in the script.

Instructions

Use np.array() to create a 2D numpy array from baseball. Name it np_baseball.
Print out the type of np_baseball.
Print out the shape attribute of np_baseball. Use np_baseball.shape.

Exercise

# Create baseball, a list of lists
baseball = [[180, 78.4],[215, 102.7],[210, 98.5],[188, 75.2]]# Import numpy
import numpy as np# Create a 2D numpy array from baseball: np_baseball
np_baseball = np.array(baseball)# Print out the type of np_baseball
print(type(np_baseball))# Print out the shape of np_baseball
print(np_baseball.shape)

Baseball data in 2D form

You have another look at the MLB data and realize that it makes more sense to restructure all this information in a 2D numpy array. This array should have 1015 rows, corresponding to the 1015 baseball players you have information on, and 2 columns (for height and weight).

The MLB was, again, very helpful and passed you the data in a different structure, a Python list of lists. In this list of lists, each sublist represents the height and weight of a single baseball player. The name of this embedded list is baseball.

Can you store the data as a 2D array to unlock numpy’s extra functionality?

Instructions

Use np.array() to create a 2D numpy array from baseball. Name it np_baseball.
Print out the shape attribute of np_baseball.

Exercise

# baseball is available as a regular list of lists# Import numpy package
import numpy as np# Create a 2D numpy array from baseball: np_baseballnp_baseball = np.array(baseball)
# Print out the shape of np_baseball
print(np_baseball.shape)

Subsetting 2D NumPy Arrays

If your 2D numpy array has a regular structure, i.e. each row and column has a fixed number of values, complicated ways of subsetting become very easy. Have a look at the code below where the elements “a” and “c” are extracted from a list of lists.

regular list of lists

x = [[“a”, “b”], [“c”, “d”]]
[x[0][0], x[1][0]]

numpy

import numpy as np
np_x = np.array(x)
np_x[:,0]
For regular Python lists, this is a real pain. For 2D numpy arrays, however, it’s pretty intuitive! The indexes before the comma refer to the rows, while those after the comma refer to the columns. The : is for slicing; in this example, it tells Python to include all rows.

The code that converts the pre-loaded baseball list to a 2D numpy array is already in the script. The first column contains the players’ height in inches and the second column holds player weight, in pounds. Add some lines to make the correct selections. Remember that in Python, the first element is at index 0!

Instructions

Print out the 50th row of np_baseball.
Make a new variable, np_weight_lb, containing the entire second column of np_baseball.
Select the height (first column) of the 124th baseball player in np_baseball and print it out.

Exercise

# baseball is available as a regular list of lists# Import numpy package
import numpy as np# Create np_baseball (2 cols)
np_baseball = np.array(baseball)# Print out the 50th row of np_baseball
print(np_baseball[49])# Select the entire second column of np_baseball: np_weight_lb
np_weight_lb = np_baseball[:,1]# Print out height of 124th player
print(np_baseball[123,0])

2D Arithmetic

Remember how you calculated the Body Mass Index for all baseball players? numpy was able to perform all calculations element-wise (i.e. element by element). For 2D numpy arrays this isn’t any different! You can combine matrices with single numbers, with vectors, and with other matrices.

Execute the code below in the IPython shell and see if you understand:

import numpy as np
np_mat = np.array([[1, 2],[3, 4],[5, 6]])
np_mat * 2
np_mat + np.array([10, 10])
np_mat + np_mat

np_baseball is coded for you; it’s again a 2D numpy array with 3 columns representing height (in inches), weight (in pounds) and age (in years).

Instructions

You managed to get hold of the changes in height, weight and age of all baseball players. It is available as a 2D numpy array, updated. Add np_baseball and updated and print out the result.
You want to convert the units of height and weight to metric (meters and kilograms respectively). As a first step, create a numpy array with three values: 0.0254, 0.453592 and 1. Name this array conversion.
Multiply np_baseball with conversion and print out the result.

Exercise

# baseball is available as a regular list of lists
# updated is available as 2D numpy array# Import numpy package
import numpy as np# Create np_baseball (3 cols)
np_baseball = np.array(baseball)# Print out addition of np_baseball and updatedprint(np_baseball + updated)# Create numpy array: conversionconversion = np.array([[0.0254,0.453592,1]])
# Print out product of np_baseball and conversion
print(np_baseball * conversion)

Average versus median

You now know how to use numpy functions to get a better feeling for your data. It basically comes down to importing numpy and then calling several simple functions on the numpy arrays:

import numpy as np
x = [1, 4, 8, 10, 12]
np.mean(x)
np.median(x)

The baseball data is available as a 2D numpy array with 3 columns (height, weight, age) and 1015 rows. The name of this numpy array is np_baseball. After restructuring the data, however, you notice that some height values are abnormally high. Follow the instructions and discover which summary statistic is best suited if you’re dealing with so-called outliers.

Instructions

Create numpy array np_height_in that is equal to first column of np_baseball.
Print out the mean of np_height_in.
Print out the median of np_height_in.

Exercise

# np_baseball is available# Import numpy
import numpy as np# Create np_height_in from np_baseballnp_height_in =np_baseball[:,0]
# Print out the mean of np_height_in
print(np.mean(np_height_in))# Print out the median of np_height_in
print(np.median(np_height_in))

Explore the baseball data

Because the mean and median are so far apart, you decide to complain to the MLB. They find the error and send the corrected data over to you. It’s again available as a 2D Numpy array np_baseball, with three columns.

The Python script in the editor already includes code to print out informative messages with the different summary statistics. Can you finish the job?

Instructions

The code to print out the mean height is already included. Complete the code for the median height. Replace None with the correct code.
Use np.std() on the first column of np_baseball to calculate stddev. Replace None with the correct code.
Do big players tend to be heavier? Use np.corrcoef() to store the correlation between the first and second column of np_baseball in corr. Replace None with the correct code.

Exercise

# np_baseball is available# Import numpy
import numpy as np# Print mean height (first column)
avg = np.mean(np_baseball[:,0])
print("Average: " + str(avg))# Print median height. Replace 'None'
med = np.median(np_baseball[:,0])
print("Median: " + str(med))# Print out the standard deviation on height. Replace 'None'
stddev = np.std(np_baseball[:,0])
print("Standard Deviation: " + str(stddev))# Print out correlation between first and second column. Replace 'None'
corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])
print("Correlation: " + str(corr))

Blend it all together

In the last few exercises you’ve learned everything there is to know about heights and weights of baseball players. Now it’s time to dive into another sport: soccer.

You’ve contacted FIFA for some data and they handed you two lists. The lists are the following:

positions = ['GK', 'M', 'A', 'D', ...]
heights = [191, 184, 185, 180, ...]

Each element in the lists corresponds to a player. The first list, positions, contains strings representing each player’s position. The possible positions are: ‘GK’ (goalkeeper), ‘M’ (midfield), ‘A’ (attack) and ‘D’ (defense). The second list, heights, contains integers representing the height of the player in cm. The first player in the lists is a goalkeeper and is pretty tall (191 cm).

You’re fairly confident that the median height of goalkeepers is higher than that of other players on the soccer field. Some of your friends don’t believe you, so you are determined to show them using the data you received from FIFA and your newly acquired Python skills.

Instructions

Convert heights and positions, which are regular lists, to numpy arrays. Call them np_heights and np_positions.
Extract all the heights of the goalkeepers. You can use a little trick here: use np_positions == ‘GK’ as an index for np_heights. Assign the result to gk_heights.
Extract all the heights of all the other players. This time use np_positions != ‘GK’ as an index for np_heights. Assign the result to other_heights.
Print out the median height of the goalkeepers using np.median(). Replace None with the correct code.
Do the same for the other players. Print out their median height. Replace None with the correct code.

Exercise

# heights and positions are available as lists# Import numpy
import numpy as np# Convert positions and heights to numpy arrays: np_positions, np_heightsnp_positions = np.array(positions)
np_heights = np.array(heights)print(np_positions)
print(np_heights)# Heights of the goalkeepers: gk_heights
gk_heights = np_heights[np_positions=='GK']# Heights of the other players: other_heightsother_heights = np_heights[np_positions != 'GK']# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))

DataCamp中Introduction to Python之NumPy相关推荐

  1. DataCamp中Introduction to Python之Python Basics练习

    DataCamp的Introduction to Python之Python Basics是一些比较基础的练习,可以很快掌握. The Python Interface In the Python s ...

  2. DataCamp中Introduction to Python之Python Lists练习

    这一节主要讲的是列表练习 Create a list As opposed to int, bool etc., a list is a compound data type; you can gro ...

  3. fun是什么意思 python中def_【python】 numpy中的矩阵转置(ndarray.T)为什么不加括号却可以实现方法的功能...

    [python] numpy中的矩阵转置(ndarray.T)为什么不加括号却可以实现方法的功能 经过搜索和查询源码了解到,.T就是一个方法,但是是被装饰器修饰了一下... @property是pyt ...

  4. ANSA二次开发 - 在ANSA中导入外部Python库

    文章目录 在ANSA中导入外部python库 NumPy SciPy OpenSSL H5py PIP 和venv包和环境管理系统 PIP venv CONDA包和环境管理系统 下载Conda Con ...

  5. python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组、使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank)

    python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组.使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank) 目录

  6. python使用numpy的np.float_power函数计算numpy数组中每个数值的指定幂次(例如平方、立方)、np.power函数默认返回整数格式、np.float_power函数返回浮点数

    python使用numpy的np.float_power函数计算numpy数组中每个数值的指定幂次(例如平方.立方).np.power函数默认返回整数格式.np.float_power函数默认返回浮点 ...

  7. python使用numpy的np.power函数计算numpy数组中每个数值的指定幂次(例如平方、立方)、np.power函数默认返回整数格式、np.float_power函数默认返回浮点数

    python使用numpy的np.power函数计算numpy数组中每个数值的指定幂次(例如平方.立方).np.power函数默认返回整数格式.np.float_power函数默认返回浮点数 目录

  8. python使用numpy中的np.mean函数计算数组的均值、np.var函数计算数据的方差、np.std函数计算数组的标准差

    python使用numpy中的np.mean函数计算数组的均值.np.var函数计算数据的方差.np.std函数计算数组的标准差 目录

  9. Python使用numpy中的hstack函数水平堆叠(horizontally stack)数组实战

    Python使用numpy中的hstack函数水平堆叠(horizontally stack)数组实战 目录 Python使用numpy中的hstack函数水平堆叠(horizontally stac ...

最新文章

  1. webpack4 高手之路 第四天
  2. 未分配内存的指针导致段错误
  3. 太空将成为数据中心冷却新前沿
  4. java面试题在线做_java考试题目及答案也可以当面试题
  5. MVC学习九:MVC 特性本质
  6. pytorch中unsqueeze()函数理解
  7. win7旗舰版太卡怎么办
  8. mysql 参数调整_mysql需要调整的参数-阿里云开发者社区
  9. 处理器startup.s 常见汇编指令,伪指令解释 .globl _start .section .data .text .align
  10. 自己动手写Docker系列 -- 4.2使用AUFS包装busybox
  11. 04 - 雷达的工作频率
  12. java哈夫曼编码译码_java实现哈夫曼编码
  13. 智能住宅小区安防报警网络系统
  14. yolov3 NMS非极大值抑制
  15. FM FFM:深入理解FM与FFM
  16. CS61A Lab 14
  17. Mtime美好时光网 盈利模式
  18. 初次接触ESP8266
  19. 关于Linux 网络抓包的一些笔记整理
  20. 备忘录 | 阅读而已,用得着这么认真么?

热门文章

  1. 修改PHP上传文件大小限制(windows、linux)
  2. 项目的px单位如何转化vw单位
  3. Camunda 代码实现流程图
  4. linux压缩包解压安装mysql教程
  5. Python(2):上机练习——计算y年m月有几天
  6. Matlab:plot函数绘制二维折线图
  7. C语言温控风机程序,温度控制电风扇单片机c系统proteus仿真.docx
  8. 大数据工程师培训机构如何选择
  9. ERROR 1819 (HY000): Your password does not satisfy the current policy requiremen 【mysql】
  10. 异常Exception和异常日志笔记