[英]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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.