ValueError:使用 pandas hub_table 不允许使用负尺寸

2024-02-11

我正在尝试制作项目-项目协作推荐代码。我的完整数据集可以找到here https://drive.google.com/open?id=0B7KsvcvcTJ4DeU1IRE93ZHVnRGM。我希望用户成为行,项目成为列,评级成为值。

我的代码如下:

import pandas as pd     
import numpy as np   
file = pd.read_csv("data.csv", names=['user', 'item', 'rating', 'timestamp'])
table = pd.pivot_table(file, values='rating', index=['user'], columns=['item'])

我的数据如下:

             user        item  rating   timestamp
0  A2EFCYXHNK06IS  5555991584       5   978480000  
1  A1WR23ER5HMAA9  5555991584       5   953424000
2  A2IR4Q0GPAFJKW  5555991584       4  1393545600
3  A2V0KUVAB9HSYO  5555991584       4   966124800
4  A1J0GL9HCA7ELW  5555991584       5  1007683200

错误是:

Traceback (most recent call last):  
  File "D:\python\reco.py", line 9, in <module>   
    table=pd.pivot_table(file,values='rating',index=['user'],columns=['item'])  
  File "C:\python35\lib\site-packages\pandas\tools\pivot.py", line 133, in   pivot_table     
        table = agged.unstack(to_unstack)   
  File "C:\python35\lib\site-packages\pandas\core\frame.py", line 4047, in       unstack  
    return unstack(self, level, fill_value)
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 402, in   unstack      
    return _unstack_multiple(obj, level)    
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 297, in   _unstack_multiple  
    unstacked = dummy.unstack('__placeholder__')  
  File "C:\python35\lib\site-packages\pandas\core\frame.py", line 4047, in   unstack  
    return unstack(self, level, fill_value)  
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 406, in   unstack  
    return _unstack_frame(obj, level, fill_value=fill_value)  
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 449, in   _unstack_frame  
    fill_value=fill_value)  
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 103, in   __init__  
    self._make_selectors()  
  File "C:\python35\lib\site-packages\pandas\core\reshape.py", line 137, in   _make_selectors  
    mask = np.zeros(np.prod(self.full_shape), dtype=bool)  
ValueError: negative dimensions are not allowed

我不能保证这会完成(我厌倦了等待它计算),但这里有一种创建稀疏数据帧的方法,希望能够最大限度地减少内存并提供帮助。

import pandas as pd
import numpy as np
file=pd.read_csv("data.csv",names=['user','item','rating','timestamp'])

from scipy.sparse import csr_matrix

user_u = list(sorted(file.user.unique()))
item_u = list(sorted(file.item.unique()))

row = file.user.astype('category', categories=user_u).cat.codes
col = file.item.astype('category', categories=item_u).cat.codes

data = file['rating'].tolist()

sparse_matrix = csr_matrix((data, (row, col)), shape=(len(user_u), len(item_u)))

df = pd.SparseDataFrame([ pd.SparseSeries(sparse_matrix[i].toarray().ravel(), fill_value=0) 
                              for i in np.arange(sparse_matrix.shape[0]) ], 
                       index=user_u, columns=item_u, default_fill_value=0)

See 这个问题 https://stackoverflow.com/questions/31661604/efficiently-create-sparse-pivot-tables-in-pandas以获得更多选择。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ValueError:使用 pandas hub_table 不允许使用负尺寸 的相关文章

随机推荐

  • 如何让用户使用 Android 中的 Intent 仅选择本地文件?

    我需要让用户从本地存储中为我的应用程序选择一个文件 现在我正在使用INTENT ACTION GET CONTENT让用户选择文件 但它也提供了从云中选择 URI 的选项 获取 URI 文件后 我将其视为本地文件并执行各种操作 包括提取文件
  • 将组合框显示文本设置为 ObservableCollection 的属性

    我有以下集合 我想将其绑定到组合框 public ObservableCollection
  • 使用 jquery.each() 循环创建对象键

    我正在 javascript 中创建一个命名空间来循环遍历表单并创建一个对象 调用该函数的目标是循环遍历所有特定表单类型并构造一个对象 该对象的键是 html 输入的名称 值是其当前值 但是 它始终返回未定义 任何帮助将不胜感激 get f
  • 尽管符号存在,但 CMake 中的符号查找失败

    我正在尝试使用 CMake 检查符号 getaddrinfo a 是否存在 list APPEND CMAKE REQUIRED DEFINITIONS D GNU SOURCE check symbol exists getaddrinf
  • 如何用C++扩展TCL?

    我可以编写可编译并用于扩展 TCL 的 C 代码 我不是指调用可执行文件 吗 我可以描述一些类 函数并通过调用已编译的 so 或 a 文件 C 代码在我的 TCl 代码中使用它们吗 如果是 请示意性地向我解释一下它是如何完成的 http c
  • CodeFirst 相对于 Database First 的优势是什么?

    我正在观看 EF 4 1 的一些视频和教程 我不明白 CodeFirst 的任何好处 除了一些 DB 非常小的 3 4 个表并且我懒得先创建 DB 的情况 大多数情况下 迄今为止最好的方法是在某种数据库编辑器中创建数据库 这肯定比在实体模型
  • Python:如何用常规属性替换属性?

    基类有这样的 def management form self code here return form management form property management form 在我的派生类中 我试图这样写 self manag
  • CSS无冲突样式

    创建 CSS 样式表的无冲突版本的好方法是什么 假设您有一堆代码 其中的类与 Bootstrap 的类重叠 这是否有效 添加一个class bootstrap 到应应用引导样式的祖先元素 然后更改bootstrap css为每个添加前缀ru
  • JXA:从 CoreServices 访问 CFString 常量

    JXA https developer apple com library mac releasenotes InterapplicationCommunication RN JavaScriptForAutomation Articles
  • XML 数据类型方法“value”的参数 1 必须是字符串文字

    我已经读过SO XML 数据类型方法 值 必须是字符串文字 https stackoverflow com q 11029674 62576但我的问题有点不同 我在一个变量中有一些 xml 我想将其分开并给出一个路径 最初我尝试过这个 de
  • 带有组的pivot_longer并删除列名前缀[重复]

    这个问题在这里已经有答案了 我正在尝试使用pivot longer 将数据帧从宽格式转换为长格式 原始数据框是 df start lt structure list amprise 0 015 ampfull 0 005 ampfall 0
  • 桶的索引计数

    所以 这是我的小问题 Let s say I have a list of buckets a0 an which respectively contain L lt c0 cn lt H items I can decide of the
  • 使用 tastypie 的 REST url

    我在 django 应用程序中使用 tastypie 并尝试让它映射像 api booking 2011 01 01 这样的 URL 该 URL 映射到 URL 中具有指定时间戳的 Booking 模型 该文档没有说明如何实现这一点 您想要
  • 如何在离开用户界面状态时停止 $interval?

    Angular UI 路由器 在状态控制器中使用 interval 如下所示 scope Timer null scope startTimer function scope Timer interval scope Foo 30000 s
  • 使用 cmd.exe /c 相对于根本不使用 cmd 的优点

    如果我在 C 中运行一个进程 使用 cmd c 比直接运行进程有什么好处吗 Ex ProcessStartInfo info new ProcessStartInfo cmd exe info Arguments c application
  • 区分键盘的真实按键和虚拟按键

    我正在用 C 编写一个程序 它必须模拟键盘的按键命令 问题是 我需要模拟 真实 的键盘按钮点击 而不是假的 例如 据我了解 当用户按下键盘上的任何按钮时 信号通过 USB 发送 然后通过键盘驱动程序继续 当使用 API SendInput
  • 将 async/await 与 DataReader 一起使用? (没有中间缓冲区!)

    我的目标很简单 我想做异步 I O 调用 使用 async wait 但是 不使用 DataFlow 依赖项 就像这个答案一样 https stackoverflow com a 13148683 859154 没有中间缓冲区 不喜欢这个答
  • 在 WPF Frame 控件中关闭导航页面声音

    我在 WPF 窗口中有一个 Frame 元素 并且应用程序中的所有内容都是使用 Frame Navigate 加载的 但是 当导航到新页面时 默认会播放该声音 它与 Internet Explorer 中播放的声音相同 有什么方法可以从应用
  • Visual Studio 2015 中的默认日期时间选择器仅显示日期选择器,不允许选择时间

    我正在使用 Visual Studio 2015 并且在我的 MVC5 应用程序中使用其默认日期时间选择器 我在显示日期时间选择器时遇到问题 它仅显示日期选择器而不显示时间选择器 但我也需要时间选择器 这是我在模型中的 datetimepi
  • ValueError:使用 pandas hub_table 不允许使用负尺寸

    我正在尝试制作项目 项目协作推荐代码 我的完整数据集可以找到here https drive google com open id 0B7KsvcvcTJ4DeU1IRE93ZHVnRGM 我希望用户成为行 项目成为列 评级成为值 我的代码