[英]How to create a 2d numpy array from a list of tuples
I have a large text file with three elements in each row - user, question, value.我有一个大文本文件,每行包含三个元素 - 用户、问题、值。 I would like to create a 2d numpy array from this data.
我想根据这些数据创建一个二维 numpy 数组。 The data sample is something like this:
数据样本是这样的:
114250 3 1
124400 7 4
111304 1 1
Unfortunately I don't know the size of the resulting matrix beforehand and thus cannot initialize it.不幸的是,我事先不知道结果矩阵的大小,因此无法对其进行初始化。
I managed to read the data into a list of 3-tuples with this code (converting the arbitrary user ids to linear 1,2,3... representation):我设法使用此代码将数据读入 3 元组列表(将任意用户 ID 转换为线性 1,2,3... 表示):
users = dict()
data = list()
for line in fileinput.input( args[0] ):
tokens = line.split("\t")
tokens = [ t.strip("\r").strip("\n") for t in tokens ]
user = tokens[0]
question = tokens[1]
response = tokens[2]
if user in users.keys():
user_id = users.get( user ) # existing user
else:
user_counter = user_counter + 1 # add new user
users[user] = user_counter
user_id = user_counter
data.append( (int(user_id), int(question), int(response)) )
I am not sure how to convert this list of tuples to a 2D numpy array.我不确定如何将此元组列表转换为二维 numpy 数组。 I would love to know how to do this in pythonic way.
我很想知道如何以 Pythonic 的方式做到这一点。
There should be some method which will read every tuple, get user_id and question as column,row and put the response value in that 2D numpy array.应该有一些方法可以读取每个元组,获取 user_id 和问题作为列,行并将响应值放入该 2D numpy 数组中。 For example a tuple like
例如像这样的元组
(10,3,1)
means that I would like to put the value 1 into a 2D matrix row 10, column 3.意味着我想将值 1 放入二维矩阵第 10 行、第 3 列。
import numpy
data = []
with open('filename', 'r') as f:
for line in f:
data.append(map(int, line.strip().split()))
r, c = max(data, key=lambda x: x[0]), max(data, key=lambda x: x[1])
A = numpy.zeros(shape = (r+1, c+1))
for i,j, val in data:
A[i][j] = val
I haven't tried this, but should work.我没有试过这个,但应该工作。 Note that the indexing starts from 0.
请注意,索引从 0 开始。
Simply generate the matrix afterwards:之后只需生成矩阵:
import numpy as np
data = numpy.array(data)
result = numpy.zeros(shape=(data[:,0].max()+1, data[:,1].max()+1), dtype=int)
result[data[:,0], data[:,1]] = data[:,2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.