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

Workaholic Formal/Informal

How do I go from 300 unfinished/half written blog posts, to published posts?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

A "random" question: usage of "random" as adjective in Spanish

Sending manuscript to multiple publishers

Why does standard notation not preserve intervals (visually)

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

What is ( CFMCC ) on ILS approach chart?

"and that skill is always a class skill for you" - does "always" have any meaning in Pathfinder?

Extending anchors in TikZ

Won the lottery - how do I keep the money?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Can I equip Skullclamp on a creature I am sacrificing?

Contours of a clandestine nature

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

Why am I allowed to create multiple unique pointers from a single object?

What exact does MIB represent in SNMP? How is it different from OID?

What flight has the highest ratio of time difference to flight time?

Preparing Indesign booklet with .psd graphics for print

Why do airplanes bank sharply to the right after air-to-air refueling?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Is there a difference between "Fahrstuhl" and "Aufzug"

Why has the US not been more assertive in confronting Russia in recent years?

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



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 5 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

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER编

          波兰旗帜列表 目录 国旗 军旗 其他制服部门旗帜 特别国家机构船只 参考文献 外部链接 导航菜单Polskie flagi, chorągwie, bandery... [波兰旗帜、条幅、船旗等]原始内容Ustawa z dnia 31 stycznia 1980 r. o godle, barwach i hymnie Rzeczypospolitej Polskiej oraz o pieczęciach państwowychZarządzenie Ministra Obrony Narodowej z dnia 14 grudnia 2005 r. zmieniające zarządzenie w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejZarządzenie Ministra Obrony Narodowej z dnia 29 stycznia 1996 r. w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejUstawa z dnia 19 lutego 1993 r. o znakach Sił Zbrojnych Rzeczypospolitej PolskiejHistoria Marynarki Wojennej RP [波兰海军史]Rozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 12 kwietnia 2002 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych Straży GranicznejRozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 18 kwietnia 2005 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych PolicjiRozporządzenie Ministra Infrastruktury z dnia 21 października 2005 r. w sprawie wzorów flag dla statków morskich na oznaczenie pełnionej specjalnej służby państwowej oraz okoliczności i warunków ich podnoszenia波兰旗帜波兰编

          Indenting and Dedenting ASP code with Python