我正在阅读这本我正在阅读的书中的SVM分类器示例代码。
我是Python新手,很难理解/可视化所有这些数组语法[:,1] [:,:-1]
。有人可以解释一下这3行代码的意思/做法。我将非常感激。
Convert string data to numerical data
label_encoder = []
X_encoded = np.empty(X.shape)
for i,item in enumerate(X[0]):
if item.isdigit():
X_encoded[:, i] = X[:, i]
else:
label_encoder.append(preprocessing.LabelEncoder())
X_encoded[:, i] = label_encoder[-1].fit_transform(X[:, i])
X = X_encoded[:, :-1].astype(int)
y = X_encoded[:, -1].astype(int)
答案 0 :(得分:1)
Numpy数组允许的功能超出python列表的范围。
numpy切片中的,
也是表示数组的维度。
考虑一个3x3矩阵,它有2个维度。让我们看看一些操作在python列表和numpy数组中的感觉如何
>>> import numpy as np
>>> py = [[1,2,3],[3,4,5],[4,5,6]]
>>> npa = np.array(py)
>> py[1:3] # [[3, 4, 5], [4, 5, 6]]
>> npa[1:3] # array([[3, 4, 5], [4, 5, 6]])
>>> # get column 2 and 3 from rows 2 and 3
>>> npa[1:3, 1:3] # row, col
假设您不熟悉列表索引/切片
py[:] # means every element in the array, also is a shorthand to create a copy
向前推进,npa[:,1]
将为您提供包含每行([:,
)第二列(,1]
)的数组。即array([2,4,5])
同样地,npa[:,:-1]
将为每个行(,:-1]
)提供除最后一列([:,
)之外的每一列的数组。即array([[1,2],[3,4], [4,5]])
参考文献:https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html