# 如何为机器学习索引，切片，调整 NumPy 数组

2018-02-11 14:07:45来源:https://cloud.tencent.com/developer/article/1040042作者:极客头条人点击

1.从列表到数组

# one dimensional example
from numpy import array
# list of data
data = [11, 22, 33, 44, 55]
# array of data
data = array(data)
print(data)
print(type(data))

[11 22 33 44 55]
<class 'numpy.ndarray'>

# two dimensional example
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data)
print(type(data))

[[11 22]
[33 44]
[55 66]]
<class 'numpy.ndarray'>
2.数组索引

# simple indexing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
# index data
print(data[0])
print(data[4])

# simple indexing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
# index data
print(data[5])

IndexError: index 5 is out of bounds for axis 0 with size 5

# simple indexing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
# index data
print(data[-1])
print(data[-5])

data[0,0]

data[0][0]

# 2d indexing
from numpy import array
# define array
data = array([[11, 22], [33, 44], [55, 66]])
# index data
print(data[0,0])

# 2d indexing
from numpy import array
# define array
data = array([[11, 22], [33, 44], [55, 66]])
# index data
print(data[0,])

[11 22]
3.数组切片

”和“to
”。切片从“from”索引开始，并在“to”索引之前结束。（切片操作的范围包含起始项，但不包含结束项）

data[from:to]

# simple slicing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[:])

[11 22 33 44 55]

# simple slicing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[0:1])

[11]

# simple slicing
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data[-2:])

[44 55]

X = [:, :-1]

y = [:, -1]

# split input and output
from numpy import array
# define array
data = array([[11, 22, 33],
[44, 55, 66],
[77, 88, 99]])
# separate data
X, y = data[:, :-1], data[:, -1]
print(X)
print(y)

[[11 22]
[44 55]
[77 88]]
[33 66 99]

dataset
train = data[:split, :]

test = data[split:, :]

# split train and test
from numpy import array
# define array
data = array([[11, 22, 33],
[44, 55, 66],
[77, 88, 99]])
# separate data
split = 2
train,test = data[:split,:],data[split:,:]
print(train)
print(test)

[[11 22 33]
[44 55 66]]
[[77 88 99]]
4.数列维数变形

NumPy 数组有一个 shape 属性，它返回一个包含数组每个维度中数据数量的元组。

# array shape
from numpy import array
# define array
data = array([11, 22, 33, 44, 55])
print(data.shape)

(5,)

# array shape
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data.shape)

(3, 2)

# array shape
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print('Rows: %d' % data.shape[0])
print('Cols: %d' % data.shape[1])

Rows: 3
Cols: 2

NumPy 为 NumPy 数组对象提供 reshape（）函数，可用于调整维数。

reshape（）函数接受一个指定数组新形状的参数。在将一维数组重新整形为具有多行一列的二维数组的情况下，作为参数的元组，从 shape[0] 属性中获取行数，并将列数设定为1。

data = data.reshape((data.shape[0], 1))

# reshape 1D array
from numpy import array
from numpy import reshape
# define array
data = array([11, 22, 33, 44, 55])
print(data.shape)
# reshape
data = data.reshape((data.shape[0], 1))
print(data.shape)

(5,)
(5, 1)

reshape( ) 函数可以直接使用，指定新的维度。以下是一个清楚的例子，其中每个序列拥有多个步长，每个步长对应其相应的观察结果。

data.reshape((data.shape[0], data.shape[1], 1))

# reshape 2D array
from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data.shape)
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data.shape)

(3, 2)
(3, 2, 1)

An Informal Introduction to Python

Array Creation in NumPy API

Indexing and Slicing in NumPy API

Basic Indexing in NumPy API

NumPy shape attribute

NumPy reshape() function

1