1

I think this is very trivial question but hopefully someone can help me out. What's the best way to split an array that contains both predictors (inputs) and the response variable (output)?

I imported a csv file with both predictors and the response, but I'd like to split it so that the predictors are in a numpy matrix (n X m) and the response is in a vector (n X 1). Any thoughts?

Thanks.

Richard
  • 167
  • 1
  • 3
  • 11

1 Answers1

2

Simply use the shape property of the imported numpy array to determine m and the extract the subarrays using slicing:

import numpy as np

# load csv data
data = np.loadtxt('data.txt', delimiter=',')
# m is the number of columns minus one
m = data.shape[1]-1
# use slicing to extract subarrays
pred = data[:,:m]
resp = data[:,m]

A nice explanation of slicing syntax can be found here.

Community
  • 1
  • 1
spfrnd
  • 886
  • 8
  • 15