文档地址:np.array()
1、<class 'numpy.ndarray'>
ndarray 表示 n 维度(n D)数组 (= n 行数组)。
2、打印 array 结构 —— np_array.shape
3、Subsetting 2D Arrays 的两种方式
不管是 arr[1][2] 还是 arr[1,2] 都是可行的,不过教程推荐第二种方式。
4、代码演示 & 一个取子集的小技巧
创建:
# 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 is available as a regular list of lists # Import numpy package import numpy as np # Create a 2D numpy array from baseball: np_baseball np_baseball = np.array(baseball) # Print out the shape of np_baseball print(np_baseball.shape)
小技巧:
# 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 np_weight = np_baseball[:,1] # Print out height of 124th player print(np_baseball[123,0])
5、整体数学运算
# 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 updated print(np_baseball + updated) # Create numpy array: conversion conversion = np.array([0.0254, 0.453592, 1]) # Print out product of np_baseball and conversion print(conversion * np_baseball)