- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到的本质上是分配问题。
我有什么:我对小的地理区域有观察,比如人口普查区。对于每个人,我都有四个不同年龄组的人数。每个区域属于一个子区域。
现在,我知道小区域分布并不完全正确,因为我知道正确分布——在更高级别的聚合、子区域级别和更精细的区域级别数据,求和时,显示不同的组总数。
我想要的东西:我想调整我的 tract-level,分解分布在四个组中,因此它与已知正确的这四个组中的汇总级分布一致,但保留 tract-level 分布的信号——即根据更粗略的数据对其进行调整,但不要将其扔出窗外。
然后,我想做的是改变边缘地区的人口数量,满足以下标准,前两个是最重要的(我意识到在满足所有这些方面需要权衡取舍) :
下面是模拟数据和占位符代码:
n=1000
np.random.seed(123)
df_small_area_scale = pd.DataFrame(data={
'grp1':np.random.randint(10,250,n),
'grp2':np.random.randint(10,250,n),
'grp3':np.random.randint(10,250,n),
'grp4':np.random.randint(10,250,n),
'subregion': np.random.choice(['A', 'B', 'C', 'D', 'E'],n),
'tract_id':range(1000)}).set_index(['subregion','tract_id'])
df_small_area_scale.head()
grp1 grp2 grp3 grp4
subregion tract_id
B 0 119 85 11 19
D 1 136 100 46 239
A 2 76 26 198 109
B 3 230 180 84 222
A 4 108 101 222 244
并且,通过 subregion
聚合我们得到这个:
df_small_area_scale.groupby(level=0).sum()
grp1 grp2 grp3 grp4
subregion
A 27241 27050 27471 26215
B 26507 24696 23315 24857
C 27474 28871 28882 28743
D 26671 26163 25077 27612
E 22739 23077 23797 24473
(让我们得到每个组中每个次区域的目标份额)
summary_area_scale_shares = summary_area_scale.stack().groupby(level=0).apply(lambda x: x/float(x.sum()))
summary_area_scale_shares.head()
subregion
A grp1 0.244444
grp2 0.266667
grp3 0.244444
grp4 0.244444
B grp1 0.255319
dtype: float64
这些是次区域“已知”分布。我希望将区域级数据调整到这些,以便在聚合区域时,它们大致匹配每个组中的这些区域总数。具体来说,subregion A
中的 grp4
总数为 26,215,但根据目标,它应该是 22,000,因此在 subregion A 中的区域应该会看到 persons re -从 grp4
分类到其他一些组。
summary_area_scale = pd.DataFrame(data={'grp1':[22000,24000,21000,25000,28000],
'grp2':[24000,22000,26000,20000,28000],
'grp3':[22000,24000,21000,25000,28000],
'grp4':[22000,24000,21000,25000,28000],
'subregion':list('ABCDE')}).set_index('subregion')
summary_area_scale
grp1 grp2 grp3 grp4
subregion
A 22000 24000 22000 22000
B 24000 22000 24000 24000
C 21000 26000 21000 21000
D 25000 20000 25000 25000
E 28000 28000 28000 28000
一个想法是在每个次区域内抽样,然后按照需要从一个箱子转移到另一个箱子的总人数的一定比例移动人员,尽管我不确定是否有一种聪明的方法可以满足上述标准。
给我带来问题的主要是确定一种方法来跨组重新分配人员以匹配次区域总数,同时保持创纪录的总数,而不是完全抛弃我想保留的先前存在的空间分布信号(但调整为现在已知的不同整体分布)。
一般而言,关于如何使细节分布更适合总体分布的任何想法,而不仅仅是抽样和移动 x 人从 grp4 -> grp3
,grp2 -> grp1
以及现有和目标分布之间的区别是什么?
此函数主要是查找包含每个组中的区域份额的表,将该分布推送到每个区域,因此它除了设置数据绑定(bind)外什么都不做。
def some_redistribution_algorithm(df):
"""
how many persons need to be moved across groups in each subregion?
minimal solution is to just take those shifts and apply uniformly
tracts keep the same counts, but the *distribution* across bins will change slightly
Quality criteria for algorithm:
- switch population at tract level such that
- tract-level population counts maintained
- Pre- and post-adjustment spatial distribution be largely unchanged
- change is not disproportional / dramatically impacting some tracts over others
(i.e. a tract with 10 grp4 population losing 8 would lose 80%, while a tract with 100 q4 hhs would lose 8%)
"""
adjustments = summary_area_scale.xs(df.name)
size = (adjustments).apply(lambda x: abs(x)).loc['grp4'].astype(np.int64)/df.shape[0]
print "Processing %s (%s tracts), beg. pop: %s, avg pop to move (here q4) %s" %(df.name,df.shape[0],
df.sum().loc['grp4'].astype(np.int64),size)
print 'Average pop per tract:'
print df.sum()/df.shape[0]
## tract-level distribution, if all tracts had the same distribution within each subregion (placeholder)
return df_small_area_scale.xs(df.name).mul(summary_area_scale_shares.unstack().xs(df.name),axis=1)
#samplerows= np.random.choice(a=df.index, size=size)
#df.loc[samplerows,:] = df.loc[samplerows,:]#, p=df.totalshare.tolist()),:]
df_small_area_scale.groupby(level=0).apply(some_redistribution_algorithm)
最佳答案
如果我正确理解您的问题,我认为迭代比例拟合可能就是您要寻找的。如果可以的话,我会陈述我最近遇到的类似问题。这是我试图解决的问题:
我知道大都市级别的年龄分布,我知道每个区域的总人数,但由于人口普查的工作方式,我想我知道每个区域的年龄分布,但我不确定.
我知道我想满足区域内的总人口(行边缘),我知道我想满足大都市级别的年龄分布(列边缘),我想用分布“播种”ipf在每个小册子中,这是我对答案的最佳猜测。当然它不起作用(我的意思是数字不会加起来),所以你立即偏离那个猜测以满足边缘。这就是迭代比例拟合的目的。
也许不是防弹的,但我使用的代码(在 Python/numpy 中)是这样的:
# this should be fairly self explanitory if you know ipf
# seed_matrix is your best bet at the totals, col_marginals are
# observed column marginals and row_marginals is the same for rows
def simple_ipf(seed_matrix, col_marginals, row_marginals, tolerance=1, cnt=0):
assert np.absolute(row_marginals.sum() - col_marginals.sum()) < 5.0
# first normalize on columns
ratios = col_marginals / seed_matrix.sum(axis=0)
seed_matrix *= ratios
closeness = np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum()
assert np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum() < .01
# print "row closeness", closeness
if closeness < tolerance:
return seed_matrix
# first normalize on rows
ratios = row_marginals / seed_matrix.sum(axis=1)
ratios[row_marginals == 0] = 0
seed_matrix = seed_matrix * ratios.reshape((ratios.size, 1))
assert np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum() < .01
closeness = np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum()
# print "col closeness", closeness
if closeness < tolerance:
return seed_matrix
if cnt >= 50:
return seed_matrix
return simple_ipf(seed_matrix, col_marginals, row_marginals,
tolerance, cnt+1)
关于python - 改变分解分布以匹配更多的聚合水平分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357409/
我正在开发一个带选项卡栏的 ios 应用程序。我的栏上有超过 5 个按钮,所以在 iphone 上我有更多的按钮。现在,假设我有这个按钮:Button1 Button2 Button3 Button4
我有一个带有 UITabBarController 的应用,其中有超过五个选项卡。 当我按更多选项卡时,我会转到moreNavigationController,它是一个UINavigationCon
我有一个导航 Controller 。 NAVC->MORE... 按钮,然后在“更多”下有一些额外的 VC。 如果我转到“更多...”下的 VC,然后转到不在“更多...”上的 VC,那么当我返回到
因此,我想出了这种方案,用于在多个线程同时具有读写访问权限的二叉树中旋转时锁定节点,这涉及每次旋转锁定四个节点,这似乎是一个很多吗?我想到了一种比我想出的方法更聪明的方法来减少所需的锁定,但谷歌并没有
所以我已经尝试了所有方法,但我似乎仍然无法将下拉内容与 dropbtn 对齐。我只希望内容始终位于更多菜单下方。 HTML: `
我正在尝试使用 expect 来自动接受在 --more-- 中提示的 EULA。 #!/usr/bin/expect spawn "./greenplum-perfmon-web-4.1.2.0-b
他们如何在下面提供的网站上制作“告诉我更多”效果。我读过 read more/less effect in jQuery,但我发现该站点的有趣之处在于,除非单击该按钮,否则无法滚动页面。 Effect
现在,Kim Stebel helped me understanding如何使用存在类型键入变量,我需要知道如何在继承中使用它们: 以下代码无法编译: class PagingListModel(s
在我的Cygwin中不可用。另一方面,提供了“ less”命令。也许Cygwin的制造商认为“更多”只是多余的。 我对此很好奇。 最佳答案 安装util-linux软件包,您将获得“更多”的信息 ht
基本上,我想知道是否有人有增加 DTU 与分片的经验。 DTU应该线性地提高性能。因此,如果您有 5 个 DTU,而您改为 10 个 DTU,那么(理论上)您应该获得大约两倍的性能。 因此,四个 SQ
我们使用 asp.net mvc、javascript 和 jQuery(托管在本地计算机上)创建了一个应用程序。基本设计是,当用户从一个页面导航到其他页面时,我们通过隐藏和显示 HTML 页面,将所
我想用 RMonad 做一些基本的事情。有没有办法使用“as monad”功能来 有一个身份 rmonad,可以应用 monad 转换器吗? 有诸如 StateT 变压器之类的常见东西吗? 向现有 m
我有一个 char*[] 数组。我需要能够为其分配字符串并再次删除它们,但我不知道: 如何检查一个元素中是否已经有一个字符串,这样我就不会覆盖它,如果它已经被占用,则继续处理下一个元素? 之后如何将其
基本上,我想知道是否有人有增加 DTU 与分片的经验。 DTU应该线性地提高性能。因此,如果您有 5 个 DTU,而您改为 10 个 DTU,那么(理论上)您应该获得大约两倍的性能。 因此,四个 SQ
我有一个程序可以同时吐出标准错误和标准输出,我想在标准错误上少运行寻呼机,但忽略标准输出。我该怎么做? 更新: 就是这样......我不想丢失标准输出......只是让它远离寻呼机 program 2
基本上,当单击具有类 "dropdown" 的链接时,我无法获取“更多...”链接来对下一个跨度的高度进行动画处理。它根本就没有动画。仅当更改为 Less... 链接并且单击 Less... 链接以折
我正在使用 ExtJS,并认为它是一个了不起的框架。但是,它们没有内置的状态图,这使得依赖于状态的应用程序开发非常痛苦。 我最近发现了这个: https://github.com/jakesgordo
我一直在研究数据结构和算法,遗憾的是在C中。我已经单独实现了一个双向链表,它保存整数并且工作正常,但是当节点(或pub)让它正常工作时我遇到了很多麻烦在本例中)保存多个不同类型的值。我可以创建一个列表
编辑拼写错误 你好, 这可能是一个愚蠢的问题,但如果它能帮助我遵循最佳实践,我不在乎:P 假设我想在 System.Data 命名空间...以及 System.Data.SqlClient 命名空间中
使用 bootstrap 3 CSS、font awesome CSS 和最新的 jQuery JS 文件。 我正在使用 javascript 在单击按钮时在另一个内容 div 之上隐藏/显示一个内容
我是一名优秀的程序员,十分优秀!