【发布时间】:2017-03-26 19:30:33
【问题描述】:
Python 对我来说是新的,我正在使用 Python 编写一些机器学习代码。我的场景是我正在从我的 sql 中读取数据并尝试为这些数据赋予形状,以便我可以将其用于 MLP 训练。
我的代码如下:
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
在调试过程中,我得到的查询结果是这样的: (6, 1, 1, 1, 2, u'F', 1, 0, 0, 19) 谁能帮助我,我可以修复错误并为我的 X_train 赋予形状,以便 MLP 接受它作为输入?
【问题讨论】:
-
使用
append,add用于集合。 -
你能解释一下查询的问题吗?你的意思是你得到了行,但有时一个字母滑入并且你想删除它?
-
目前看来 append 有效,但我不确定为什么这个奇怪的字母 'u' 会出现在我的查询结果中。我的 X_train 现在是这样的: [(6, 1, 1, 1, 2, u'F', 1, 0, 0, 19), (6, 1, 1, 1, 2, u'V', 1 , 0, 0, 14)] 你能告诉我如何用 '[' this 删除这个 '(' 吗?
-
u'F'是一个 unicode 字符。大概您的数据库有一个字符或字符串值字段。
标签: python numpy machine-learning scikit-learn