简体   繁体   English

从 CSV 文件读取时对数组进行数值运算

[英]Numerical operations on arrays while reading from CSV file

I am trying to do a few numerical operations on a few arrays while reading some values from CSV files.我试图在从 CSV 文件中读取一些值的同时对几个数组进行一些数值运算。

I have the coordinates of a receiver which is fixed and I read coordinates of the heliostats from a CSV file which track the Sun.我有一个固定的接收器坐标,我从跟踪太阳的 CSV 文件中读取定日镜的坐标。

The coordinates of the receiver:接收器坐标:

# 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")

The coordinates of the ten heliostats: This data I read from a CSV file with the follwoing data:十个定日镜的坐标:我从带有以下数据的 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

I read the coordinates of the CSV as follows:我读取 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))]

Now, when I try to print the co-ordinates of the ten heliostats, I get three big arrays with all Xcoordinate, Ycoordinate and Zcoordinate grouped into one instead of ten different outputs.现在,当我尝试打印十个定日镜的坐标时,我得到了三个大阵列,其中所有 Xcoordinate、Ycoordinate 和 Zcoordinate 被分组为一个而不是十个不同的输出。

 [[[-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

I used:我用了:

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

What is the mistake?什么是错误?

Further, I would like to have an array where I wuold like B - A for which I use:此外,我想有一个我喜欢 B - A 的数组,我使用它:

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

How should i do a numerical operation on Arrays A and B?我应该如何对数组 A 和 B 进行数值运算? I tried a matrix operation here.我在这里尝试了矩阵运算。 Is that wrong?错了吗?

Your code is correct你的代码是正确的

The following output is the way numpy arrays are displayed.以下输出是 numpy 数组的显示方式。

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

Despite the illusion that the values are stuck together, they are perfectly distinct in the array.尽管存在这些值粘在一起的错觉,但它们在数组中是完全不同的。 You can access to a single value with您可以访问单个值

print(B[1, 0, 0])    # print Y[0]

The substraction of arrays A and B you want to perform will work您要执行的数组 A 和 B 的减法将起作用

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

May I make two suggestions ?我可以提两个建议吗?

  • You can read a numpy array written as a matrix in a text file (it's the case here) with the function loadtxt of numpy :您可以使用 numpy 的函数 loadtxt 读取在文本文件中写为矩阵的 numpy 数组(这里就是这种情况):

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

    The result will be a (3, 10) numpy array.结果将是一个 (3, 10) numpy 数组。

  • You can perform broadcasing operations directly on numpy arrays (so you don't need to convert it in matrix).您可以直接对 numpy 数组执行广播操作(因此您无需将其转换为矩阵)。 You just need to be careful with the dimensions.您只需要注意尺寸。

    In your original script you just need to write :在您的原始脚本中,您只需要编写:

     T1 = A - B

    If you get array B with loadtxt as suggested, you will get a (10, 3) array, while A is a (3, 1) array.如果按照建议使用 loadtxt 获取数组 B,您将得到一个 (10, 3) 数组,而 A 是一个 (3, 1) 数组。 The array B must first be reshaped in a (3, 10) array :数组 B 必须首先在 (3, 10) 数组中重新整形:

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

EDIT : compute the norm of each 3D vector of T1编辑:计算 T1 的每个 3D 向量的范数

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

Note that in your code T1 is a matrix, so T1**2 is a matrix product.请注意,在您的代码中 T1 是一个矩阵,所以 T1**2 是一个矩阵乘积。 In order to compute sqrt( v[0]**2 + v[1]**2 + v[2]**2 ) for each vector v of T1, I first convert it to a numpy array.为了计算 T1 的每个向量 v 的 sqrt( v[0]**2 + v[1]**2 + v[2]**2 ),我首先将其转换为一个 numpy 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM