如何修改Pychem中的代码以规避numpy多维索引相关的FutureWarning
Fixing the FutureWarning for Multidimensional Indexing in NumPy
The warning you're hitting comes from NumPy phasing out support for non-tuple sequences when doing multidimensional indexing. In your code, np.meshgrid(...) returns a list of arrays, and passing that directly to self.commission_matrix[...] triggers the deprecation alert.
The Simple Fix
All you need to do is wrap the result of np.meshgrid in tuple() to convert the sequence into the format NumPy now recommends for indexing:
for exc_name in self.exchanges.keys(): indexes = [index for cur_name, index in self.currency2index.items() if exc_name in cur_name] # Convert meshgrid's list output to a tuple grid_indices = tuple(np.meshgrid(indexes, indexes, indexing='ij', sparse=True)) self.commission_matrix[grid_indices] = self.trading_fee[exc_name]
Why This Works
- When using
sparse=True,np.meshgridreturns two 2D sparse arrays (one for row indices, one for column indices) packaged in a list. - NumPy’s updated indexing rules require multidimensional indices to be passed as a tuple of arrays instead of a list. Wrapping the meshgrid result in
tuple()adjusts the format to match this requirement, wiping out the warning without changing your code’s actual behavior.
This tweak keeps your code future-proof while maintaining exactly the same output you’re getting now.
内容的提问来源于stack exchange,提问作者Bsarv
相关产品推荐
相关产品推荐

