You can avoid the need for copying if you can first read the data into a simple NumPy array (omitting the names parameter):
data = np.genfromtxt(path, dtype=float, delimiter=',', skip_header=1)
Then (we are lucky) X consists of all but the first and last columns (i.e. omitting the datetime and occupancy ). Therefore, we can express X and y as slices:
X = data[:, 1:-1] y = data[:, -1].astype(int)
Then we can easily pass these functions to scikit-learn:
clf = GradientBoostingClassifier(random_state=42) clf.fit(X, y)
and, if you want, we can consider a simple NumPy array as a structured array afterwards:
features = ["temperature", "relative_humidity", "light", "C02", "humidity"] X = X.ravel().view([(field, X.dtype.type) for field in features])
Unfortunately, this workaround relies on X , expressed as a slice - we cannot avoid copying if, for example, occupancy appeared between other function codes. It also means that you must define X using X = data[:, 1:-1] instead of the more human-friendly X = data[features] .
import numpy as np from sklearn.ensemble import GradientBoostingClassifier data = np.genfromtxt(path, dtype=float, delimiter=',', skip_header=1) X = data[:, 1:-1] y = data[:, -1].astype(int) clf = GradientBoostingClassifier(random_state=42) clf.fit(X, y) features = ["temperature", "relative_humidity", "light", "C02", "humidity"] X = X.ravel().view([(field, X.dtype.type) for field in features])
If you must start with a structured array, then hpaulj answer shows how the view/reshape/slice structured array will get a simple array without copying:
import numpy as np nan = np.nan data = np.array([(nan, 23.18, 27.272 , 426. , 721.25, 0.00479299, 1.), (nan, 23.15, 27.2675, 429.5 , 714. , 0.00478344, 1.), (nan, 23.15, 27.245 , 426. , 713.5 , 0.00477946, 1.), (nan, 20.89, 27.745 , 423.5 , 1521.5 , 0.00423682, 1.), (nan, 20.89, 28.0225, 418.75, 1632. , 0.00427949, 1.), (nan, 21. , 28.1 , 409. , 1864. , 0.00432073, 1.)], dtype=[('datetime', '<f8'), ('temperature', '<f8'), ('relative_humidity', '<f8'), ('light', '<f8'), ('C02', '<f8'), ('humidity', '<f8'), ('occupancy', '<f8')]) target = 'occupancy' nrows = len(data) X = data.view('<f8').reshape(nrows, -1)[:, 1:-1] y = data[target].astype(int)
This exploits the fact that each field is 8 bytes long. Therefore, it is easy to convert a structured array to a simple dtype <f8 array. Reshaping makes it a two-dimensional array with the same number of rows. Slicing removes the columns / t 2> occupancy column / field from the array.