{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.datasets import load_iris\n", "from sklearn.preprocessing import MinMaxScaler as skMinMaxScaler" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class MinMaxScaler():\n", " def __init__(self, feature_range=(0, 1)):\n", " self.feature_range = feature_range\n", "\n", " def fit(self, X):\n", " self.data_min_ = X.min(axis=0)\n", " self.data_max_ = X.max(axis=0)\n", " self.data_range_ = self.data_max_ - self.data_min_\n", " self.scale_ = (self.feature_range[1] - self.feature_range[0]) / self.data_range_\n", " return self\n", "\n", " def transform(self, X):\n", " return self.feature_range[0] + (X - self.data_min_) * self.scale_" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "X, _ = load_iris(return_X_y=True)\n", "sc1 = MinMaxScaler().fit(X)\n", "sc2 = skMinMaxScaler().fit(X)\n", "assert np.allclose(sc1.data_min_, sc2.data_min_)\n", "assert np.allclose(sc1.data_max_, sc2.data_max_)\n", "assert np.allclose(sc1.data_range_, sc2.data_range_)\n", "assert np.allclose(sc1.scale_, sc2.scale_)\n", "Xt1 = sc1.transform(X)\n", "Xt2 = sc2.transform(X)\n", "assert np.allclose(Xt1, Xt2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }