How do you split an array in python in terms of the number of elements in the array. Im doing knn classification and I need to take into account of the first k elements of the 2D array.
Asked
Active
Viewed 1.1k times
-3
-
4Did you ask Python to split it ? Try being polite this time!? :P – d-coder Apr 05 '18 at 04:32
-
5Possible duplicate of [Split list into smaller lists](https://stackoverflow.com/questions/752308/split-list-into-smaller-lists) – Moerwald Apr 05 '18 at 04:33
-
Are you talking about a normal python list (which is called an array in other languages) or a numpy array ? – sudormrfbin Apr 05 '18 at 05:27
2 Answers
1
import numpy as np
x = np.array([1, 2, 4, 4, 6, 7])
print(x[range(0, 4)])
You can also split it up by taking the range of elements that you want to work with. You could store x[range(x, x)]) in a variable and work with those particular elements of the array as well. The output as you can see splits the array up:
[1 2 4 4]

Simeon Ikudabo
- 2,152
- 1
- 10
- 27