gpt4 book ai didi

python - 从 numpy 数组创建字符串列表(非循环解决方案)

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:44 25 4
gpt4 key购买 nike

我想拼凑一个新列表,它是使用 numpy 数组的两列的字符串。但是,如果不循环每个元素,我似乎无法让它工作:

import numpy as np
test_list = np.tile(np.array([[1,2],[3,4],[5,6]]),(100000,1))
print(test_list[:,0])
print(test_list[:,1])

def dumbstring(points):
# Loop through and append a list
string_pnts = []
for x in points:
string_pnts.append("X co-ordinate is %g and y is %g" % (x[0], x[1]))
return string_pnts

def dumbstring2(points):
# Prefill a list
string_pnts = [""] * len(points)
i = 0
for x in points:
string_pnts[i] = ("X co-ordinate is %g and y is %g" % (x[0], x[1]))
i += 1
return string_pnts

def numpystring(points):
return ("X co-ordinate is %g and y is %g" % (points[:,0], points[:,1]))

def numpystring2(point_x, point_y):
return ("X co-ordinate is %g and y is %g" % (point_x, point_y))

前两个有效(我原以为预填充会比附加更快,但看起来是一样的):

%timeit tdumbstring = dumbstring(test_list) # 239ms
%timeit tdumbstring2 = dumbstring2(test_list) # 239ms

但是,最后一个没有 - 我想知道是否没有办法向量化这个函数?

tnumpystring = numpystring(test_list) # Error
tnumpystring2 = numpystring2(test_list[:,0],test_list[:,1]) # Error

编辑:

我尝试了 Pandas,因为我实际上不需要 Numpy,但它有点慢:

import pandas as pd
df = pd.DataFrame(test_list)
df.columns = ['x','y']
% time pdtest = ("X co-ordinate is " + df.x.map(str) + " and y is " + df.y.map(str)).tolist()
print(test[:5])

我也尝试过映射,但这也比循环 np 慢:

def mappy(pt_x,pt_y):
return("X co-ordinate is %g and y is %g" % (pt_x, pt_y))
%time mtest1 = list(map(lambda x: mappy(x[0],x[1]),test_list))
print(mtest1[:5])

时间:

enter image description here

最佳答案

这是使用 numpy.core.defchararray.add 的解决方案,首先将类型设置为 str

from numpy.core.defchararray import add    
test_list = np.tile(np.array([[1,2],[3,4],[5,6]]),(100000,1)).astype(str)

def stringy_arr(points):
return add(add('X coordinate is ', points[:,0]),add(' and y coordinate is ', points[:,1]))

稍微更快的计时:

%timeit stringy_arr(test_list)
1 loops, best of 3: 216 ms per loop

array(['X coordinate is 1 and y coordinate is 2',
'X coordinate is 3 and y coordinate is 4',
'X coordinate is 5 and y coordinate is 6', ...,
'X coordinate is 1 and y coordinate is 2',
'X coordinate is 3 and y coordinate is 4',
'X coordinate is 5 and y coordinate is 6'],
dtype='|S85')

# Previously tried functions
%time dumbstring(test_list)
1 loops, best of 3: 340 ms per loop

%timeit tdumbstring2 = dumbstring2(test_list)
1 loops, best of 3: 320 ms per loop

%time mtest1 = list(map(lambda x: mappy(x[0],x[1]),test_list))
1 loops, best of 3: 340 ms per loop

编辑

您也可以使用纯 python 进行理解,比我第一个提出的解决方案快得多:

test_list = np.tile(np.array([[1,2],[3,4],[5,6]]),(10000000,1)).astype(str)  #10M
test_list = test_list.tolist()

def comp(points):
return ['X coordinate is %s Y coordinate is %s' % (x,y) for x,y in points]

%timeit comp(test_list)
1 loops, best of 3: 6.53 s per loop

['X coordinate is 1 Y coordinate is 2',
'X coordinate is 3 Y coordinate is 4',
'X coordinate is 5 Y coordinate is 6',
'X coordinate is 1 Y coordinate is 2',
'X coordinate is 3 Y coordinate is 4',
'X coordinate is 5 Y coordinate is 6',...

%timeit dumbstring(test_list)
1 loops, best of 3: 30.7 s per loop

关于python - 从 numpy 数组创建字符串列表(非循环解决方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35622837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com