在 MySQL 中将标准化表导出为矩阵格式
为了提供一些背景知识,我正在模拟不同的房屋属性如何影响周围房屋的价格。为此,我有一个包含 4,000 套房屋销售情况和每套房屋的人口统计数据的表。我正在生成一个值作为每对房屋之间“可替代性”的衡量标准。
创建这个时我的第一个想法是在 Excel 中构建一个 4000x4000 矩阵。然而,一旦我的计算变得有点复杂,我很快就会遇到内存限制。
现在我已将交易放入上述表中,我想构建一个“交互”表,其中每一行都是一对房屋,其中一列作为可替代性度量。
表结构看起来像:
House1ID House2ID SubIndex
1 2 400
1 3 450
2 3 500
我的问题是我正在使用的统计软件包需要以下格式的数据:
1 2 3
1 0 400 450
2 400 0 500
3 450 500 0
有没有办法从上面提到的表结构到所需的矩阵输出?
非常感谢所有帮助!
编辑: 经过更多调查后,我意识到我现在可以使用交叉联接通过视图生成规范化的“表”。虽然这根本没有真正改变问题,但我认为这很有趣,并认为我会指出我正在使用的方法。
To give some background, I am modeling how different home attributes affect the prices of the homes around them. To do this, I have a table of 4,000 home sales and the demographics of each home. I'm generating a value as a measure of "substitutability" between each pair of homes.
My first thought in creating this was to build a 4000x4000 matrix in Excel. However, once my calculations got a little hairy, I bumped up against memory limits pretty quickly.
So now I've put the transactions into the above mentioned table and I want to build an "interaction" table, with each row being a house pair with one column as the substitutability measure.
The table structure would look something like:
House1ID House2ID SubIndex
1 2 400
1 3 450
2 3 500
My problem is that the statistical software package I'm using wants the data in the following format:
1 2 3
1 0 400 450
2 400 0 500
3 450 500 0
Is there a way to go from the table structure mentioned above to the matrix output needed?
All help is greatly appreciated!
EDIT:
After a little more investigation, I've realized I can at generate the normalized "table" now with a View using a Cross Join. While this doesn't really change the question at all, I thought it interesting and figured I would point out the method I was using.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会考虑使用 view 来获取数据以您需要的格式呈现。
这里有一些关于使用视图的优秀示例和提示。
I would look into using a view to have the data presented in the format you need.
There are some great examples and hints on using views here.