{ "cells": [ { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [], "source": [ "import scipy.stats as ss\n", "from sklearn.datasets import make_classification\n", "from sklearn.model_selection import RandomizedSearchCV\n", "from sklearn.ensemble import RandomForestClassifier\n", "from collections.abc import Iterable, Callable\n", "from typing import Union\n", "import pandas as pd\n", "import numpy as np\n", "\n", "from abc import ABC, abstractmethod" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "X, y = make_classification()" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "parameters = {\"n_estimators\": {\"distribution\": \"randint\", \"args\": (10, 100)},\n", " \"min_weight_fraction_leaf\": {\"distribution\": \"norm\", \"args\": (0.25, 0.01)},\n", " \"criterion\": {\"distribution\": \"choice\", \"args\": [\"gini\", \"entropy\"]},\n", " } " ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "def make_distributions(parameters: dict[str, dict[str, Union[str, tuple, Iterable]]]) -> dict[str, Union[Callable, Iterable]]:\n", " params_dict = {}\n", " for parameter, description in parameters.items():\n", " try:\n", " distr = getattr(ss, description['distribution'])\n", " \n", " except AttributeError:\n", " distribution = description['args']\n", " \n", " else:\n", " distribution = distr(*description['args'])\n", " \n", " params_dict[parameter] = distribution\n", " \n", " return params_dict" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RandomizedSearchCV(cv=11, estimator=RandomForestClassifier(),\n", " param_distributions={'criterion': ['gini', 'entropy'],\n", " 'min_weight_fraction_leaf': ,\n", " 'n_estimators': })" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv = RandomizedSearchCV(RandomForestClassifier(), params_dist, cv=11)\n", "cv.fit(X, y)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'criterion': 'entropy',\n", " 'min_weight_fraction_leaf': 0.23251444102460192,\n", " 'n_estimators': 79}" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv.best_params_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" } } }, "nbformat": 4, "nbformat_minor": 2 }