我有一个具有回归模型截距和系数的数据库。我想提取系数并将其转换为numpy数组。
cursor.execute("""SELECT * FROM model_coeffs WHERE desc = 'L1 penalty model'""")
cursor.fetchall()
输出如下所示:
[(3, 'L1 penalty model', 'Intercept', '[0.]'),
(3,
'L1 penalty model',
"['mean radius' 'mean texture' 'mean perimeter' 'mean area'\n 'mean smoothness' 'mean compactness' 'mean concavity'\n 'mean concave points' 'mean symmetry' 'mean fractal dimension'\n 'radius error' 'texture error' 'perimeter error' 'area error'\n 'smoothness error' 'compactness error' 'concavity error'\n 'concave points error' 'symmetry error' 'fractal dimension error'\n 'worst radius' 'worst texture' 'worst perimeter' 'worst area'\n 'worst smoothness' 'worst compactness' 'worst concavity'\n 'worst concave points' 'worst symmetry' 'worst fractal dimension']",
'[[ 5.80829151e+00 1.47482359e-02 -4.59988460e-01 -9.43196068e-05\n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n 6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00\n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n 0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02\n 0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00\n 0.00000000e+00 0.00000000e+00]]')]
在这种情况下,系数存储为字符串。我想使用Sqlite提取并将它们转换为数组,以便它们看起来像这样:
[[ 5.80829151e+00 1.47482359e-02 -4.59988460e-01 -9.43196068e-05
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02
0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00]]
我尝试使用cursor.fetchone()
,但它仅适用于第一行(即提取截距的值)。直接从包含系数的第二行中直接检索系数的sql / pythonic方法是什么?
答案 0 :(得分:0)
如果sql始终返回2行,则执行两次fetchone()
(这将丢弃第一行)。
如果sql将返回多个集合,请执行fetchall()
,遍历结果,并丢弃(忽略)偶数行