Apply function to every subset combination and return square matrix The Next CEO of Stack OverflowMatrix transpose functionString Matching and ClusteringSquare spiral matrixShrinking and expanding squareComputing rowMeans for every combination of columnsPython Pandas Apply with a Lambda FunctionLongest sequence of same subsequent number in a square matrixCreating every possible combination until a code word is foundFilling a matrix with square number digitsGroupby, apply custom function to data, return results in new columns

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

What does "Its cash flow is deeply negative" mean?

How to make a variable always equal to the result of some calculations?

Would a galaxy be visible from outside, but nearby?

What happened in Rome, when the western empire "fell"?

What benefits would be gained by using human laborers instead of drones in deep sea mining?

Why didn't Khan get resurrected in the Genesis Explosion?

Giving the same color to different shapefiles in QGIS

What was the first Unix version to run on a microcomputer?

How to solve a differential equation with a term to a power?

In excess I'm lethal

Preparing Indesign booklet with .psd graphics for print

How to transpose the 1st and -1th levels of arbitrarily nested array?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Is it possible to search for a directory/file combination?

Won the lottery - how do I keep the money?

If the heap is initialized for security, then why is the stack uninitialized?

How fast would a person need to move to trick the eye?

Is micro rebar a better way to reinforce concrete than rebar?

Novel about a guy who is possessed by the divine essence and the world ends?

What is ( CFMCC ) on ILS approach chart?

multiple labels for a single equation

What can we do to stop prior company from asking us questions?



Apply function to every subset combination and return square matrix



The Next CEO of Stack OverflowMatrix transpose functionString Matching and ClusteringSquare spiral matrixShrinking and expanding squareComputing rowMeans for every combination of columnsPython Pandas Apply with a Lambda FunctionLongest sequence of same subsequent number in a square matrixCreating every possible combination until a code word is foundFilling a matrix with square number digitsGroupby, apply custom function to data, return results in new columns










0












$begingroup$


I don't know how to do this without four nested for loops.



I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



I took a stab at compression lists but that seems excessive too.



Note: this code runs but will produce NA because all possible combinations are not available.



Sample data



dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


Code



def mean_diff(x, y):
x = pd.Series(x)
y = pd.Series(y)
return x.mean() - y.mean()


dmat = pd.DataFrame()
for i in dat['hour'].unique():
for j in dat['hour'].unique():
for k in dat['day'].unique():
for l in dat['day'].unique():
x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

# Calculate difference
jds = mean_diff(x, y)

# Build data frame and append
outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
dmat = dmat.append(outdat, ignore_index=True)

# Pivot data to get matrix
distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








share









$endgroup$
















    0












    $begingroup$


    I don't know how to do this without four nested for loops.



    I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



    I took a stab at compression lists but that seems excessive too.



    Note: this code runs but will produce NA because all possible combinations are not available.



    Sample data



    dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


    Code



    def mean_diff(x, y):
    x = pd.Series(x)
    y = pd.Series(y)
    return x.mean() - y.mean()


    dmat = pd.DataFrame()
    for i in dat['hour'].unique():
    for j in dat['hour'].unique():
    for k in dat['day'].unique():
    for l in dat['day'].unique():
    x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
    y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

    # Calculate difference
    jds = mean_diff(x, y)

    # Build data frame and append
    outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
    dmat = dmat.append(outdat, ignore_index=True)

    # Pivot data to get matrix
    distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








    share









    $endgroup$














      0












      0








      0





      $begingroup$


      I don't know how to do this without four nested for loops.



      I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



      I took a stab at compression lists but that seems excessive too.



      Note: this code runs but will produce NA because all possible combinations are not available.



      Sample data



      dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


      Code



      def mean_diff(x, y):
      x = pd.Series(x)
      y = pd.Series(y)
      return x.mean() - y.mean()


      dmat = pd.DataFrame()
      for i in dat['hour'].unique():
      for j in dat['hour'].unique():
      for k in dat['day'].unique():
      for l in dat['day'].unique():
      x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
      y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

      # Calculate difference
      jds = mean_diff(x, y)

      # Build data frame and append
      outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
      dmat = dmat.append(outdat, ignore_index=True)

      # Pivot data to get matrix
      distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








      share









      $endgroup$




      I don't know how to do this without four nested for loops.



      I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



      I took a stab at compression lists but that seems excessive too.



      Note: this code runs but will produce NA because all possible combinations are not available.



      Sample data



      dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


      Code



      def mean_diff(x, y):
      x = pd.Series(x)
      y = pd.Series(y)
      return x.mean() - y.mean()


      dmat = pd.DataFrame()
      for i in dat['hour'].unique():
      for j in dat['hour'].unique():
      for k in dat['day'].unique():
      for l in dat['day'].unique():
      x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
      y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

      # Calculate difference
      jds = mean_diff(x, y)

      # Build data frame and append
      outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
      dmat = dmat.append(outdat, ignore_index=True)

      # Pivot data to get matrix
      distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')






      python performance





      share












      share










      share



      share










      asked 2 mins ago









      AmstellAmstell

      8111




      8111




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f216508%2fapply-function-to-every-subset-combination-and-return-square-matrix%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f216508%2fapply-function-to-every-subset-combination-and-return-square-matrix%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

          बाताम इन्हें भी देखें सन्दर्भ दिक्चालन सूची1°05′00″N 104°02′0″E / 1.08333°N 104.03333°E / 1.08333; 104.033331°05′00″N 104°02′0″E / 1.08333°N 104.03333°E / 1.08333; 104.03333

          Why is the 'in' operator throwing an error with a string literal instead of logging false?Why can't I use switch statement on a String?Python join: why is it string.join(list) instead of list.join(string)?Multiline String Literal in C#Why does comparing strings using either '==' or 'is' sometimes produce a different result?How to initialize an array's length in javascript?How can I print literal curly-brace characters in python string and also use .format on it?Why does ++[[]][+[]]+[+[]] return the string “10”?Why is char[] preferred over String for passwords?Why does this code using random strings print “hello world”?jQuery.inArray(), how to use it right?

          How can we generalize the fact of finite dimensional vector space to an infinte dimensional case?$k[x]$-module and cyclic module over a finite dimensional vector spaceSubspace of a finite dimensional space is finite dimensionalIf V is an infinite-dimensional vector space, and S is an infinite-dimensional subspace of V, must the dimension of V/S be finite? ExplainWhy is an infinite dimensional space so different than a finite dimensional one?base for finite dimensional vector space is not infinite dimensional vector space?Any finite-dimensional vector space is the dual space of anotherHaving Trouble Understanding Meaning Of A Finite-Dimensional Vector SpaceProve that “Every subspaces of a finite-dimensional vector space is finite-dimensional”Ring as a finite dimensional Vector space over a field KQuestion regarding basis and dimension