-3

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.

Gurz Singh
  • 41
  • 1
  • 1
  • 1

2 Answers2

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
0

In Numpy, there is a method numpy.split.

x = np.arange(9.0)
np.split(x, 3)
Minato
  • 452
  • 5
  • 19