Extract single value from sqlite3 database
I have a database with regression model intercepts, coefficients. I would like to extract the coefficients and convert them into a numpy array.
cursor.execute("""SELECT * FROM model_coeffs WHERE desc = 'L1 penalty model'""")
cursor.fetchall()
The output is shown below:
[(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-05n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02n 0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00]]')]
In this case, the coefficients are stored as a string. I would like to extract using Sqlite and convert them into an array so they look like such:
[[ 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]]
I tried using cursor.fetchone(), but it only works for the first row (i.e extracting the value of the intercept). Whats a sql/pythonic way to retrieve the coefficients directly from the second row containing the coefficients?
python-3.x sqlite
add a comment |
I have a database with regression model intercepts, coefficients. I would like to extract the coefficients and convert them into a numpy array.
cursor.execute("""SELECT * FROM model_coeffs WHERE desc = 'L1 penalty model'""")
cursor.fetchall()
The output is shown below:
[(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-05n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02n 0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00]]')]
In this case, the coefficients are stored as a string. I would like to extract using Sqlite and convert them into an array so they look like such:
[[ 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]]
I tried using cursor.fetchone(), but it only works for the first row (i.e extracting the value of the intercept). Whats a sql/pythonic way to retrieve the coefficients directly from the second row containing the coefficients?
python-3.x sqlite
add a comment |
I have a database with regression model intercepts, coefficients. I would like to extract the coefficients and convert them into a numpy array.
cursor.execute("""SELECT * FROM model_coeffs WHERE desc = 'L1 penalty model'""")
cursor.fetchall()
The output is shown below:
[(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-05n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02n 0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00]]')]
In this case, the coefficients are stored as a string. I would like to extract using Sqlite and convert them into an array so they look like such:
[[ 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]]
I tried using cursor.fetchone(), but it only works for the first row (i.e extracting the value of the intercept). Whats a sql/pythonic way to retrieve the coefficients directly from the second row containing the coefficients?
python-3.x sqlite
I have a database with regression model intercepts, coefficients. I would like to extract the coefficients and convert them into a numpy array.
cursor.execute("""SELECT * FROM model_coeffs WHERE desc = 'L1 penalty model'""")
cursor.fetchall()
The output is shown below:
[(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-05n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 6.72375188e-01 -5.25653647e-02 0.00000000e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00n 0.00000000e+00 -2.04390704e-01 -1.03770307e-01 -2.79521678e-02n 0.00000000e+00 0.00000000e+00 -2.37746363e+00 0.00000000e+00n 0.00000000e+00 0.00000000e+00]]')]
In this case, the coefficients are stored as a string. I would like to extract using Sqlite and convert them into an array so they look like such:
[[ 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]]
I tried using cursor.fetchone(), but it only works for the first row (i.e extracting the value of the intercept). Whats a sql/pythonic way to retrieve the coefficients directly from the second row containing the coefficients?
python-3.x sqlite
python-3.x sqlite
edited Nov 27 '18 at 15:33
Workhorse
asked Nov 27 '18 at 15:17
WorkhorseWorkhorse
462511
462511
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If the sql will always return 2 rows, then do fetchone() twice, (this discarding the first row).
If the sql will return mulitple sets, do a fetchall(), iterate over the results, and discard (ignore) the even numbered rows
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53502737%2fextract-single-value-from-sqlite3-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If the sql will always return 2 rows, then do fetchone() twice, (this discarding the first row).
If the sql will return mulitple sets, do a fetchall(), iterate over the results, and discard (ignore) the even numbered rows
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
add a comment |
If the sql will always return 2 rows, then do fetchone() twice, (this discarding the first row).
If the sql will return mulitple sets, do a fetchall(), iterate over the results, and discard (ignore) the even numbered rows
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
add a comment |
If the sql will always return 2 rows, then do fetchone() twice, (this discarding the first row).
If the sql will return mulitple sets, do a fetchall(), iterate over the results, and discard (ignore) the even numbered rows
If the sql will always return 2 rows, then do fetchone() twice, (this discarding the first row).
If the sql will return mulitple sets, do a fetchall(), iterate over the results, and discard (ignore) the even numbered rows
answered Nov 27 '18 at 16:27
DinoCoderSaurusDinoCoderSaurus
1,004158
1,004158
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
add a comment |
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
Thank you, this worked.
– Workhorse
Nov 27 '18 at 16:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53502737%2fextract-single-value-from-sqlite3-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown