Python NameError:未定义名称“jam”
我按照一个教程,但我得到了一个错误Python NameError:未定义名称“jam”,python,python-3.x,artificial-intelligence,Python,Python 3.x,Artificial Intelligence,我按照一个教程,但我得到了一个错误 # Initialize variables cnt_row = -1 cnt_col = 0 cnt_zero = 0 # Grab all relevant MIDI data (available in MIDI_dat) for i in range(0, len(jam['annotations'])): if jam['annotations'][int(i)]['namespace'] == 'note_midi': f
# Initialize variables
cnt_row = -1
cnt_col = 0
cnt_zero = 0
# Grab all relevant MIDI data (available in MIDI_dat)
for i in range(0, len(jam['annotations'])):
if jam['annotations'][int(i)]['namespace'] == 'note_midi':
for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
cnt_row = cnt_row + 1
for k in range(0, len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1):
if cnt_zero == 0:
MIDI_arr = np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)
cnt_zero = cnt_zero + 1
if cnt_zero > 0:
MIDI_arr = np.vstack((MIDI_arr, np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)))
cnt_zero = cnt_zero + 1 # Keep
if cnt_col > 2:
cnt_col = 0
MIDI_arr[cnt_row, cnt_col] = sorted(jam['annotations'][int(i)]['data'])[int(j)][int(k)]
cnt_col = cnt_col + 1
MIDI_dat = np.zeros((cnt_row + 1, cnt_col), dtype = np.float32)
cnt_col2 = 0
for n in range(0, cnt_row + 1):
for m in range(0, cnt_col):
if cnt_col2 > 2:
cnt_col2 = 0
MIDI_dat[n, cnt_col2] = MIDI_arr[n, cnt_col2]
cnt_col2 = cnt_col2 + 1
# Return the unique MIDI notes played (available in MIDI_val)
MIDI_dat_dur = np.copy(MIDI_dat)
for r in range(0, len(MIDI_dat[:, 0])):
MIDI_dat_dur[r, 0] = MIDI_dat[r, 0] + MIDI_dat[r, 1]
tab_1, = np.where(np.logical_and(MIDI_dat[:, 0] >= start, MIDI_dat[:, 0] <= stop))
tab_2, = np.where(np.logical_and(MIDI_dat_dur[:, 0] >= start, MIDI_dat_dur[:, 0] <= stop))
tab_3, = np.where(np.logical_and(np.logical_and(MIDI_dat[:, 0] < start, MIDI_dat_dur[:, 0] > stop), MIDI_dat[:, 1] > int(stop-start)))
if tab_1.size != 0 and tab_2.size == 0 and tab_3.size == 0:
tab_ind = tab_1
if tab_1.size == 0 and tab_2.size != 0 and tab_3.size == 0:
tab_ind = tab_2
if tab_1.size == 0 and tab_2.size == 0 and tab_3.size != 0:
tab_ind = tab_3
if tab_1.size != 0 and tab_2.size != 0 and tab_3.size == 0:
tab_ind = np.concatenate([tab_1, tab_2])
if tab_1.size != 0 and tab_2.size == 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_1, tab_3])
if tab_1.size == 0 and tab_2.size != 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_2, tab_3])
if tab_1.size != 0 and tab_2.size != 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_1, tab_2, tab_3])
if tab_1.size == 0 and tab_2.size == 0 and tab_3.size == 0:
tab_ind = []
if len(tab_ind) != 0:
MIDI_val = np.zeros((len(tab_ind), 1), dtype = np.float32)
for z in range(0, len(tab_ind)):
MIDI_val[z, 0] = int(round(MIDI_dat[tab_ind[z], 2]))
elif len(tab_ind) == 0:
MIDI_val = []
MIDI_val = np.unique(MIDI_val)
if MIDI_val.size >= 6:
MIDI_val = np.delete(MIDI_val, np.s_[6::])
现在我对编码有点陌生,所以我不太确定jam指的是什么,但因为我在学习教程,我真的不明白为什么它没有定义?提前感谢您为初学者提供的帮助。您需要声明一个变量jam并从任何地方为其赋值,python将不允许以这种方式声明
cnt_row = -1
cnt_col = 0
cnt_zero = 0
jam = MIDI_dat <------- (or the variable from which you are accessing the data from)
# Grab all relevant MIDI data (available in MIDI_dat)
for i in range(0, len(jam['annotations'])):
if jam['annotations'][int(i)]['namespace'] == 'note_midi':
for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
cnt_row = cnt_row + 1
for k in range(0, len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1):
if cnt_zero == 0:
MIDI_arr = np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)
cnt_zero = cnt_zero + 1
if cnt_zero > 0:
MIDI_arr = np.vstack((MIDI_arr, np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)))
cnt_zero = cnt_zero + 1 # Keep
if cnt_col > 2:
cnt_col = 0
MIDI_arr[cnt_row, cnt_col] = sorted(jam['annotations'][int(i)]['data'])[int(j)][int(k)]
cnt_col = cnt_col + 1
果酱还没有定义,没什么好说的。如果这正是教程代码,那么它就有一个bug。@tdelaney是正确的,看起来“jam”是指某种数据,但我认为教程有一个bug,或者只是给了一个变量一个错误的名称。您是在IDE中还是在控制台上运行?错误发生后,您可以查看诸如CQT之类的变量,以查看它们是否具有“annotations”元素。从命令行来看,它的python-i myscript.py.Well很不幸。如果你没有时间或者不想,我理解,但是如果这能解决任何问题,教程就在这里。谢谢你的评论:编辑:我使用jupyter实验室来运行代码。
cnt_row = -1
cnt_col = 0
cnt_zero = 0
jam = MIDI_dat <------- (or the variable from which you are accessing the data from)
# Grab all relevant MIDI data (available in MIDI_dat)
for i in range(0, len(jam['annotations'])):
if jam['annotations'][int(i)]['namespace'] == 'note_midi':
for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
cnt_row = cnt_row + 1
for k in range(0, len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1):
if cnt_zero == 0:
MIDI_arr = np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)
cnt_zero = cnt_zero + 1
if cnt_zero > 0:
MIDI_arr = np.vstack((MIDI_arr, np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)))
cnt_zero = cnt_zero + 1 # Keep
if cnt_col > 2:
cnt_col = 0
MIDI_arr[cnt_row, cnt_col] = sorted(jam['annotations'][int(i)]['data'])[int(j)][int(k)]
cnt_col = cnt_col + 1