“np.searchsorted” 在 netCDF4 数组上未正常工作。

huangapple go评论51阅读模式
英文:

np.searchsorted not working properly on netCDF4 array

问题

np.searchsorted 无法找到正确的索引,但我认为这与数据类型有关。

在我看来,这个函数似乎无法区分浮点数和字符串,但我不能将数组转换为浮点数。

coord = Dataset(r"C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc", 'r')
lat = coord.variables['latitude']
lon = coord.variables['longitude']
lat = np.array(lat, dtype=float)
lon = np.array(lon, dtype=float)

这给我返回以下错误:

Traceback (most recent call last):
  File "c:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\extract.py", line 41, in <module>
    lat = np.array(lat, dtype=float)
TypeError: Variable.__array__() takes no arguments (1 given)

当我尝试将要搜索的元素转换为浮点数时:

coord = Dataset(r"C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc", 'r')
lat = coord.variables['latitude']
lon = coord.variables['longitude']
x0 = np.searchsorted(lat, float(x))
y0 = np.searchsorted(lon, float(y))

这不会返回错误,但会给我最后可能的索引,这是不正确的。我注意到对于负数元素,这种方法完全正常。

英文:

np.searchsorted is not finding the right indexes but i think it is related to the type of data.

To me it looks like this function can't tell floats and strs apart but I can't convert the array to float.

 coord=Dataset(r&quot;C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc&quot;,&#39;r&#39;)
 lat=coord.variables\[&#39;latitude&#39;\]
 lon=coord.variables\[&#39;longitude&#39;\]
 lat=np.array(lat,dtype=float)
 lon=np.array(lon,dtype=float)

this gives me back the error:

Traceback (most recent call last):

File &quot;c:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\extract.py&quot;, line 41, in \&lt;module\&gt;

lat=np.array(lat,dtype=float)

^^^^^^^^^^^^^^^^^^^^^^^^^

TypeError: Variable.__array__() takes no arguments (1 given)

and when I try to make element I'm searching for into a float:

 coord=Dataset(r&quot;C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc&quot;,&#39;r&#39;)
 lat=coord.variables\[&#39;latitude&#39;\]
 lon=coord.variables\[&#39;longitude&#39;\]
 x0=np.searchsorted(lat,float(x))
 y0=np.searchsorted(lon,float(y))

this doesn't give me an error back but gives me the last possible index which is incorrect what I've noticed is that for negative elements this last method works just fine.

答案1

得分: 0

如果出现TypeError: Variable.array()接受的参数数量为1 (给定了1个),可能是因为您没有在变量对象后面加上[:]。

关于您代码片段的第二部分,看起来您正在尝试使用np.searchsorted函数来查找latlon数组中距离给定值xy最近的索引。然而,np.searchsorted函数要求输入数组按升序排序,所以在调用np.searchsorted之前,您应确保latlon已经排序。或者,您可以使用np.argmin函数来找到与xy最接近的元素在latlon中的索引:

coord = Dataset(r"C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc", 'r')
lat = coord.variables['latitude'][:]
lon = coord.variables['longitude'][:]
x0 = np.argmin(np.abs(lat - float(x)))
y0 = np.argmin(np.abs(lon - float(y)))

请注意,我在latlon的末尾添加了[:]来提取它们的值作为NumPy数组,而不是netCDF4变量。

英文:

If you get the error TypeError: Variable.array() takes no arguments (1 given),it is probably because you did not put [:] after the variable object.

Regarding the second part of your code snippet, it looks like you're trying to use the np.searchsorted function to find the index of a value in the lat and lon arrays that's closest to a given value x and y, respectively. However, the np.searchsorted function expects the input array to be sorted in ascending order, so you should make sure that lat and lon are sorted before calling np.searchsorted. Alternatively, you could use the np.argmin function to find the index of the element in lat and lon that's closest to x and y, respectively:

coord = Dataset(r&quot;C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\coordenadas.nc&quot;, &#39;r&#39;)
lat = coord.variables[&#39;latitude&#39;][:]
lon = coord.variables[&#39;longitude&#39;][:]
x0 = np.argmin(np.abs(lat - float(x)))
y0 = np.argmin(np.abs(lon - float(y)))

Note that I added [:] to the end of lat and lon to extract their values as NumPy arrays, instead of as netCDF4 variables.

huangapple
  • 本文由 发表于 2023年6月5日 00:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定