鱼C论坛

 找回密码
 立即注册
查看: 814|回复: 3

list index out of range

[复制链接]
发表于 2023-10-13 21:49:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发生异常: IndexError
list index out of range
  File "D:\0000可见光2\程序\InceptionTime\Test3.py", line 38, in __init__
    self.dataset_name = output_directory.split('/')[-2]
  File "D:\0000可见光2\程序\InceptionTime\Test3.py", line 133, in <module>
    model = Classifier_NNE(output_directory='output_directory', input_shape=X_train.shape[1:], nb_classes=8)
IndexError: list index out of range
import keras
import numpy as np
from utils import calculate_metrics
from utils import create_directory
from utils import check_if_file_exits
import gc
from constants import UNIVARIATE_ARCHIVE_NAMES  as ARCHIVE_NAMES
import time


class Classifier_NNE:

    def create_classifier(self, model_name, input_shape, nb_classes, output_directory, verbose=True,
                          build=True):
        if self.check_if_match('inception*', model_name):
            import inception
            return inception.Classifier_INCEPTION(output_directory, input_shape, nb_classes, verbose,
                                                  build=build)

    def check_if_match(self, rex, name2):
        import re
        pattern = re.compile(rex)
        return pattern.match(name2)

    def __init__(self, output_directory, input_shape, nb_classes, verbose=False, nb_iterations=5,
                 clf_name='inception'):
        self.classifiers = [clf_name]
        out_add = ''
        for cc in self.classifiers:
            out_add = out_add + cc + '-'
        self.archive_name = ARCHIVE_NAMES[0]
        self.iterations_to_take = [i for i in range(nb_iterations)]
        for cc in self.iterations_to_take:
            out_add = out_add + str(cc) + '-'
        self.output_directory = output_directory.replace('nne',
                                                         'nne' + '/' + out_add)
        create_directory(self.output_directory)
        self.dataset_name = output_directory.split('/')[-2]
        self.verbose = verbose
        self.models_dir = output_directory.replace('nne', 'classifier')

    def fit(self, x_train, y_train, x_test, y_test, y_true):
        # no training since models are pre-trained
        start_time = time.time()

        y_pred = np.zeros(shape=y_test.shape)

        ll = 0

        # loop through all classifiers
        for model_name in self.classifiers:
            # loop through different initialization of classifiers
            for itr in self.iterations_to_take:
                if itr == 0:
                    itr_str = ''
                else:
                    itr_str = '_itr_' + str(itr)

                curr_archive_name = self.archive_name + itr_str

                curr_dir = self.models_dir.replace('classifier', model_name).replace(
                    self.archive_name, curr_archive_name)

                model = self.create_classifier(model_name, None, None,
                                               curr_dir, build=False)

                predictions_file_name = curr_dir + 'y_pred.npy'
                # check if predictions already made
                if check_if_file_exits(predictions_file_name):
                    # then load only the predictions from the file
                    curr_y_pred = np.load(predictions_file_name)
                else:
                    # then compute the predictions
                    curr_y_pred = model.predict(x_test, y_true, x_train, y_train, y_test,
                                                return_df_metrics=False)
                    keras.backend.clear_session()

                    np.save(predictions_file_name, curr_y_pred)

                y_pred = y_pred + curr_y_pred

                ll += 1

        # average predictions
        y_pred = y_pred / ll

        # save predictions
        np.save(self.output_directory + 'y_pred.npy', y_pred)

        # convert the predicted from binary to integer
        y_pred = np.argmax(y_pred, axis=1)

        duration = time.time() - start_time

        df_metrics = calculate_metrics(y_true, y_pred, duration)

        df_metrics.to_csv(self.output_directory + 'df_metrics.csv', index=False)

        gc.collect()

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import sklearn

df1 = pd.read_csv("train.csv")
df1 = np.array(df1)
X = np.expand_dims(df1[:, 1:891].astype(float), axis=2)  # 对数据进行增维并转化为32为
#X = np.expand_dims(df1[:, 1:891].astype(float), axis=1)
Y = df1[:, 0]
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.2, random_state=42)

#y_train, y_val = transform_labels(y_train, y_val)
# save orignal y because later we will use binary
y_true = y_val.astype(np.int64)
y_true_train = y_train.astype(np.int64)
# transform the labels from integers to one hot vectors
enc = sklearn.preprocessing.OneHotEncoder()
enc.fit(np.concatenate((y_train, y_val), axis=0).reshape(-1, 1))
y_train = enc.transform(y_train.reshape(-1, 1)).toarray()
y_val = enc.transform(y_val.reshape(-1, 1)).toarray()

'''y_train = to_categorical(y_train)  # one-hot encoding
y_val= to_categorical(y_val)
y_true = y_val'''
df2 = pd.read_csv("test.csv")
df2 = np.array(df1)
X_test = np.expand_dims(df2[:, 1:891].astype(float), axis=2) 
y_test=df2[:, 0]
y_true1 = y_test.astype(np.int64)
y_test = enc.transform(y_test.reshape(-1, 1)).toarray()
# 创建模型
model = Classifier_NNE(output_directory='output_directory', input_shape=X_train.shape[1:], nb_classes=8)

# 训练模型
model.fit(X_train, y_train, X_val, y_val,y_true)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-13 21:50:08 | 显示全部楼层
The error you're encountering, IndexError: list index out of range, is due to trying to access an index in a list that doesn't exist. In your case, the error is occurring at this line:
self.dataset_name = output_directory.split('/')[-2]

Here, you're trying to split the output_directory string by the '/' character and access the second last element. If the output_directory string doesn't contain enough '/' characters, you'll get an IndexError.

To fix this, you need to ensure that output_directory contains at least two '/' characters. If you're not sure whether output_directory will always contain enough '/' characters, you can add a check before this line to prevent the error:
split_directory = output_directory.split('/')
if len(split_directory) >= 2:
    self.dataset_name = split_directory[-2]
else:
    # Handle the case where output_directory doesn't contain enough '/' characters
    # For example, you could set self.dataset_name to a default value
    self.dataset_name = "default"

This code will prevent the IndexError by checking that split_directory contains enough elements before trying to access the second last element. If split_directory doesn't contain enough elements, self.dataset_name is set to a default value. You should replace \"default\" with a value that makes sense in your context.
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-13 21:51:09 | 显示全部楼层
这个错误通常表示您正在尝试访问列表中不存在的索引。在这种情况下,错误发生在第133行,因为您的output_directory参数是一个字符串,而不是列表。您需要检查代码中是否有任何错误,确保您正在使用正确的参数类型。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-13 21:51:35 | 显示全部楼层
陶远航 发表于 2023-10-13 21:50
The error you're encountering, IndexError: list index out of range, is due to trying to access an in ...

谢谢哥 都没翻译 热乎的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-24 04:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表