Msy20070905 2024-07-16 08:44 采纳率: 21.2%
浏览 17

ValueError: setting an array element with a sequence.

import tensorflow as tf
import numpy as np
import wfdb
import pandas as pd
import ast
from matplotlib import pyplot as plt
import keras.backend as K


def load_raw_data(df, sampling_rate, path):
    if sampling_rate == 100:
        data = [wfdb.rdsamp(path+f) for f in df.filename_lr]
    else:
        data = [wfdb.rdsamp(path+f) for f in df.filename_hr]
    data = np.array([signal for signal, meta in data])
    return data

path = ''
sampling_rate = 100

# load and convert annotation data
Y = pd.read_csv('ptbxl_database.csv', index_col='ecg_id')
Y.scp_codes = Y.scp_codes.apply(lambda x: ast.literal_eval(x))
# Load raw signal data
X = load_raw_data(Y, sampling_rate, path)

# Load scp_statements.csv for diagnostic aggregation
agg_df = pd.read_csv('scp_statements.csv', index_col=0)
agg_df = agg_df[agg_df.diagnostic == 1]

def aggregate_diagnostic(y_dic):
    tmp = []
    for key in y_dic.keys():
        if key in agg_df.index:
            tmp.append(agg_df.loc[key].diagnostic_class)
    return list(set(tmp))

# Apply diagnostic superclass
Y['diagnostic_superclass'] = Y.scp_codes.apply(aggregate_diagnostic)

# Split data into train and test
test_fold = 10
# Train
X_train = X[np.where(Y.strat_fold != test_fold)]
y_train = Y[(Y.strat_fold != test_fold)].diagnostic_superclass
# Test
X_test = X[np.where(Y.strat_fold == test_fold)]
y_test = Y[Y.strat_fold == test_fold].diagnostic_superclass
# tenserflow 训练
print(X_test , y_test)
print(type(y_test))
print(type(X_test))
# <class 'pandas.core.series.Series'>
# <class 'numpy.ndarray'>

#该数据类型
y_train = y_train.astype(int[2])
y_test = y_test.astype(int[2])

X_train = tf.convert_to_tensor(X_train)
X_test = tf.convert_to_tensor(X_test)



# artificial intelligence and the training
model = tf.keras.models.Sequential([
    # 卷积层
    tf.keras.layers.Conv1D(filters = 96,
                           kernel_size = 3,
                           padding = "same"),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Activation("relu"),
    tf.keras.layers.AveragePooling1D(pool_size = 4,
                                     padding = "valid"),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation = "sigmoid",
                          kernel_regularizer = tf.keras.regularizers.l1()),
    tf.keras.layers.Dense(5, activation = "sigmoid",
                          kernel_regularizer = tf.keras.regularizers.l2())
])

model.compile(optimizer="nadam",
              loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics = ["sparse_categorical_accuracy"])

model.fit(X_train,y_train,
          batch_size = 64,
          epochs = 10000,
          validation_data = (X_test, y_test),
          validation_freq = 1)

history = model.fit(X_train,y_train,
          batch_size = 64,
          epochs = 10000,
          validation_data = (X_test, y_test))
model.summary()

# 可视化
acc=history.history['sparse_categorical_accuaracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss=history.history['loss']
val_loss = history.history['val_loss']

plt.subplot(1,2,1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accurcy')
plt.legend()

plt.subplot(1,2,1)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()

这段代码的输出是

[[[-0.145 -0.097  0.049 ... -0.452 -0.269 -0.032]
  [-0.115 -0.083  0.032 ... -0.339 -0.246 -0.041]
  [-0.064 -0.06   0.005 ... -0.188 -0.199 -0.04 ]
  ...
  [ 0.23   0.292  0.063 ...  0.443  0.362  0.222]
  [ 0.251  0.319  0.067 ...  0.498  0.405  0.261]
  [ 0.338  0.361  0.023 ...  0.515  0.447  0.299]]

 [[ 0.182  0.196  0.013 ...  0.413  0.279  0.284]
  [ 0.23   0.309  0.079 ...  0.524  0.4    0.402]
  [ 0.125 -0.092 -0.217 ... -0.002  0.022  0.181]
  ...
  [-0.076 -0.085 -0.009 ... -0.02  -0.01   0.032]
  [-0.048 -0.067 -0.019 ...  0.007  0.002  0.008]
  [-0.04  -0.053 -0.013 ...  0.032  0.022  0.015]]

 [[ 0.036  0.025 -0.011 ...  0.082  0.067 -0.014]
  [ 0.054  0.044 -0.01  ...  0.11   0.091  0.003]
  [ 0.095  0.09  -0.005 ...  0.143  0.123  0.035]
  ...
  [-0.261 -0.018  0.242 ... -0.039 -0.066 -0.082]
  [-0.049 -0.039  0.01  ... -0.043 -0.064 -0.2  ]
  [ 0.071 -0.047 -0.118 ... -0.049 -0.067 -0.082]]

 ...

 [[-0.045 -0.055 -0.01  ... -0.145 -0.165 -0.124]
  [-0.045 -0.055 -0.01  ... -0.142 -0.161 -0.119]
  [-0.042 -0.056 -0.013 ... -0.136 -0.154 -0.111]
  ...
  [ 0.03   0.147  0.117 ... -0.038 -0.101 -0.116]
  [ 0.059  0.158  0.099 ... -0.001 -0.09  -0.107]
  [ 0.076  0.158  0.082 ...  0.017 -0.077 -0.098]]

 [[-0.059 -0.023  0.036 ...  0.096  0.031 -0.014]
  [-0.033  0.007  0.04  ...  0.127  0.056  0.007]
  [-0.004  0.036  0.04  ...  0.184  0.108  0.041]
  ...
  [-0.029 -0.029  0.    ... -0.072 -0.123 -0.173]
  [-0.07  -0.063  0.007 ... -0.115 -0.119 -0.224]
  [-0.015 -0.048 -0.033 ... -0.121 -0.121 -0.209]]

 [[-0.031 -0.001  0.03  ...  0.048  0.024 -0.001]
  [-0.015  0.015  0.03  ...  0.051  0.024  0.   ]
  [-0.007  0.018  0.025 ...  0.064  0.021 -0.002]
  ...
  [-0.109 -0.054  0.055 ... -0.057 -0.082 -0.09 ]
  [-0.091 -0.067  0.024 ...  0.003 -0.047 -0.085]
  [ 0.013 -0.237 -0.249 ...  0.336  0.267  0.112]]] ecg_id
9        [NORM]
38       [NORM]
40       [NORM]
57       [NORM]
59       [NORM]
          ...  
21809    [NORM]
21812      [CD]
21818    [NORM]
21819      [CD]
21826      [MI]
Name: diagnostic_superclass, Length: 2198, dtype: object
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>

但后面报ValueError: setting an array element with a sequence.该怎么处理啊

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-07-16 08:44
    关注

    引自免费微信小程序:皆我百晓生

    这个问题是在尝试将TensorFlow的tensor转换为数组时出现的。错误“ValueError: setting an array element with a sequence”意味着你正在尝试将一个序列(如列表或数组)赋值给numpy数组的单个元素位置。这是因为你的数据集数据(numpy数组)与模型输入(TensorFlow tensor)的数据类型不匹配。你的数据集中的数据看起来是二维的,而你的模型期望的是一维输入数据。这意味着你需要在训练模型之前将数据集转换为一维。为此,你可以通过如下代码对输入数据进行reshape处理。下面的代码将数据展平并赋值给 X_trainX_test

    X_train = X_train.reshape(-1, 1)  # 将数据展平并添加到训练集
    X_test = X_test.reshape(-1, 1)  # 将数据展平并添加到测试集
    

    这样处理后,你就可以将处理后的数据输入到模型中了。请注意,这样做会使得数据的形状改变,因此在应用这种处理方式时需要谨慎。如果输入数据的结构很重要并且需要在处理时保留其形状信息,你可能需要采用不同的方法来处理这个问题。你可能需要重新审视你的数据和模型输入的结构以确保两者之间的匹配。希望这个答案能帮到你解决问题!如果还有其他问题,欢迎继续提问。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月16日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见