Kaggle competition: SERPRO - Abalone
<SOS> metric: rmse Kaggle data source: serpro-abalone<define_variables>
import numpy as np
import pandas as pd
data.show_batch(rows=3, figsize=(7,7))<compute_test_metric>
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import VotingRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import Lars
from sklearn.linear_model import LassoLars
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import BayesianRidge
from sklearn.linear_model import ARDRegression
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import RANSACRegressor
from sklearn.linear_model import TheilSenRegressor
from sklearn.linear_model import HuberRegressor
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neighbors import RadiusNeighborsRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
from sklearn.svm import NuSVR
from sklearn.svm import LinearSVR
from sklearn.tree import DecisionTreeRegressor
from xgboost import XGBRegressor
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )<choose_model_class>
train_data = pd.read_csv('.. /input/abalone-train.csv', index_col='id' )
learn = cnn_learner(data, base_arch=models.resnet50, metrics=[quadratic_kappa])
 learn.summary()<find_best_params>
test_data = pd.read_csv('.. /input/abalone-test.csv', index_col='id' )
learn.unfreeze()
learn.lr_find()
learn.recorder.plot(suggestion=True )<train_model>
train_data = pd.get_dummies(train_data)
 test_data = pd.get_dummies(test_data )
learn.fit_one_cycle(10, max_lr=slice(1e-6,1e-3))<set_options>
data = train_data
outliers = np.concatenate((
data[(data['height'] < 0.01)|(data['height'] > 0.3)].index,
data[(data['viscera_weight'] < 0.0001)|(data['viscera_weight'] > 0.6)].index 
), axis=0)
 train_data.drop(outliers, inplace=True )
learn.export()
learn.save('stage-2' )<predict_on_test>
X_train = train_data.drop(['rings'], axis=1)
 y_train = train_data['rings']
print('Forma dos dados de treino:', X_train.shape, y_train.shape )
preds, y, losses = learn.get_preds(with_loss=True )<choose_model_class>
models = []
models.append(( 'LinReg', LinearRegression(n_jobs=-1, fit_intercept=True, normalize=True)))
 models.append(( 'LogReg', LogisticRegression(n_jobs=-1, random_state=42, multi_class='auto', C=1000, solver='newton-cg')))
 models.append(( 'OMP', OrthogonalMatchingPursuit(n_nonzero_coefs=7, fit_intercept=True, normalize=True)))
 models.append(( 'PAR', PassiveAggressiveRegressor(random_state=42, C=0.1, fit_intercept=True, max_iter=1000, tol=0.001)))
 models.append(( 'PP', Perceptron(random_state=42, penalty='l2', alpha=1e-3, fit_intercept=True, max_iter=1000, tol=1e-3)))
 models.append(( 'RANSAC', RANSACRegressor(random_state=42, min_samples=0.75)))
 models.append(( 'Ridge', Ridge(random_state=42, alpha=0.001, fit_intercept=True, normalize=True)))
 models.append(( 'SGD', SGDRegressor(random_state=42, alpha=1e-6, fit_intercept=True, penalty=None, tol=1e-3)))
 models.append(( 'TSR', TheilSenRegressor(random_state=42, n_jobs=-1, fit_intercept=True)))
 models.append(( 'DTR', DecisionTreeRegressor(random_state=42, max_depth=4, min_samples_split=0.25)))
 models.append(( 'GPR', GaussianProcessRegressor(random_state=42, alpha=0.01, normalize_y=False)))
 models.append(( 'KRR', KernelRidge(alpha=0.1)))
 models.append(( 'GNB', GaussianNB(var_smoothing=0.001)))
 models.append(( 'kNN', KNeighborsRegressor(n_jobs=-1, n_neighbors=11, weights='distance')))
 models.append(( 'SVM', SVR(gamma='auto', kernel='linear')))
 models.append(( 'MLP', MLPRegressor(random_state=42, max_iter=500,
activation='tanh', hidden_layer_sizes=(5, 2), solver='lbfgs')))
 models.append(( 'RFR', RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=100, max_depth=9)))
 models.append(( 'GBR', GradientBoostingRegressor(random_state=42, learning_rate=0.05, n_estimators=100,
subsample=0.8, max_depth=6, max_features=0.75)))
 models.append(( 'ETR', ExtraTreesRegressor(random_state=42, n_jobs=-1, n_estimators=200, max_features=0.75)))
 models.append(( 'BDTR', BaggingRegressor(random_state=42, n_jobs=-1, base_estimator=DecisionTreeRegressor() ,
max_features=0.75, n_estimators=75)))
 models.append(( 'ABDTR', AdaBoostRegressor(random_state=42, n_estimators=200, base_estimator=DecisionTreeRegressor())))
 models.append(( 'XGBR', XGBRegressor(random_state=42, n_jobs=-1, learning_rate=0.1,
n_estimators=50, max_depth=5, objective='reg:squarederror')))
 models.append(( 'VR', VotingRegressor(estimators=[ 
('MLP', MLPRegressor(random_state=42, max_iter=500,
activation='tanh', hidden_layer_sizes=(5, 2), solver='lbfgs')) , 
('GPR', GaussianProcessRegressor(random_state=42, alpha=0.01, normalize_y=False)) , 
('GBR', GradientBoostingRegressor(random_state=42, learning_rate=0.05, n_estimators=100,
subsample=0.8, max_depth=6, max_features=0.75))
], n_jobs=-1, weights=(2,1,1))))
<predict_on_test><EOS> 
sufixo_arquivo = '05set'
for name, model in models:
print(model, '
')
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 submission = pd.DataFrame({
'id': X_test.index,
'rings': y_pred
})
 submission.set_index('id', inplace=True)
 filename = 'abalone-submission-p-%s-%s.csv' %(sufixo_arquivo, name.lower())
 submission.to_csv(filename )