繁体   English   中英

这是什么:机器学习中的(冒号)Python?

[英]What are this : (colons) in Machine Learning with Python?

我是 Python 和机器学习的新手,我很困惑它们出现在某个地方(数组)中的这些冒号是什么,有些却没有,有人可以向我解释那些是什么吗?

别介意我是菜鸟。

companies = pd.read_csv('D:/Programming/Python/TensorFlow/Datasets/Linear Regression/1000_Companies.csv')
X = companies.iloc[:, :-1].values
y = companies.iloc[:, 4].values

#changing the name of cities to machine understandable format
labelencoder = LabelEncoder()
X[:, 3] = labelencoder.fit_transform(X[:, 3])
ct = ColumnTransformer(
    [('one_hot_encoder', OneHotEncoder(), [3])],    # The column numbers to be transformed (here is [0] but can be [0, 1, 3])
    remainder='passthrough'                         # Leave the rest of the columns untouched
)
X = np.array(ct.fit_transform(X), dtype=np.float)
X = X[:, 1:]

冒号用于索引和切片列表中的项目。 例如, [1:]表示列表中的第二个元素到最后一个元素, [:]表示列表中的所有项目。

冒号表示您正在从该特定维度获取所有内容。 例如,使用 A[i, :] 意味着您从第 i 行获取所有值。 A[:, j] 表示查看 j 列中的所有行。 即使在第三维中,如果您说 A[:, :, k],这意味着您正在获取第三维数组的第 k 页的所有行和列。

我有同样的问题。 这是你的答案:

negative indices count backwards from the end
colons, :, are used for slices: start:stop:step

print("Everything:", rank_1_tensor[:].numpy())
print("Before 4:", rank_1_tensor[:4].numpy())
print("From 4 to the end:", rank_1_tensor[4:].numpy())
print("From 2, before 7:", rank_1_tensor[2:7].numpy())
print("Every other item:", rank_1_tensor[::2].numpy())
print("Reversed:", rank_1_tensor[::-1].numpy())

更多信息: https://www.tensorflow.org/guide/tensor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM