英文:
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"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)
this gives me back the error:
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)
and when I try to make element I'm searching for into a float:
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))
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
函数来查找lat
和lon
数组中距离给定值x
和y
最近的索引。然而,np.searchsorted
函数要求输入数组按升序排序,所以在调用np.searchsorted
之前,您应确保lat
和lon
已经排序。或者,您可以使用np.argmin
函数来找到与x
和y
最接近的元素在lat
和lon
中的索引:
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)))
请注意,我在lat
和lon
的末尾添加了[:]
来提取它们的值作为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"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)))
Note that I added [:]
to the end of lat
and lon
to extract their values as NumPy arrays, instead of as netCDF4 variables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论