OrdinalEncoder in classification and TypeError: argument must be a string or number

Viewed 14

I tried to run the below code, but I am getting

TypeError: argument must be a string or number

all the values are converted to string.

def preprocess_breast_attribute(train, test):
    
    continous = ["deg-malig"]
    
    sc = StandardScaler()
    
    traincontinous = sc.fit_transform(train[continous])
    testcontinous = sc.transform(test[continous])
    
    #onehotencoder always need 2D input not series
    
    categorical = ["age","tumor-size", "inv-nodes","menopause", "node-caps", "deg-malig", "breast", "breast-quad", "irradiat"]
    
    encoder = OrdinalEncoder()
    
    trainCategorical = encoder.fit_transform(np.array(train[categorical]).reshape(-1,1))
    testCategorical = encoder.transform(np.array(test[categorical]).reshape(-1,1))
    
    trainX = np.hstack([traincontinous, trainCategorical])
    testX = np.hstack([testcontinous, testCategorical])
    
    
    print(trainX.shape)
    print(testX.shape)
    
    
    return trainX, testX
   

df

train, test = train_test_split(df, test_size = 0.2, random_state = 42)
    
trainX, testX = preprocess_breast_attribute(train, test)    
0 Answers
Related