|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
|
|