<jupyter_start><jupyter_text>Kaggle competition [link](https://www.kaggle.com/c/tabular-playground-series-nov-2021/data) 1. Download dataset<jupyter_code>!pip install -q kaggle
from google.colab import files

files.upload()

!mkdir ~/.kaggle
!cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json
!kaggle competitions download -c tabular-playground-series-nov-2021<jupyter_output><empty_output><jupyter_text>2. Unzip and read train dataset<jupyter_code>!unzip train.csv.zip
!unzip test.csv.zip
import pandas as pd
raw_data = pd.read_csv('train.csv')
raw_data.head()
raw_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 600000 entries, 0 to 599999
Columns: 102 entries, id to target
dtypes: float64(100), int64(2)
memory usage: 466.9 MB<jupyter_text>2. Prepare train dataset<jupyter_code>X = raw_data.copy()
X.pop('id')
y = X.pop('target')<jupyter_output><empty_output><jupyter_text>3. Model choice<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
from numpy.random import uniform
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import BernoulliNB
clf1 = SGDClassifier(n_jobs=-1, random_state=1)
clf2 = LogisticRegression(n_jobs=-1, random_state=1)
clf3 = LinearSVC(dual=False, random_state=1)
clf4 = BernoulliNB()
pipe = Pipeline([('scaler', StandardScaler()), ('classifier', clf1)])
params1 = {'classifier__loss' : ['hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron'],
           'classifier__penalty' : ['l2', 'l1', 'elasticnet'],
           'classifier__alpha' : uniform(0, 1, 1000),
           'classifier__max_iter' : range(1000),
           'classifier__epsilon' : uniform(0, 1, 100),
           'classifier__learning_rate' : ['optimal', 'constant', 'invscaling', 'adaptive'],
           'classifier__eta0' : uniform(0, 1, 100),
           'classifier__power_t' : uniform(0, 1, 100),
           'classifier__class_weight' : [None, 'balanced'],
           'classifier' : [clf1]
           }

params2 = {'classifier__penalty' : ['l1', 'l2', 'elasticnet', 'none'],
           'classifier__tol': uniform(0, 1, 1000),
           'classifier__C': [0.1, 1.0, 10.0, 100.0],
           'classifier__class_weight' : [None, 'balanced'],
           'classifier__solver' : ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
           'classifier__multi_class' : ['auto', 'ovr', 'multinomial'],
           'classifier' : [clf2]}

params3 = {'classifier__penalty' : ['l1', 'l2'],
           'classifier__loss' : ['hinge', 'squared_hinge'],
           'classifier__tol': uniform(0, 1, 1000),
           'classifier__C': [0.1, 1.0, 10.0, 100.0],
           'classifier__multi_class' : ['ovr', 'crammer_singer'],
           'classifier__intercept_scaling' : [0.1, 1.0, 10.0, 100.0],
           'classifier__class_weight' : [None, 'balanced'],
           'classifier' : [clf3]
           }

params4 = {'classifier__alpha' : uniform(0, 1, 1000),
           'classifier__binarize' : uniform(0, 1, 1000),
           'classifier' : [clf4]
           }<jupyter_output><empty_output><jupyter_text>a) Best score<jupyter_code>params = [params1, params2, params3, params4]

random_grid = RandomizedSearchCV(pipe, params, n_jobs=-1)
random_grid.fit(X, y)
random_grid.best_estimator_
score = round(random_grid.best_score_, 2)
print(f'Best score: {score * 100}%')<jupyter_output>Best score: 73.0%<jupyter_text>b) Best model and parameters<jupyter_code>random_grid.best_estimator_<jupyter_output><empty_output><jupyter_text>4. Predict test values<jupyter_code>test_set = pd.read_csv('test.csv')
test_set.head()
test_index = test_set.pop('id')
y_pred = random_grid.predict(test_set)
y_pred
submission_dict = {'id': test_index,
                   'target' : y_pred}

submission_df = pd.DataFrame(submission_dict)
submission_df.head()
submission_df.to_csv('submission.csv', index=False)
print('File created')<jupyter_output>File created
{'fraction_non_alphanumeric': 0.14587426326129665, 'fraction_numerical': 0.04002946954813359, 'mean_word_length': 4.526458616010855, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '334879', 'license': 'MIT', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1464, 'n_words': 278, 'path': 'Tabular_Playground_Series_Nov_2021.ipynb', 'repo_name': 'robmaz22/Kaggle-competitions', 'size': '4072'}