Extract single value from sqlite3 database












0















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?










share|improve this question





























    0















    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?










    share|improve this question



























      0












      0








      0








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 15:33







      Workhorse

















      asked Nov 27 '18 at 15:17









      WorkhorseWorkhorse

      462511




      462511
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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






          share|improve this answer
























          • Thank you, this worked.

            – Workhorse
            Nov 27 '18 at 16:28











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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






          share|improve this answer
























          • Thank you, this worked.

            – Workhorse
            Nov 27 '18 at 16:28
















          0














          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






          share|improve this answer
























          • Thank you, this worked.

            – Workhorse
            Nov 27 '18 at 16:28














          0












          0








          0







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 16:27









          DinoCoderSaurusDinoCoderSaurus

          1,004158




          1,004158













          • 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





          Thank you, this worked.

          – Workhorse
          Nov 27 '18 at 16:28




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Lallio

          Futebolista

          Jornalista