HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Efficient implementation of aggregating test/train data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
efficientaggregatingtraintestimplementationdata

Problem

Here is a short python snippet to ingest train data:

x_train = []
y_train = []

for x,y in train:
    x_train.append(x)
    y_train.append(y)

x_train = np.asarray(x_train)
y_train = np.asarray(y_train)


The variable train is a list of 60000 (x,y) tuples.

Solution

How about:

x_train, y_train = np.hsplit(np.asarray(train), 2)


Which uses only numpy functions and should be a bit faster than your implementation using intermediate lists.

Code Snippets

x_train, y_train = np.hsplit(np.asarray(train), 2)

Context

StackExchange Code Review Q#140709, answer score: 3

Revisions (0)

No revisions yet.