问题
I am creating a sklearn pipeline that consists of 3 steps:
- Transforms pandas dataframe into 3D array
- Transforms 3D array into recurrence plot (image)
- Trains an image classification model using Keras
This is my initial data set:
train_df - pandas dataframe
id cycle s1
1 1 0.05
1 2 0.04
1 3 0.05
1 4 0.05
2 1 0.02
2 2 0.03
y_train
array([[1., 0., 0.],
[1., 0., 0.],
...
[1., 0., 0.]], dtype=float32)
When I run my current code (see below), I get the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'id'
Code (As you may notice I only transform X
in the pipeline. Ideally I would like to transform y
in the pipeline as well):
import numpy as np
from keras.wrappers.scikit_learn import KerasClassifier
import tensorflow as tf
from tensorflow.keras.models import * # I know that this is not a best way to import libraries
from tensorflow.keras.layers import * # due to possible issues with namespace
from tensorflow.keras.optimizers import * # But my current problem is not related to this aspect
from tensorflow.keras.utils import * # Therefore please ignore it.
from tensorflow.keras.callbacks import *
from sklearn.pipeline import Pipeline
def create_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(50, 50, 17)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model
def _rec_plot(s, eps=0.10, steps=10):
d = pdist(s[:,None])
d = np.floor(d/eps)
d[d>steps] = steps
Z = squareform(d)
return Z
def 2Dto3D(X, y=None):
x = []
for engine_id in X.id.unique():
for sequence in gen_sequence(X[X.id==engine_id], sequence_length, sequence_cols):
x.append(sequence)
x = np.asarray(x)
return x
def ArrayToImage(x, y=None):
x = np.apply_along_axis(_rec_plot, 1, x).astype('float16')
return x
transformer1 = sklearn.preprocessing.FunctionTransformer(func=2Dto3D)
transformer2 = sklearn.preprocessing.FunctionTransformer(func=ArrayToImage)
clf = KerasClassifier(build_fn=create_model, verbose=0)
blackbox_model = Pipeline([('2Dto3D', transformer1),('3DtoImage', transformer2),('clf',clf)])
blackbox_model.fit(train_df, y_train)
来源:https://stackoverflow.com/questions/63330277/attributeerror-numpy-ndarray-object-has-no-attribute-id