gpt4 book ai didi

python - 从 CSV 文件读取时对数组的数值运算

转载 作者:太空宇宙 更新时间:2023-11-04 05:34:11 25 4
gpt4 key购买 nike

我正在尝试对几个数组执行一些数值运算,同时从 CSV 文件中读取一些值。

我有固定接收器的坐标,我从跟踪太阳的 CSV 文件中读取定日镜的坐标。

接收器的坐标:

# co-ordinates of Receiver
XT = 0 # X co-ordinate of Receiver
YT = 0 # Y co-ordinate of Receiver
ZT = 207.724 # Z co-ordinate of Receiver, this is the height of tower
A = np.array(([XT],[YT],[ZT]))
print(A," are the co-ordinates of the target i.e. the receiver")

十个定日镜坐标:我从包含以下数据的 CSV 文件中读取的数据:

#X,Y,Z
#-1269.56,-1359.2,5.7
#1521.28,-68.0507,5.7
#-13.6163,1220.79,5.7
#-1388.76,547.708,5.7
#1551.75,-82.2342,5.7
#405.92,-1853.83,5.7
#1473.43,-881.703,5.7
#1291.73,478.988,5.7
#539.027,1095.43,5.7
#-1648.13,-73.7251,5.7

我读取CSV的坐标如下:

import csv
# Reading data from csv file
with open('Heliostat Field Layout Large heliostat.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
X = []
Y = []
Z = []
for row in readCSV:
X_coordinates = row[0]
Y_coordinates = row[1]
Z_coordinates = row[2]
X.append(X_coordinates)
Y.append(Y_coordinates)
Z.append(Z_coordinates)
Xcoordinate = [float(X[c]) for c in range(1,len(X))]
Ycoordinate=[float(Y[c]) for c in range(1,len(Y))]
Zcoordinate=[float(Z[c]) for c in range(1,len(Z))]

现在,当我尝试打印十个定日镜的坐标时,我得到了三个大数组,所有 X 坐标、Y 坐标和 Z 坐标都分组到一个而不是十个不同的输出中。

 [[[-1269.56    1521.28     -13.6163 -1388.76    1551.75     405.92    1473.43
1291.73 539.027 -1648.13 ]]

[[-1359.2 -68.0507 1220.79 547.708 -82.2342 -1853.83
-881.703 478.988 1095.43 -73.7251]]

[[ 5.7 5.7 5.7 5.7 5.7 5.7 5.7
5.7 5.7 5.7 ]]] are the co-ordinates of the heliostats

我用过:

B = np.array(([Xcoordinate],[Ycoordinate],[Zcoordinate]))
print(B," are the co-ordinates of the heliostats")

错在哪里?

此外,我想要一个我喜欢 B - A 的数组我使用的是:

#T1 = matrix(A)- matrix(B)
#print(T1," is the target vector for heliostat 1, T1")

我应该如何对数组 A 和 B 进行数值运算?我在这里尝试了矩阵运算。错了吗?

最佳答案

您的代码是正确的

下面的输出是numpy数组的显示方式。

[[-1359.2 -68.0507 1220.79 547.708 -82.2342 -1853.83 -881.703 478.988 1095.43 -73.7251]]

尽管值被粘在一起的错觉,但它们在数组中是完全不同的。您可以使用

访问单个值
print(B[1, 0, 0])    # print Y[0]

您要执行的数组 A 和 B 的减法将起作用

T1 = np.matrix(A)- np.matrix(B)
print(T1," is the target vector for heliostat 1, T1")

我可以提两个建议吗?

  • 您可以使用 numpy 的函数 loadtxt 读取文本文件中作为矩阵编写的 numpy 数组(这里就是这种情况):

    your_file = 'Heliostat Field Layout Large heliostat.csv'
    B = np.loadtxt(your_file, delimiter=',', skiprows=1)

    结果将是一个 (3, 10) numpy 数组。

  • 您可以直接对 numpy 数组执行广播操作(因此您不需要将其转换为矩阵)。您只需要注意尺寸即可。

    在您的原始脚本中,您只需编写:

    T1 = A - B

    如果按照建议用 loadtxt 得到数组 B,你会得到一个 (10, 3) 数组,而 A 是一个 (3, 1) 数组。数组 B 必须首先在 (3, 10) 数组中重新整形:

    B = B.reshape((3, 10))
    T1 = A - B

编辑:计算 T1 的每个 3D 向量的范数

norm_T1 = np.sqrt( np.sum( np.array(T1)**2, axis=0 ) )

请注意,在您的代码中,T1 是矩阵,因此 T1**2 是矩阵乘积。为了计算 T1 的每个向量 v 的 sqrt( v[0]**2 + v[1]**2 + v[2]**2 ),我首先将其转换为 numpy 数组。

关于python - 从 CSV 文件读取时对数组的数值运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113740/

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