text_plain_output_1.png
from catboost import CatBoostRegressor

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import StandardScaler

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import random
seed = 42
random.seed(seed)
np.random.seed(seed)

pd.read_csv('/kaggle/input/30-days-of-ml/sample_submission.csv').head(5)

train = pd.read_csv('/kaggle/input/30-days-of-ml/train.csv')

train.columns

train.isna().sum().sum()

cat = train[['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9']]

ohe = OneHotEncoder().fit_transform(cat).toarray()

cont = train[['cont0', 'cont1', 'cont2', 'cont3', 'cont4', 'cont5', 'cont6', 'cont7', 'cont8', 'cont9', 'cont10', 'cont11', 'cont12', 'cont13']]

cont.corr().style.background_gradient(cmap='coolwarm').set_precision(5)

cont_array = cont.to_numpy()
cont_array.shape

target = train['target']
target

X = np.hstack((ohe, cont_array))
X.shape

y = target.to_numpy()
y.shape

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=seed, test_size=0.2)
(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

from sklearn.preprocessing import StandardScaler
ss = StandardScaler()

X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

from sklearn.metrics import mean_squared_error
def rmse(y_true, y_pred):
    return mean_squared_error(y_true=y_true, y_pred=y_pred) ** 0.5

from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(X_train, y_train)
pred = linreg.predict(X_test)
rmse(y_test, pred)

test_size = 0.2

df = train[train.columns[1:]]
test_bound = int(df.shape[0] * (1 - test_size))
train_df = df[:test_bound]
test_df = df[test_bound:]
X_train = train_df[train_df.columns[:-1]]
y_train = train_df['target']
X_test = test_df[test_df.columns[:-1]]
y_test = test_df['target']
(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

params = {'depth': 7, 'iterations': 111, 'learning_rate': 0.07, 'l2_leaf_reg': 0.36}

model = CatBoostRegressor(**params, random_state=seed, cat_features=['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9'], loss_function='RMSE')
model.fit(train.drop(columns=['id', 'target']), train['target'], verbose=False)
pred = model.predict(X_test)
rmse(y_test, pred)