我在这里的第一个问题,所以请告诉我。
我正在尝试用从 excel 文件中读取的训练数据来为我的神经网络提供数据。当我在工作表中的行数少于 50 时,它工作得非常好。但是当我尝试使用包含近 4.000 行的真正 excel 文件时,它突然需要永远。虽然 4.000 很多,但我很确定我的做法仍然非常低效。
正如您在下面的代码中看到的那样,我在循环中一遍又一遍地使用 read_excel。我觉得应该有一种方法可以只阅读整个专栏 1 次,然后从那里开始使用它。
我的目标是从第 0 行开始读取 5 行作为第一个输入。然后再次读取 5 行,但从第 1 行开始,再从第 3 行开始读取 5 行所以它就像一个 5 行的窗口,被读取然后移动窗口加 1。输出应始终是窗口后的 1 行。
**Example:** if row 1-20 contains numbers 1-20 then:
input1 = [1,2,3,4,5] and output1 = 6
input2 = [2,3,4,5,6] and output2 = 7
...
input15 = [15,16,17,18,19] and output15 = 20
注意输入是列表,输出只是数字。因此,当我将它们附加到最终的输入和输出列表时,我最终得到的输入是列表列表,而输出是输出列表
我的代码
from pandas import read_excel
# initialize final input & output lists. The contents of the temporary input & output lists
# are gonna be appended to these final lists
training_input = []
training_output = []
# excel relevant info
my_sheet = 'Junaid'
file_name = '../Documents/Junaid1.xlsx'
# initialize counters
loop_count = 0
row_counter = 0
for x in range(25):
# load the excel file containing inputs & outputs
# using parameters skiprows, nrows (number of rows) and index col
df = read_excel(file_name, sheet_name = my_sheet, skiprows=row_counter, nrows=6, index_col=0)
# initialize temporary input & output lists
input_temp = []
output_temp = []
for y in df.index:
# append the first 5 rows of the 6 to input list
if loop_count < 5:
input_temp.append(df.index[loop_count])
loop_count += 1
else:
# append the 6th data to output list
training_output.append(df.index[loop_count])
training_input.append(input_temp)
row_counter += 1
loop_count = 0