对 Python 中的 x,y 网格执行计算
Performing a calculation over an x,y grid in Python
我想对 X、Y 数据执行计算以生成计算出的 Z。我的代码如下:
injection_wells.csv
的示例数据集
Name
X
Y
Q
MW-1
2517700
996400
5
MW-2
2517770
996420
5
import pandas as pd
import math
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.tri as tri
IW = pd.read_csv (r'Injection_wells.csv')
`Note that - Injection wells is a table of three wells with names, X, Y, and Q (flow rate).`
#pull all the relevant well information by well into their own arrays
MW1 = IW[IW['Name'] == 'MW1']
MW2 = IW[IW['Name'] == 'MW2']
MW3 = IW[IW['Name'] == 'MW3']
#initiate grid
xi = np.linspace(2517675,2517800,625)
yi = np.linspace(996300,996375,375)
#make it so i can apply np.float to an array
vector = np.vectorize(np.float)
X,Y = np.meshgrid(xi,yi)
#perform calculation over every X and Y.
PSI = ((MW1['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:]))-np.float(MW1['X']))/(vector(Y[:,None])-np.float(MW1['Y']))))+
(MW2['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:])-np.float(MW2['X']))/vector((Y[:,None])-np.float(MW2['Y'])))))+
(MW3['Q']/(2*math.pi))*(np.arctan(((vector((X[None,:])-np.float(MW3['X']))/vector((Y[:,None])-np.float(MW3['Y'])))))))
我收到错误:
ValueError Traceback (most recent call last)
<ipython-input-11-fd6ee058014f> in <module>
17 X,Y = np.meshgrid(xi,yi)
18
---> 19 PSI = ((MW1['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:]))-np.float(MW1['X']))/(vector(Y[:,None])-np.float(MW1['Y']))))+
20 (MW2['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:])-np.float(MW2['X']))/vector((Y[:,None])-np.float(MW2['Y'])))))+
21 (MW3['Q']/(2*math.pi))*(np.arctan(((vector((X[None,:])-np.float(MW3['X']))/vector((Y[:,None])-np.float(MW3['Y'])))))))
~\Anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
63 other = item_from_zerodim(other)
64
---> 65 return method(self, other)
66
67 return new_method
~\Anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right)
343 result = arithmetic_op(lvalues, rvalues, op)
344
--> 345 return left._construct_result(result, name=res_name)
346
347 wrapper.__name__ = op_name
~\Anaconda3\lib\site-packages\pandas\core\series.py in _construct_result(self, result, name)
2755 # We do not pass dtype to ensure that the Series constructor
2756 # does inference in the case where `result` has object-dtype.
-> 2757 out = self._constructor(result, index=self.index)
2758 out = out.__finalize__(self)
2759
~\Anaconda3\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
311 try:
312 if len(index) != len(data):
--> 313 raise ValueError(
314 f"Length of passed values is {len(data)}, "
315 f"index implies {len(index)}."
ValueError: Length of passed values is 375, index implies 1.
我知道这与我试图将函数应用于只接受一个值的数组有关。我正在努力克服这个问题,并能够按原样对整个网格化数据集执行此计算。如有任何帮助,我们将不胜感激。
下面是我要计算的等式。请注意,等式中的 theta 是从网格节点到注入井(对于每个网格节点)的距离的反正切值,这就是我要在代码中复制的内容。
谢谢
我想在这里跳枪,因为我想我现在明白了这个问题,在多看了一点之后。
所以你有一个注入井数据的 DataFrame,有四列:
name x y q
str int int int
并且您有一个要计算的函数 f(x, y, q) -> z
。我不确定我是否完全按照你的函数在做什么,因为它的格式很难阅读,所以我将使用一个简化的例子:
def func(x, y, q):
return (q / 2 * np.pi) * np.arctan(y, x)
现在不用将井数据分成不同的数组,只需按行将函数应用于整个数据帧即可:
df["z"] = func(df.x, df.y, df.q)
我想对 X、Y 数据执行计算以生成计算出的 Z。我的代码如下:
injection_wells.csv
的示例数据集Name | X | Y | Q |
---|---|---|---|
MW-1 | 2517700 | 996400 | 5 |
MW-2 | 2517770 | 996420 | 5 |
import pandas as pd
import math
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.tri as tri
IW = pd.read_csv (r'Injection_wells.csv')
`Note that - Injection wells is a table of three wells with names, X, Y, and Q (flow rate).`
#pull all the relevant well information by well into their own arrays
MW1 = IW[IW['Name'] == 'MW1']
MW2 = IW[IW['Name'] == 'MW2']
MW3 = IW[IW['Name'] == 'MW3']
#initiate grid
xi = np.linspace(2517675,2517800,625)
yi = np.linspace(996300,996375,375)
#make it so i can apply np.float to an array
vector = np.vectorize(np.float)
X,Y = np.meshgrid(xi,yi)
#perform calculation over every X and Y.
PSI = ((MW1['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:]))-np.float(MW1['X']))/(vector(Y[:,None])-np.float(MW1['Y']))))+
(MW2['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:])-np.float(MW2['X']))/vector((Y[:,None])-np.float(MW2['Y'])))))+
(MW3['Q']/(2*math.pi))*(np.arctan(((vector((X[None,:])-np.float(MW3['X']))/vector((Y[:,None])-np.float(MW3['Y'])))))))
我收到错误:
ValueError Traceback (most recent call last)
<ipython-input-11-fd6ee058014f> in <module>
17 X,Y = np.meshgrid(xi,yi)
18
---> 19 PSI = ((MW1['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:]))-np.float(MW1['X']))/(vector(Y[:,None])-np.float(MW1['Y']))))+
20 (MW2['Q']/(2*math.pi))*(np.arctan(((vector(X[None,:])-np.float(MW2['X']))/vector((Y[:,None])-np.float(MW2['Y'])))))+
21 (MW3['Q']/(2*math.pi))*(np.arctan(((vector((X[None,:])-np.float(MW3['X']))/vector((Y[:,None])-np.float(MW3['Y'])))))))
~\Anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
63 other = item_from_zerodim(other)
64
---> 65 return method(self, other)
66
67 return new_method
~\Anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right)
343 result = arithmetic_op(lvalues, rvalues, op)
344
--> 345 return left._construct_result(result, name=res_name)
346
347 wrapper.__name__ = op_name
~\Anaconda3\lib\site-packages\pandas\core\series.py in _construct_result(self, result, name)
2755 # We do not pass dtype to ensure that the Series constructor
2756 # does inference in the case where `result` has object-dtype.
-> 2757 out = self._constructor(result, index=self.index)
2758 out = out.__finalize__(self)
2759
~\Anaconda3\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
311 try:
312 if len(index) != len(data):
--> 313 raise ValueError(
314 f"Length of passed values is {len(data)}, "
315 f"index implies {len(index)}."
ValueError: Length of passed values is 375, index implies 1.
我知道这与我试图将函数应用于只接受一个值的数组有关。我正在努力克服这个问题,并能够按原样对整个网格化数据集执行此计算。如有任何帮助,我们将不胜感激。
下面是我要计算的等式。请注意,等式中的 theta 是从网格节点到注入井(对于每个网格节点)的距离的反正切值,这就是我要在代码中复制的内容。
谢谢
我想在这里跳枪,因为我想我现在明白了这个问题,在多看了一点之后。
所以你有一个注入井数据的 DataFrame,有四列:
name x y q
str int int int
并且您有一个要计算的函数 f(x, y, q) -> z
。我不确定我是否完全按照你的函数在做什么,因为它的格式很难阅读,所以我将使用一个简化的例子:
def func(x, y, q):
return (q / 2 * np.pi) * np.arctan(y, x)
现在不用将井数据分成不同的数组,只需按行将函数应用于整个数据帧即可:
df["z"] = func(df.x, df.y, df.q)