<jupyter_start><jupyter_text>Sklearn, XGBoost  sklearn.ensemble.RandomForestClassifier<jupyter_code>import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.stats as sts
import seaborn as sns
from contextlib import contextmanager

sns.set()
sns.set_style("whitegrid")

color_palette = sns.color_palette('deep') + sns.color_palette('husl', 6) + sns.color_palette('bright') + sns.color_palette('pastel')

%matplotlib inline
sns.palplot(color_palette)

# will be in numpy >= 1.15
@contextmanager
def printoptions(*args, **kwargs):
    original = np.get_printoptions()
    np.set_printoptions(*args, **kwargs)
    try:
        yield
    finally: 
        np.set_printoptions(**original)
        
def ndprint(a, precision=3):
    with printoptions(precision=precision, suppress=True):
        print(a)
from sklearn import datasets, metrics, model_selection as mdsel, tree, ensemble
import xgboost as xgb<jupyter_output><empty_output><jupyter_text>Данные Задача на kaggle: https://www.kaggle.com/c/bioresponseДанные: https://www.kaggle.com/c/bioresponse/dataПо данным характеристикам молекулы требуется определить, будет ли дан биологический ответ (biological response).Признаки нормализаваны.Для демонстрации используется обучающая выборка из исходных данных train.csv, файл с данными прилагается.<jupyter_code>bioresponce = pd.read_csv('data/17_19_bioresponse_train.csv', header=0, sep=',')
bioresponce.head()
bioresponce_target = bioresponce.Activity.values
bioresponce_data = bioresponce.iloc[:, 1:]<jupyter_output><empty_output><jupyter_text>Модель RandomForestClassifier  Зависимость качества от количества деревьев<jupyter_code>n_trees = [1] + np.arange(10, 55, 5) 
%%time
scoring = []
for n_tree in n_trees:
    estimator = ensemble.RandomForestClassifier(n_estimators = n_tree, min_samples_split=5, random_state=1)
    score = mdsel.cross_val_score(estimator, bioresponce_data, bioresponce_target, 
                                             scoring = 'accuracy', cv = 3)    
    scoring.append(score)
scoring = np.asmatrix(scoring)
scoring
plt.plot(n_trees, scoring.mean(axis = 1), marker='.', label='RandomForest')
plt.grid(True)
plt.xlabel('n_trees')
plt.ylabel('score')
plt.title('Accuracy score')
plt.legend(loc='lower right')<jupyter_output><empty_output><jupyter_text>Кривые обучения для деревьев большей глубины<jupyter_code>%%time
xgb_scoring = []
for n_tree in n_trees:
    estimator = xgb.XGBClassifier(learning_rate=0.1, max_depth=5, n_estimators=n_tree, min_child_weight=3)
    score = mdsel.cross_val_score(estimator, bioresponce_data, bioresponce_target, 
                                             scoring = 'accuracy', cv = 3)    
    xgb_scoring.append(score)
xgb_scoring = np.asmatrix(xgb_scoring)
xgb_scoring
plt.plot(n_trees, scoring.mean(axis = 1), marker='.', label='RandomForest')
plt.plot(n_trees, xgb_scoring.mean(axis = 1), marker='.', label='XGBoost')
plt.grid(True)
plt.xlabel('n_trees')
plt.ylabel('score')
plt.title('Accuracy score')
plt.legend(loc='lower right')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11514758352254298, 'fraction_numerical': 0.009082062925721699, 'mean_word_length': 5.293877551020408, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '526970', 'license': 'MIT', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1146, 'n_words': 207, 'path': '02_supervised_learning/19_w4p_sklearn.rf_vs_gb.ipynb', 'repo_name': 'pkuderov/mlda-course', 'size': '3083'}