(X_train, X_test) = X[50:], X[:50]
How does the split happens here? Please provide the inner explanation.
(X_train, X_test) = X[50:], X[:50]
How does the split happens here? Please provide the inner explanation.
Take the following example on a simple list:
a = [0, 1, 2, 3, 4, 5, 6, 7]
print(a[:3])
print(a[3:])
Will output
[0, 1, 2]
# [:3] returns everything from the start, to before the third index
[3, 4, 5, 6, 7]
# [3:] returns everything from the third index to the end
Expanded to [50:]
and [:50]
will return everything from the 50th index to the end and everything from the beginning to before the 50th index respectively.
The second part of your question is about tuple unpacking. If there are the same number of variables to set as there are elements in some collection such as a tuple or list, it'll unpack them. For example:
(a, b) = 42, 9001
print("Val A:", a)
print("Val B:", b)
Will output:
Val A: 42
Val B: 9001
You actually don't even need the ()
brackets around the variables.
Expanded to your question, it's actually just a simplified, one line version of saying:
X_train = X[50:]
X_test = X[:50]
Assuming X is a list, tuple or an array, the first slice ([50:]
) will return all the elements from the 50th element to the end. The second slice ([:50]
) will return the first through the 49th element.