我正在使用一个函数,输入底层 numpy 数组的开始和结束索引来对数据子集执行计算。当输入开始和停止的向量时,我不断收到此错误:
“类型错误:只有整数标量数组可以转换为标量索引”。
def calculate_subset_results(start_dates, stop_dates, L):
# Create index arrays for each combination of start and stop dates
indices = np.arange(L.shape[0])
start_indices = indices[start_dates[:, None]]
stop_indices = indices[stop_dates[:, None]]
# Slice the array L using numpy broadcasting
subsets = L[start_indices:stop_indices] #HERE OCCURS THE ERROR
# Perform calculations on the subsets
results = np.sum(subsets, axis=1) # Sum along the rows
return results
# Example usage
L = np.random.rand(100000) # Example 1D numpy array
start_dates = np.array([0, 10000, 20000]) # Example array of start dates
stop_dates = np.array([5000, 15000, 30000]) # Example array of stop dates
subset_results = calculate_subset_results(start_dates, stop_dates, L)
print(L[20000])
我需要改变输入向量的方式吗?有什么技巧吗?我想禁止循环,因为我需要执行几次并且想坚持使用向量运算。
我想向函数输入一个向量并接收一个向量作为输出。
在这种情况下,start_indices 和 stop_indices 必须是整数而不是标量,这里有一个示例来解释更多错误。
>>> L = np.random.rand(100000)
>>> L
array([0.88811571, 0.25921032, 0.36896352, ..., 0.15395515, 0.4545259 ,
0.47172845])
>>> L[2000]
0.38937897690807544
>>> indices = np.arange(L.shape[0])
>>> L[indices]
array([0.88811571, 0.25921032, 0.36896352, ..., 0.15395515, 0.4545259 ,
0.47172845])
>>> L[indices:indices]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: only integer scalar arrays can be converted to a scalar index
>>> L[indices[:5000]]
array([0.88811571, 0.25921032, 0.36896352, ..., 0.71316957, 0.81522261,
0.63835866])