4 Numpy
4.1.1Your First NumPy Array
#Create list baseball
baseball = [180, 215, 210, 210, 188, 176,209, 200]
#Import the numpy package as np
import 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))
#<class’numpy.ndarray’>
4.1.2
#height is available as a regular list
#Import numpy
import numpy as np
#Create a numpy array from height:np_height
np_height = np.array(height_in)
#Print out np_height
print(np_height)
#Convert np_height to m: np_height_m
np_height_m = np_height * 0.0254
#Print np_height_m
print(np_height_m)
4.1.3 array和list的异同:1.可以用[]获取子集;2.numpy array 可以[boolean]
In [9]:
x = [4 , 9 , 6, 3, 1]
In [10]:x
Out[10]: [4, 9, 6, 3, 1]
In [11]:import numpy as np
In [12]:y = np.array(x)
In [13]:y
Out[13]: array([4, 9, 6, 3, 1])
In [14]:type(y)
Out[14]: numpy.ndarray
In [15]:
high = y>5
In [16]:high
Out[16]: array([False, True, True, False, False], dtype=bool)
In [17]:highest = y>6
In [18]: highest
Out[18]:array([False, True, False, False, False], dtype=bool)
In [19]:y[high]
Out[19]: array([9, 6])
In [20]:y[highest]
Out[20]: array([9])
In [4]:print(y[1:3])
#[9 6]
4.1.4 remarks附注
4.1.4.1NumPy arrays:contain only one type
In [3]:
np.array([True, 1, 2])
Out[3]: array([1, 1, 2])
In [5]:np.array([1.0,'is',True])
Out[5]: array([‘1.0’, ‘is’, ‘True’],dtype=’<U32’)
4.1.4.2Different types: different behavior
In [8]:
python_list=[1,2,3]
In [9]:numpy_array =np.array(python_list)
In [10]:python_list+python_list
AAAA
Out[10]: [1, 2, 3, 1, 2, 3]
In [12]:numpy_array +numpy_array
BBBB
Out[12]: array([2, 4, 6])
4.2 2D NumPy Arrays
4.2.1 First 2D NumPy Arrays二维数组
#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))
******#<class ‘numpy.ndarray’>
#Print out the shape of np_baseball 打印二维数组维数
print(np_baseball.shape)
#(4, 2)
4.2.2 取二维数组中的元素’a’, ‘c’
#regular list of lists
x = [["a", "b"], ["c", "d"]]
[x[0][0], x[1][0]]
#[‘a’,‘c’] 不能x[:, 0]
#numpy
import numpy as np
np_x = np.array(x)
np_x[:, 0]
#array([‘a’, ‘c’], dtype=’<U1’)
In [9]:print(np_x[:,0])
#[‘a’ ‘c’]
In [10]:print([ x[0][0], x[1][0] ])
#[‘a’, ‘c’]
4.2.3 2D Arithmetic
In [1]:
import numpy as np
np_mat = np.array([[1, 2], [3, 4], [5,6]])
In [2]:np_mat * 2
Out[2]:
array([[ 2, 4],
[ 6, 8],
[10, 12]])
In [3]:np_mat + np.array([10, 10])
Out[3]:
array([[11, 12],
[13, 14],
[15, 16]])
In [4]:np_mat + np_mat
Out[4]:
array([[ 2, 4],
[ 6, 8],
[10, 12]])
4.3 NumPy: Basic Statistics
average/median
import numpy as np
x = [1, 4, 8, 10, 12]
In [3]: np.mean(x)
Out[3]: 7.0
In [4]: np.median(x)
Out[4]: 8.0
correlation/the standard deviation
#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))
#Average: 73.6896551724
#Print median height. Replace ‘None’
med = np.median(np_baseball[:,0])
print("Median: " + str(med))
#Median: 74.0
#Print out the standard deviation on height. Replace ‘None’
stddev = np.std(np_baseball[:,0])
print("Standard Deviation: " + str(stddev))
#Standard Deviation: 2.31279188105
#Print out correlation between first and second column. Replace ‘None’
corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])
print("Correlation: " + str(corr))
#Correlation: [[1. 0.53153932]
[ 0.53153932 1. ]]
分组
#heights and positions are available as lists
#Import numpy
import numpy as np
#Convert positions and heights to numpy arrays: np_positions, np_heights
np_positions = np.array(positions)
np_heights = np.array(heights)
#Heights of the goalkeepers: gk_heights
gk_heights = np_heights[np_positions == 'GK']
#Heights of the other players: other_heights
other_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)))