用pandas创建稀疏矩阵,并用.dat文件中索引[x,y]的一列中的值填充它

2024-06-03 10:53:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.dat文件,它包含三列-userIDartistIDweight。 将数据读入Python数据帧pd.read U表格('列车日期')。在

我想创建一个稀疏矩阵(/2D array),它将数据帧的前两列('userID,'artistID)的值作为索引,第三列作为值('weight)。 数据帧中未给出的索引组合应为NaN。在

我尝试创建一个空的numpy数组并使用for循环填充它,但这需要很多时间(大约有100k行在其中)列车日期). 在

import csv
import numpy as np

f = open("train.dat", "rt")
reader = csv.reader(f, delimiter="\t")
next(reader)
data = [d for d in reader]
f.close()

data = np.array(data, dtype=float)
col = int(a[:,0].max()) + 1
row = int(a[:,1].max()) + 1

empty = np.empty((row, col))
empty[:] = np.nan

for d in data:
   empty[int(d[0]), int(d[1])] = d[2]

还尝试创建一个coo_矩阵并将其转换为csr_矩阵(这样我就可以使用索引访问数据),但是索引重置了。在

^{pr2}$

数据示例:

userID    artistID  weight
    45           7      0.7114779874213837
   204         144      0.46399999999999997
    36         650      2.4232887490165225
   140         146      1.0146699266503667
   170          31      1.4124783362218372
   240         468      0.6529992406985573

Tags: 数据numpyfordatanp矩阵arrayreader
1条回答
网友
1楼 · 发布于 2024-06-03 10:53:24

将数据复制到文件:

In [290]: data = pd.read_csv('stack48133358.txt',delim_whitespace=True)
In [291]: data
Out[291]: 
   userID  artistID    weight
0      45         7  0.711478
1     204       144  0.464000
2      36       650  2.423289
3     140       146  1.014670
4     170        31  1.412478
5     240       468  0.652999
In [292]: M = sparse.csr_matrix((data.weight, (data.userID, data.artistID)))
In [293]: M
Out[293]: 
<241x651 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in Compressed Sparse Row format>
In [294]: print(M)
  (36, 650)     2.42328874902
  (45, 7)       0.711477987421
  (140, 146)    1.01466992665
  (170, 31)     1.41247833622
  (204, 144)    0.464
  (240, 468)    0.652999240699

我还可以使用genfromtxt加载该文件:

^{pr2}$

相关问题 更多 >