我有一个2d numpy矩阵,想要计算以下测试统计量。
我有蛮力代码来执行此操作,但似乎应该有一个更通用的numpy解决方案适用于任何2D矩阵,使用<!DOCTYPE html>
<title>anonymized</title>
<body>
<div class="wrapper">
<div class="sideBar">
<p class="sideBarText">We are anonymized. We are here to provide top-quality service for all our clients, no matter their needs.</p>
</div>
<div class="headerImage">
<pre class="logoLink">anonymized</pre>
</div>
<div class="globalNav">
<ul>
<li class="globalNavItem">About Us</li>
<li class="globalNavItem">Services</li>
<li class="globalNavItem">Testimonials</li>
<li class="globalNavItem">Contact</li>
</ul>
</div>
<!-- <div class = "pageNav">
<ul style = "list-style: none;">
<li class = "pageNavHeader">Home</li>
<li class = "pageNavItem">Test1</li>
<li class = "pageNavItem">Test2</li>
<li class = "pageNavItem">Test3</li>
<li class = "pageNavItem">Test4</li>
<li class = "pageNavItem">Test5</li>
<li class = "pageNavItem">Test6</li>
</ul>
</div> -->
<p>The 1881 world tour of King Kalākaua of the Kingdom of Hawaii was his attempt to save the Hawaiian culture and population from extinction through the importation of a labor force from Asia-Pacific nations. His efforts brought the small island nation
to the attention of world leaders, but sparked rumors that the kingdom was for sale. In Hawaii there were critics who believed the labor negotiations were just his excuse to see the world. The 281-day trip gave him the distinction of being the first
monarch to circumnavigate the globe, just as his 1874 travels had made him the first reigning monarch to visit America and the first honoree of a state dinner at the White House. Kalākaua met with heads of state in Asia, the Mideast and Europe,
to encourage an influx of sugar plantation labor in family groups, as well as unmarried women as potential brides for Hawaii's existing contract laborers. While in Asia, he tried to forestall American ambitions by offering a plan to Emperor Meiji
for putting Hawaii under the protection of the Empire of Japan with an arranged marriage between his niece Kaiulani and a Japanese prince. On his visit to Portugal, he negotiated a treaty of friendship and commerce with Hawaii that would provide
a legal framework for the emigration of Portuguese laborers to Hawaii. The King had an audience in Rome with Pope Leo XIII and met with many of the crowned heads of Europe. Britain's Queen Victoria and the splendor of her royal life impressed him
more than any other monarchy; having been greatly affected by the ornate trappings of European sovereigns, he would soon have Hawaii's monarchy mirror that grandeur. The King traveled with no security guards; only a small group of personal friends
made the journey with him. Except for land transportation in cities, and two loaned ships in China and the US, his modes of transportation were seldom reserved exclusively for him. He shared regularly scheduled steamships and rail transport with
fare-paying passengers. On the Red Sea, he played cards and danced with other passengers. Like other tourists, he visited the white elephants of Siam, the Giza pyramid complex in Egypt, tourist sites in India, and museums in Europe. Along the way,
he exceeded his original budget, went shopping anyway, and sent letters back home. President James A. Garfield died four days before they arrived back in the United States, and Kalākaua paid a courtesy call to newly inaugurated President Chester
A. Arthur at the White House in Washington, D.C. There were no public or private appearances for the King in New York, only a day at Coney Island. Before leaving the eastern US, the King met with Thomas Edison to have a demonstration of electric
lights, and visited Virginia's Fort Monroe. He toured Hampton Normal and Agricultural School, and shopped for horses in Kentucky. The royal party boarded a train to California, where they were house guests of Claus Spreckels at his estate in Aptos
(near Santa Cruz), and had a few days of seeing the sights in the area before sailing back to Hawaii. Kalākaua was successful in jump-starting new immigration, with the first transplants arriving in Hawaii less than a year later. In the years that
followed, he began emulating the lifestyles of European royalty with expensive furnishings in Iolani Palace, a public coronation of himself, and a two-week public celebration of his birthday.</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="globalNavScrollLock.js"></script>
</body>
之类的东西。我无法理解。
np.diag()
在这种情况下,def bruteforce(m):
s = 0.0
for (i,j) in itertools.product(range(0,m.shape[0]),range(0,m.shape[0])):
if i<j:
n = (m[i,j]-m[j,i])**2
d = m[i,j]+m[j,i]
if float(d) != 0.:
s = s+(float(n)/float(d))
else:
return('NA')
return(s)
是整数的NxN矩阵。有没有办法在numpy中进行矢量化,避免像这样的暴力循环?
答案 0 :(得分:5)
如果m
是一个方阵,这将完成这项工作:
import numpy as np
np.sum((m-m.T)**2/(m+m.T))/2
这是一个涵盖分母中有0的情况的函数:
def find_s(m):
d=(m+m.T)
off_diag_indices=np.triu_indices(len(d),1)
if 0 in d[off_diag_indices]:
return 'NA'
else:
numerator=(m-m.T)**2
denominator=m+m.T
return np.sum(numerator[off_diag_indices]/denominator[off_diag_indices])
我使用off_diag_indices
的原因是因为我们实际上允许m+m.T
的对角线上有0,因为我们从不对对角线上的元素求和。