Search
 
SCRIPT & CODE EXAMPLE
 

C

columntransformer in randomizedsearchcv

import seaborn as sns
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, KBinsDiscretizer, MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

df = sns.load_dataset('titanic')[['survived', 'age', 'embarked']]
X_train, X_test, y_train, y_test = train_test_split(df.drop(columns='survived'), df['survived'], test_size=0.2, 
                                                    random_state=123)
num = ['age']
cat = ['embarked']

num_transformer = Pipeline(steps=[('imputer', SimpleImputer()), 
                                  ('discritiser', KBinsDiscretizer(encode='ordinal', strategy='uniform')),
                                  ('scaler', MinMaxScaler())])

cat_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
                                  ('onehot', OneHotEncoder(handle_unknown='ignore'))])

preprocessor = ColumnTransformer(transformers=[('num', num_transformer, num),
                                               ('cat', cat_transformer, cat)])

pipe = Pipeline(steps=[('preprocessor', preprocessor),
                       ('classiffier', LogisticRegression(random_state=1, max_iter=10000))])

param_grid = {'preprocessor__num__imputer__strategy' : ['mean', 'median'],
              'preprocessor__num__discritiser__n_bins' : range(5,10),
              'classiffier__C' : [0.1, 10, 100],
              'classiffier__solver' : ['liblinear', 'saga']}
grid_search = GridSearchCV(pipe, param_grid=param_grid, cv=10)
grid_search.fit(X_train, y_train)
Comment

PREVIOUS NEXT
Code Example
C :: voide means in c 
C :: printing a string with putchar 
C :: largest value in u64 
C :: cast from float to long c 
C :: c code to mips assembly converter online 
C :: Syntax for creating a node 
C :: C++ How to use enums for flags? 
C :: run steam as root 
C :: User input in struct 
C :: What does x = (a<b)? A:b mean in C programming? 
C :: scranton inhabitants 
C :: C access global variable same name 
C :: worst fit program in c 
C :: clipboard lib 
C :: ssl_get_servername return null 
C :: c disable struct padding 
C :: transform yt video into background overlay 
C :: counting 7s in an integer c 
C :: esp rainmaker led 
C :: taking input and converting it to a string in c 
C :: ringing a bell using c 
C :: how to compress image in c 
C :: comando para ejecutar hilos en c 
C :: function pointer in c 
C :: printf("%d", 10 ? 0 ? 5:1:1:12) what will print 
C :: c check if is a right triangle 
Dart :: flutter text form field change underline color 
Dart :: flutter width infinity 
Dart :: flutter snackbar shape 
Dart :: flutter get device width 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =