Sklearn Column Transformer not working for mixed data types

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder, OrdinalEncoder
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd
import numpy as np



# Selecting numerical columns
numerical_cols = data[['MonthlyCharges', 'TotalCharges', 'tenure']].to_numpy()

# Selecting an ordinal column
ordinal_cols = data[['Contract']].to_numpy()

# Selecting categorical columns
categorical_cols = data[['PaperlessBilling', 'PaymentMethod']].to_numpy()

# Selecting the target column
target_col = data['Churn'].to_numpy()



# Define the order for the 'Contract' column
contract_order = ['Month-to-month', 'One year', 'Two year']

# Create transformers for the numeric and categorical data
numeric_transformer = Pipeline(steps=[
    ('scaler', StandardScaler()),
('scaler', StandardScaler()),
('scaler', StandardScaler())])

# Assuming 'contract_column' is the name of the contract column in your DataFrame
ordinal_transformer = Pipeline(steps=[
    ('ordinal', OrdinalEncoder(categories=[contract_order]))])

# Assuming 'categorical_columns' is the list of other categorical columns in your DataFrame
categorical_transformer = Pipeline(steps=[
    ('onehot', OneHotEncoder(handle_unknown='ignore')),
     ('onehot', OneHotEncoder(handle_unknown='ignore')),
      ('onehot', OneHotEncoder(handle_unknown='ignore'))])

# Create a ColumnTransformer to apply the transformations to the correct columns
preprocessor = ColumnTransformer([
        ('num', numeric_transformer, numerical_cols),
        ('ord', ordinal_transformer, ordinal_cols),  
        ('cat', categorical_transformer, categorical_cols)
    ])




# Create a pipeline that combines the preprocessor with the model
model = Pipeline(steps=[('preprocessor', preprocessor),
                        ('classifier', LogisticRegression())])

print(numerical_cols)
print(ordinal_cols)
print(categorical_cols)


# Separate the features and the target
X = data[numerical_cols + ordinal_cols + categorical_cols]
y = data[target_col].apply(lambda x: 1 if x == 'Yes' else 0) # Encoding 


# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the model
model.fit(X_train, y_train)




---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 68
     65 print(categorical_cols)
     67 # Separate the features and the target
---> 68 X = data[numerical_cols + ordinal_cols + categorical_cols]
     69 y = data[target_col].apply(lambda x: 1 if x == 'Yes' else 0) # Encoding 
     72 # Split data into train and test sets

TypeError: unsupported operand type(s) for +: 'float' and 'str'

I am trying to do logistic regression with data which involves a variety of datatypes. I split the data into the numerical_cols, ordinal_cols and categorical_cols variables. However, when I print them I see they have not been transformed. I think this is the reason for the error. How can I transform the variables correctly?

New contributor

s213439 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật