1

I want to create a model using logistic regression. For this reason first i take the data from the file and separate each line from this txt and split it according to "," and put them into my array (datum). After that i converted that array to numPy array and shuffle it randomly. But when i slice array into two different piece for testing and training.

This error occured:

Traceback (most recent call last):
  File ".\logisticRegression.py", line 32, in <module>
    training_data = matrix_data[167:,:]
TypeError: list indices must be integers or slices, not tuple

Here is the code that i wrote:

import numpy as np
import matplotlib.pyplot as plt


def load_data(path):
    datum= []
    with open(path) as fp:
        line = fp.readline()
        while line:
            arr_line= line.split(",")
            datum.append(arr_line)
            line=fp.readline()
    return datum

#Sigmoid function
def sigmoid(x):
    return 1/(1+np.exp(-x))

#Loss function
def square_loss(y_pred, target):
    return np.mean(pow(((y_pred - target),2)))

if __name__ == "__main__":
    # load the data from the file
    matrix_data = load_data("all_data.txt")
    np.array(matrix_data)

    np.random.shuffle(matrix_data)

    training_data = matrix_data[167:,:] #These all lines gives error
    test_data = matrix_data[41:,:] #These all lines gives error

    X_tr, y_tr = training_data[:, :-1], training_data[:, -1] #These all lines gives error
    X_te, y_te = test_data[:, :-1], test_data[:, -1] #These all lines gives error
 
    

Note: I searched for this error and i found that the problem is the lack of comma in my array but when i print the array it has comma for each index.

3
  • 2
    np.array(matrix_data) What do you expect that line to do? You're calling np.array() but you're not saving the result into a new variable... Commented Nov 2, 2020 at 22:23
  • 1
    matrix_data is a list. Commented Nov 2, 2020 at 22:27
  • 1
    As an aside, use the csv module. At the very least, stop with the while line stuff, file objects are iterable. All you need is for line in open(path): ... Commented Nov 2, 2020 at 22:27

1 Answer 1

2

You have to assign the result of np.array to a variable, it doesn't change the argument matrix_data:

matrix_data = np.array(matrix_data)

Your code failes because you still have a list and not a numpy datastructure.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i can't believe that is the problem :) . It escaped me.

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.