简体   繁体   中英

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

I am new to Python and machine learning and I am confused what are these colons in someplace(arrays) they appear and some they don't can someone explain to me what are those?

Don't mind me I am a noob.

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:]

The colons are used to index and slice items in a list. For example, [1:] would mean the second element to the last element in a list, and [:] would mean all items in the list.

The colon means that you are grabbing everything from that particular dimension. For example, using A[i, :] means you are taking all values from the ith row. A[:, j] means you look at all rows in column j. Even in the third dimension if you say, A[:, :, k], this means that you are taking all rows and columns of page k of the third dimensional array.

I had the same question. Here is your answer:

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())

More info: https://www.tensorflow.org/guide/tensor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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