0

I am trying to create a sparse matrix for my decision tree algorithm in python. See my code below

import numpy as np
from scipy import sparse

# loading the txt files
trainData=np.loadtxt('trainData.txt')
testData=np.loadtxt('testData.txt')
trainLabel=np.loadtxt('trainLabel.txt')
testLabel=np.loadtxt('testLabel.txt')

#convert trainData and testData into sparse matrices

nWords = max(max(trainData[:,1]),max(testData[:,1]))
trainDataSparse = sparse.csr_matrix(trainData[:,0],trainData[:,1],np.ones(len(trainData)),max(trainData[:,0]),nWords)
testDataSparse = sparse.csc_matrix(testData[:,0],testData[:,1],np.ones(len(testData)),max(testData[:,0]),nWords)

When I run this I get the following error

trainDataSparse = sparse.csr_matrix(trainData[:,0],trainData[:,1],np.ones(len(trainData)),max(trainData[:,0]),nWords)
TypeError: __init__() takes from 2 to 5 positional arguments but 6 were given

From what I see, I am only entering five arguments to the function, where is that sixth argument that error is talking about? and how to fix that?

kareemostafa
  • 703
  • 1
  • 5
  • 7
  • In regards of your error on the number of argument, [here](https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given) there is a somewhat deeper explanation. – Valentino Feb 10 '19 at 21:59

1 Answers1

1

The correct signature for csr_matrix is:

csr_matrix(arg1, shape=None, dtype=None, copy=False)

It's hard for me to tell from your code what you want to do, so I can't suggest a fix, but the documentation has many examples that you can look at.

The reason why you see 6 instead of 5 is because the implicit self argument is added to the count.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69