Python is new for me and I am doing some machine learning code using python. My scenario is that I am reading data from my sql and trying to give a shape to this data so i can use it for MLP training.
My code is below:
connection = mysql.connector.connect(host='localhost', port=3306, user='root', passwd='mysql', db='medicalgame')
cur = connection.cursor()
query = ""
cur.execute(query)
# X_train will be a list of list and later we'll convert it to a numpy ndarray
X_train = []
for row in cur:
X_train.add(row)
connection.close()
X_train should be ready
X_train = np.asarray(X_train)
print 'The shape of X_train is', X_train.shape
During debug the query result i got is like this: (6, 1, 1, 1, 2, u'F', 1, 0, 0, 19) Can anyone help me how can, I fix the error and give shape to my X_train, so that MLP accept it as an input ?
The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.
It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.
The Python "AttributeError: 'NoneType' object has no attribute 'append'" occurs when we try to call the append() method on a None value, e.g. assignment from function that doesn't return anything. To solve the error, make sure to only call append() on list objects.
The Python "AttributeError: 'list' object has no attribute 'replace'" occurs when we call the replace() method on a list instead of a string. To solve the error, call replace() on a string, e.g. by accessing the list at a specific index or by iterating over the list.
the message is clear. list
has no method add
because it is ordered (it has a dunder __add__
method but that's for addition between lists). You can insert
but you want to append
. So the correct way is:
X_train = []
for row in cur:
X_train.append(row)
BUT the preferred way converting to a list directly (iterating on cur
elements to create your list in a simple and performant way):
X_train = list(cur)
BUT you cannot do that since your list contains bogus data. Fortunately, you can filter them out in a nested list comprehension like this:
X_train = [[x for x in r if type(x)==int] for r in cur]
this builds your list of lists but filters out non-integer values and feeding it to numpy.asarray
yields (with your sample data):
[[ 6 1 1 1 2 1 0 0 19]
[ 6 1 1 1 2 1 0 0 14]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With