0

I am trying to load data in a csv file (with delimiter ',') into a numpy array. Example of a line is: 81905.75578271,81906.6205052,50685.487931,.... (1000 columns). I have this code but it seems to not be working properly as in the exit of the function the debugger cannot recognize the data, and when I call the xtrain.shape it returns 0:

def load_data(path):
    # return np.loadtxt(path,dtype=int,delimiter=',')
    file = open(path,'r')
    data = []
    for line in file:
        array_vals = line.split(",")
        array = []
        for val in array_vals:
            if not val:
                array.append(float(val))
        data.append(np.asarray(array))
    return np.asarray(data)

x_train =  load_data(path)
2
  • 1
    use pandas read_csv method. Commented Jun 13, 2018 at 7:09
  • Are you getting any errors? Commented Jun 13, 2018 at 7:15

1 Answer 1

4

This should give you your required output.

import numpy as np
def load_data(path):
    return np.loadtxt(path,delimiter=',')
Sign up to request clarification or add additional context in comments.

1 Comment

I actually tried before, but it didnt work. Ive just realized that there were some ',' at the end of some lines in the file -.-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.