Question 11

Anything wrong with my code:

from sklearn.compose import make_column_selector,ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline

categorical_columns=make_column_selector(dtype_include=object)
numerical_columns=make_column_selector(dtype_include=int)
data_cat=data[categorical_columns]
data_num=data[numerical_columns]

ct=ColumnTransformer([("cat",OneHotEncoder(handle_unknown="ignore"),data_cat),
                      ("num",StandardScaler(),data_num)],
                        remainder="passthrough")
model=Pipeline(steps=[("ct",ct),("lr",LogisticRegression())])
cv_better=cross_validate(model,ct,target,cv=10,return_estimator=True)

Getting singleton error. ColumnTransformer transformation also not happening to the data

Error:TypeError:
Singleton array array(ColumnTransformer(remainder='passthrough',
                  transformers=[('cat', OneHotEncoder(handle_unknown='ignore'),
                                            workclass      education       marital-status          occupation  \
0            Private           11th        Never-married   Machine-op-inspct   
1            Private        HS-grad   Married-civ-spouse     Farming-fishing   
2          Local-gov     Assoc-acdm   Married-civ-spouse     Protective-serv   
3            Private   Some-college   Married-civ-spouse   Machine-op-inspct   
4                  ?   Some-...
48838      Husband   White     Male   United-States  
48839    Unmarried   White   Female   United-States  
48840    Own-child   White     Male   United-States  
48841         Wife   White   Female   United-States  

[48842 rows x 8 columns]),
                                ('num', StandardScaler(),
                                        age  capital-gain  capital-loss  hours-per-week
0       25             0             0              40
1       38             0             0              50
2       28             0             0              40
3       44          7688             0              40
4       18             0             0              30
...    ...           ...           ...             ...
48837   27             0             0              38
48838   40             0             0              40
48839   58             0             0              40
48840   22             0             0              20
48841   52         15024             0              40

[48842 rows x 4 columns])]), dtype=object) cannot be considered a valid collection.

The above code is not correct. To get the categorical columns you need to call the selector.

categorical_columns_selector=make_column_selector(dtype_include=object)
numerical_columns_selector=make_column_selector(dtype_include=int)
categorical_columns = catetogrical_columns_selector(data)
numerical_columns = numerical_columns_selector(data)
data_cat=data[categorical_columns]
data_num=data[numerical_columns]

The selector is a Python class that when called on some dataframe will return the list of the desired columns.

made the correction still there is a problem with code.

Please let me know. Code after correction:

from sklearn.compose import make_column_selector,ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline

categorical_columns_selector=make_column_selector(dtype_include=object)
categorical_columns=categorical_columns_selector(data)
numerical_columns_selector=make_column_selector(dtype_include=int)
numerical_columns=numerical_columns_selector(data)
data_cat=data[categorical_columns]
data_num=data[numerical_columns]

ct=ColumnTransformer(transformers=[("cat",OneHotEncoder(handle_unknown="ignore"),data_cat),
                      ("num",StandardScaler(),data_num)],
                        remainder="passthrough")
model=make_pipeline(ct,LogisticRegression())
cv_better=cross_validate(model,ct,target,cv=10,return_estimator=True)

Please provide the full traceback to understand what is the error and by which functions it is raised.
Without this information, it is difficult to answer.