我尝试过以下方法:
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.read_csv("training.csv")
>>> data_raw = df.values
>>> data = []
>>> seq_len = 5
>>> for index in range(len(data_raw) - seq_len):
... data.append(data_raw[index: index + seq_len])
...
>>> len(data)
1994
>>> len(data_raw)
1999
>>> del data[0]
数据可在此处获得:training.csv
我已经看到del删除了数组中的第一个元素.并重新排列值,如第一个位置,现在是第0个位置,依此类推.
我想删除索引处的值:0,4,5,9,10,14,依此类推.
但是这对于当前的del语句来说是不可能的,因为它会重新排列值.
请帮我找到缺失的部分.
解决方法:
首先,可以生成所需的删除索引:0,4,5,9,10,14,15,19,20,24,25,29 ….
indices = []
for i in range(1,401):
indices.append(5*(i-1))
indices.append(5*i-1)
del indices[-1] # This is to remove 1999, which is out of index for df
print(indices[:12])
[0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 25, 29]
然后使用np.delete:
data_raw = np.random.randint(0, 10, size=(1999, 10))
new_data = np.delete(data_raw, indices, axis=0) # Since this is not inplace op
验证:
np.array_equal(new_data[:6],data_raw[[1,2,3,6,7,8]])
# Where 0,4,5,9 is removed
# True
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。