gpt4 book ai didi

Python,在numpy数组中加载.csv,返回一个列表

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:51 24 4
gpt4 key购买 nike

我想改进此代码,以便在最后返回“解决方案”的列表:

[b坐标[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3] ]

代码:

import csv
import numpy as np
import scipy.spatial

points = np.array([(float(X), float(Y), float(Z))
for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
# load XYZ coordinates of 'points' in a np.array

tri = scipy.spatial.Delaunay(points)
# do the triangulation

indices = tri.simplices
# indices of vertices

vert = points[tri.simplices]
# the vertices for each tetrahedron

targets = np.array([(float(X), float(Y), float(Z))
for X, Y, Z in csv.reader(open('targets.csv'))])
# load the XYZ target values in a np.array

tetrahedra = tri.find_simplex(targets)
# find which tetrahedron each point belong to

X = tri.transform[tetrahedra,:3]
Y = targets - tri.transform[tetrahedra,3]
b = np.einsum('ijk,ik->ij', X, Y)
bcoords = np.c_[b, 1 - b.sum(axis=1)]
# find the barycentric coordinates of each point

print bcoords

_

代码在两个 np.array 中加载两个 .csv 文件,并使用模块 scipy.spatial.Delaunay 查找重心坐标四面体中的

XYZcolorlist.csv 是点 R、G、B、X、Y、Z 的云

targets.csv是一组目标X、Y、Z

XYZcolorlist.csv:

255,63,127,35.5344302104,21.380721966,20.3661095969
255,95,127,40.2074945517,26.5282949405,22.7094284437
255,127,127,43.6647438365,32.3482625492,23.6181801523
255,159,127,47.1225628354,39.1780944388,22.9366615044
255,223,159,61.7379149646,62.8387601708,32.3936200864
...

目标.csv:

49.72,5,8.64
50.06,5,8.64
50.4,5,8.64
50.74,5,8.64
51.08,5,8.64
51.42,5,8.64
51.76,5,8.64
...

对于targets.csv的每个点,我想得到:

  • 包含的 4 个顶点

  • 与每个顶点关联的 4 个 float(R)、float(G)、float(B):

  • 与每个关联的 4 个重心坐标

我想使用 numpy 快速做到这一点

代码给出了所有这些,除了 4 个 R、G、B

或者,我可以使用以下代码加载整个文件的数据:

points = np.array([(float(R), float(G), float(B), float(X), float(Y), float(Z))
for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
# load R,G,B,X,Y,Z coordinates of 'points' in a np.array

如何返回列表:

[b坐标[0, 1, 2, 3], R[0, 1, 2, 3], G[0, 1, 2, 3], B[0, 1, 2, 3] ]

是否可以构建一个dict[]

谢谢

最佳答案

我真的会使用 np.genfromtxt 来读取 csv 文件。这是一个例子:

import numpy as np
X, Y, Z = np.genfromtxt('targets.csv', delimiter=',', unpack=True)

这比 csv 容易得多,并且会立即返回 numpy.ndarray。

关于Python,在numpy数组中加载.csv,返回一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616537/

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