0

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:]
Jay Patel
  • 19
  • 5
  • 2
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Chase Aug 12 '20 at 12:37

3 Answers3

0

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.

Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
0

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.

Kevin D.
  • 104
  • 8
0

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

Eddy C
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '23 at 12:14