{"id":19489619,"url":"https://github.com/brackendev/scikit-learn-hy","last_synced_at":"2025-10-09T13:06:47.787Z","repository":{"id":37610198,"uuid":"208638423","full_name":"brackendev/scikit-learn-Hy","owner":"brackendev","description":"An introduction to scikit-learn (machine learning in Python) and Hy (a Lisp dialect embedded in Python)","archived":false,"fork":false,"pushed_at":"2023-07-06T21:45:20.000Z","size":61,"stargazers_count":9,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T02:22:28.564Z","etag":null,"topics":["hy","hylang","machine-learning","python","scikit-learn","tutorial"],"latest_commit_sha":null,"homepage":"http://bracken.dev/","language":"Hy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brackendev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-15T18:28:29.000Z","updated_at":"2023-08-23T18:10:03.000Z","dependencies_parsed_at":"2023-01-17T16:47:20.501Z","dependency_job_id":null,"html_url":"https://github.com/brackendev/scikit-learn-Hy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/brackendev/scikit-learn-Hy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brackendev%2Fscikit-learn-Hy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brackendev%2Fscikit-learn-Hy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brackendev%2Fscikit-learn-Hy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brackendev%2Fscikit-learn-Hy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brackendev","download_url":"https://codeload.github.com/brackendev/scikit-learn-Hy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brackendev%2Fscikit-learn-Hy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001541,"owners_count":26083102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hy","hylang","machine-learning","python","scikit-learn","tutorial"],"created_at":"2024-11-10T21:09:23.281Z","updated_at":"2025-10-09T13:06:47.770Z","avatar_url":"https://github.com/brackendev.png","language":"Hy","readme":"scikit-learn-Hy\n===\n\n**An introduction to [scikit-learn](https://www.scikit-learn.org/) (machine learning in Python) and [Hy](https://www.github.com/hylang/hy/) (a Lisp dialect embedded in Python).**\n\n* [Hy 0.20.0](https://github.com/hylang/hy) and [Python 3.7](https://www.python.org/downloads/release/python-377/) reference platform.\n* Examples are in Hy and tests are in Python (to demonstrate Hy module support).\n\n## TODO\n\n- [ ] Support the latest Hy release.\n- [ ] Support the latest Python release.\n- [ ] Support the latest scikit-learn release.\n\n## Author\n\nBracken Spencer\n\n* [GitHub](https://www.github.com/brackendev)\n* [LinkedIn](https://www.linkedin.com/in/brackenspencer/)\n* [Twitter](https://twitter.com/brackendev)\n\n## License\n\nscikit-learn-Hy is released under the BSD 3-Clause license. See the `LICENSE` file for more info.\n\n## Installation\n\nIt is assumed that [Python 3.7](https://www.python.org/downloads/release/python-377/) and [pyenv](https://github.com/pyenv/pyenv) are installed. Via a command shell in the project directory, execute:\n\n```bash\n$ pip install -r requirements.txt\n```\n\nTo run the tests, execute:\n\n```bash\n$ pytest scikit_learn_tests.py\n```\n\n## Usage\n\nFollow [An introduction to machine learning with scikit-learn and Hy](#an-introduction-to-machine-learning-with-scikit-learn-and-hy) below this **Usage** section.\n\nAdditionally, the example code in the [introduction](#an-introduction-to-machine-learning-with-scikit-learn-and-hy) is also available in this project's `scikit_learn` Hy module. For example:\n\n```hy\n$ hy\nhy 0.20.0 using CPython(default) 3.7.7 on Darwin\n=\u003e (import scikit_learn)\nWelcome to scikit-learn-Hy!\n\n=\u003e (scikit-learn.learning-and-predicting)\narray([8])\n```\n\nAvailable example functions:\n* [learning-and-predicting](#choosing-the-parameters-of-the-model)\n* [model-persistence](#model-persistence)\n* [model-persistence-from-file](#model-persistence)\n* [type-casting-32](#type-casting)\n* [type-casting-64](#type-casting)\n* [type-casting-more1](#type-casting)\n* [type-casting-more2](#type-casting)\n* [refitting-and-updating-parameters1](#refitting-and-updating-parameters)\n* [refitting-and-updating-parameters2](#refitting-and-updating-parameters)\n* [multiclass-vs-multilabel-fitting1](#multiclass-vs-multilabel-fitting)\n* [multiclass-vs-multilabel-fitting2](#multiclass-vs-multilabel-fitting)\n* [multiclass-vs-multilabel-fitting3](#multiclass-vs-multilabel-fitting)\n\n- - -\n\nAn Introduction to Machine Learning with scikit-learn and Hy\n===\n\nMachine Learning: The Problem Setting\n-------------------------------------\n\nIn general, a learning problem considers a set of n\n[samples](https://en.wikipedia.org/wiki/Sample_(statistics)) of\ndata and then tries to predict properties of unknown data. If each sample is\nmore than a single number and, for instance, a multi-dimensional entry\n(aka [multivariate](https://en.wikipedia.org/wiki/Multivariate_random_variable)\ndata), it is said to have several attributes or **features**.\n\nLearning problems fall into a few categories:\n\n * [Supervised learning](https://en.wikipedia.org/wiki/Supervised_learning)\n   in which the data comes with additional attributes that we want to predict.\n   ([Click here](https://scikit-learn.org/stable/supervised_learning.html#supervised-learning)\n   to go to the scikit-learn supervised learning page.) This problem\n   can be either:\n\n    * [Classification](https://en.wikipedia.org/wiki/Classification_in_machine_learning):\n      samples belong to two or more classes and we\n      want to learn from already labeled data how to predict the class\n      of unlabeled data. An example of a classification problem would\n      be handwritten digit recognition, in which the aim is\n      to assign each input vector to one of a finite number of discrete\n      categories.  Another way to think of classification is as a discrete\n      (as opposed to continuous) form of supervised learning where one has a\n      limited number of categories and for each of the n samples provided,\n      one is to try to label them with the correct category or class.\n\n    * [Regression](https://en.wikipedia.org/wiki/Regression_analysis):\n      if the desired output consists of one or more\n      continuous variables, then the task is called *regression*. An\n      example of a regression problem would be the prediction of the\n      length of a salmon as a function of its age and weight.\n\n * [Unsupervised learning](https://en.wikipedia.org/wiki/Unsupervised_learning),\n   in which the training data consists of a set of input vectors x\n   without any corresponding target values. The goal in such problems\n   may be to discover groups of similar examples within the data, where\n   it is called [clustering](https://en.wikipedia.org/wiki/Cluster_analysis),\n   or to determine the distribution of data within the input space, known as\n   [density estimation](https://en.wikipedia.org/wiki/Density_estimation), or\n   to project the data from a high-dimensional space down to two or three\n   dimensions for the purpose of *visualization*. ([Click here](https://scikit-learn.org/stable/unsupervised_learning.html#unsupervised-learning)\n   to go to the Scikit-Learn unsupervised learning page.)\n\n#### Training Set and Testing Set\n\nMachine learning is about learning some properties of a data set\n    and then testing those properties against another data set. A common\n    practice in machine learning is to evaluate an algorithm by splitting a data\n    set into two. We call one of those sets the **training set**, on which we\n    learn some properties; we call the other set the **testing set**, on which\n    we test the learned properties.\n\n\nLoading an Example Dataset\n--------------------------\n\n`scikit-learn` comes with a few standard datasets, for instance the\n[iris](https://en.wikipedia.org/wiki/Iris_flower_data_set) and [digits](https://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Digits)\ndatasets for classification and the [boston house prices dataset](https://archive.ics.uci.edu/ml/machine-learning-databases/housing/) for regression.\n\nIn the following, we start a Hy REPL from our shell and then\nload the ``iris`` and ``digits`` datasets.  Our notational convention is that\n``$`` denotes the shell prompt while ``=\u003e`` denotes the Hy\nREPL prompt:\n\n```hy\n$ hy\n=\u003e (import [sklearn [datasets]])\n=\u003e (setv iris (datasets.load-iris))\n=\u003e (setv digits (datasets.load-digits))\n```\n\nA dataset is a dictionary-like object that holds all the data and some\nmetadata about the data. This data is stored in the ``.data`` member,\nwhich is a ``n_samples, n_features`` array. In the case of supervised\nproblem, one or more response variables are stored in the ``.target`` member. More\ndetails on the different datasets can be found in the :ref:`dedicated\nsection \u003cdatasets\u003e`.\n\nFor instance, in the case of the digits dataset, ``digits.data`` gives\naccess to the features that can be used to classify the digits samples:\n\n```hy\n=\u003e (print digits.data)\n[[ 0.  0.  5. ...  0.  0.  0.]\n [ 0.  0.  0. ... 10.  0.  0.]\n [ 0.  0.  0. ... 16.  9.  0.]\n ...\n [ 0.  0.  1. ...  6.  0.  0.]\n [ 0.  0.  2. ... 12.  0.  0.]\n [ 0.  0. 10. ... 12.  1.  0.]]\n```\n\nand ``digits.target`` gives the ground truth for the digit dataset, that\nis the number corresponding to each digit image that we are trying to\nlearn:\n\n```hy\n=\u003e (print digits.target)\n[0 1 2 ... 8 9 8]\n```\n\n#### Shape of the Data Arrays\n\nThe data is always a 2D array, shape ``(n_samples n_features)``, although\n    the original data may have had a different shape. In the case of the\n    digits, each original sample is an image of shape ``(8 8)`` and can be\n    accessed using:\n\n```hy\n=\u003e (first digits.images)\narray([[ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.],\n       [ 0.,  0., 13., 15., 10., 15.,  5.,  0.],\n       [ 0.,  3., 15.,  2.,  0., 11.,  8.,  0.],\n       [ 0.,  4., 12.,  0.,  0.,  8.,  8.,  0.],\n       [ 0.,  5.,  8.,  0.,  0.,  9.,  8.,  0.],\n       [ 0.,  4., 11.,  0.,  1., 12.,  7.,  0.],\n       [ 0.,  2., 14.,  5., 10., 12.,  0.,  0.],\n       [ 0.,  0.,  6., 13., 10.,  0.,  0.,  0.]])\n```\n\nThe [simple example on this dataset](https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py) illustrates how starting\n    from the original problem one can shape the data for consumption in\n    scikit-learn.\n\n#### Loading from External Datasets\n\nTo load from an external dataset, please refer to [loading external datasets](https://scikit-learn.org/stable/datasets/index.html#external-datasets).\n\nLearning and Predicting\n------------------------\n\nIn the case of the digits dataset, the task is to predict, given an image,\nwhich digit it represents. We are given samples of each of the 10\npossible classes (the digits zero through nine) on which we *fit* an\n[estimator](https://en.wikipedia.org/wiki/Estimator) to be able to *predict*\nthe classes to which unseen samples belong.\n\nIn scikit-learn, an estimator for classification is a Python object that\nimplements the functions ``fit(X, y)`` and ``predict(T)``.\n\nAn example of an estimator is the class ``sklearn.svm.SVC``, which\nimplements [support vector classification](https://en.wikipedia.org/wiki/Support_vector_machine\u003e). The\nestimator's constructor takes as arguments the model's parameters.\n\nFor now, we will consider the estimator as a black box:\n\n```hy\n=\u003e (import [sklearn.svm [SVC]])\n=\u003e (setv clf (SVC :gamma 0.001 :C 100))\n```\n\n#### Choosing the Parameters of the Model\n\n  In this example, we set the value of ``gamma`` manually.\n  To find good values for these parameters, we can use tools\n  such as [grid search](https://scikit-learn.org/stable/modules/grid_search.html#grid-search) and [cross validation](https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation).\n\nThe ``clf`` (for classifier) estimator instance is first\nfitted to the model; that is, it must *learn* from the model. This is\ndone by passing our training set to the ``fit`` method. For the training\nset, we'll use all the images from our dataset, except for the last\nimage, which we'll reserve for our predicting. We select the training set with\nthe ``all-but-last`` Hy function, which produces a new array that contains all but\nthe last item from ``digits.data``:\n\n```hy\n=\u003e (defn all-but-last [lst]\n... (list (drop-last 1 lst)))\n=\u003e (clf.fit (all-but-last digits.data) (all-but-last digits.target))\nSVC(C=100, gamma=0.001)\n```\n\nNow you can *predict* new values. In this case, you'll predict using the last\nimage from ``digits.data``. By predicting, you'll determine the image from the\ntraining set that best matches the last image.\n\n```hy\n=\u003e (clf.predict (cut digits.data -1))\narray([8])\n```\n\nThe corresponding image is:\n\n![](https://scikit-learn.org/stable/_images/sphx_glr_plot_digits_last_image_001.png)\n\nAs you can see, it is a challenging task: after all, the images are of poor\nresolution. Do you agree with the classifier?\n\nA complete example of this classification problem is available as an\nexample that you can run and study:\n[Recognizing hand-written digits](https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html#sphx-glr-auto-examples-classification-plot-digits-classification-py).\n\n\nModel Persistence\n-----------------\n\nIt is possible to save a model in scikit-learn by using Python's built-in\npersistence model, [pickle](https://docs.python.org/2/library/pickle.html):\n\n```hy\n=\u003e (import [sklearn.svm [SVC]])\n=\u003e (import [sklearn [datasets]])\n=\u003e (setv clf (SVC :gamma \"scale\"))\n=\u003e (setv x iris.data)\n=\u003e (setv y iris.target)\n=\u003e (clf.fit x y)\nSVC()\n\n=\u003e (import pickle)\n=\u003e (setv s (pickle.dumps clf))\n=\u003e (setv clf2 (pickle.loads s))\n=\u003e (clf2.predict (cut x 0 1))\narray([0])\n\n=\u003e (first y)\n0\n```\n\nIn the specific case of scikit-learn, it may be more interesting to use\njoblib's replacement for pickle (``joblib.dump`` \u0026 ``joblib.load``),\nwhich is more efficient on big data but it can only pickle to the disk\nand not to a string:\n\n```hy\n=\u003e (import [joblib [dump load]])\n=\u003e (dump clf \"filename.joblib\")\n['filename.joblib']\n```\n\nLater, you can reload the pickled model (possibly in another Hy process)\nwith:\n\n```hy\n=\u003e (setv clf (load \"filename.joblib\"))\n```\n\n##### Note:\n\n``joblib.dump`` and ``joblib.load`` functions also accept file-like object\n    instead of filenames. More information on data persistence with Joblib is\n    available [here](https://joblib.readthedocs.io/en/latest/persistence.html).\n\nNote that pickle has some security and maintainability issues. Please refer to\nsection [Model persistence](https://scikit-learn.org/stable/modules/model_persistence.html#model-persistence) for more detailed information about model\npersistence with scikit-learn.\n\n\nConventions\n-----------\n\nscikit-learn estimators follow certain rules to make their behavior more\npredictive.  These are described in more detail in the [Glossary of Common Terms and API Elements](https://scikit-learn.org/stable/glossary.html#glossary).\n\n#### Type Casting\n\nUnless otherwise specified, input will be cast to ``float64``:\n\n```hy\n=\u003e (import [numpy :as np])\n=\u003e (import [sklearn [random_projection]])\n=\u003e (setv rng (np.random.RandomState 0))\n=\u003e (setv x (np.array (rng.rand 10 2000) :dtype \"float32\"))\n=\u003e (print x.dtype)\nfloat32\n\n=\u003e (setv transformer (random-projection.GaussianRandomProjection))\n=\u003e (setv x-new (transformer.fit-transform x))\n=\u003e (print x-new.dtype)\nfloat64\n```\n\nIn this example, ``x`` is ``float32``, which is cast to ``float64`` by\n``(transformer.fit-transform x)``.\n\nRegression targets are cast to ``float64`` and classification targets are\nmaintained:\n\n```hy\n=\u003e (import [sklearn [datasets]])\n=\u003e (import [sklearn.svm [SVC]])\n=\u003e (setv iris (datasets.load-iris))\n=\u003e (setv clf (SVC :gamma \"scale\"))\n=\u003e (clf.fit iris.data iris.target)\nSVC()\n\n=\u003e (list (clf.predict (list (take 3 iris.data))))\n[0, 0, 0]\n\n=\u003e (clf.fit iris.data (. iris target-names [iris.target]))\nSVC()\n\n=\u003e (list (clf.predict (list (take 3 iris.data))))\n['setosa', 'setosa', 'setosa']\n```\n\nHere, the first ``predict`` returns an integer array, since ``iris.target``\n(an integer array) was used in ``fit``. The second ``predict`` returns a string\narray, since ``iris.target_names`` was for fitting.\n\n#### Refitting and Updating Parameters\n\nHyper-parameters of an estimator can be updated after it has been constructed\nvia the [set_params](https://scikit-learn.org/stable/glossary.html#term-set-params) method. Calling ``fit`` more than\nonce will overwrite what was learned by any previous ``fit``:\n\n```hy\n=\u003e (import [numpy :as np])\n=\u003e (import [sklearn.datasets [load_iris]])\n=\u003e (import [sklearn.svm [SVC]])\n=\u003e (setv x (first (load-iris :return_X_y True)))\n=\u003e (setv y (last (load-iris :return_X_y True)))\n=\u003e (setv clf (SVC :gamma \"auto\"))\n=\u003e (clf.set-params :kernel \"linear\")\nSVC(gamma='auto', kernel='linear')\n\n=\u003e (clf.fit x y)\nSVC(gamma='auto', kernel='linear')\n\n=\u003e (clf.predict (list (take 5 x)))\narray([0, 0, 0, 0, 0])\n\n=\u003e (clf.set-params :kernel \"rbf\" :gamma \"scale\")\nSVC()\n\n=\u003e (clf.fit x y)\nSVC()\n\n=\u003e (clf.predict (list (take 5 x)))\narray([0, 0, 0, 0, 0])\n```\n\nHere, the default kernel ``rbf`` is first changed to ``linear`` via\n[clf.set-params](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.set_params) after the estimator has\nbeen constructed, and changed back to ``rbf`` to refit the estimator and to\nmake a second prediction.\n\n#### Multiclass vs. Multilabel Fitting\n\nWhen using [multiclass classifiers](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.multiclass),\nthe learning and prediction task that is performed is dependent on the format of\nthe target data fit upon:\n\n```hy\n=\u003e (import [sklearn.svm [SVC]])\n=\u003e (import [sklearn.multiclass [OneVsRestClassifier]])\n=\u003e (import [sklearn.preprocessing [LabelBinarizer]])\n=\u003e (setv x '((1 2) (2 4) (4 5) (3 2) (3 1)))\n=\u003e (setv y '(0 0 1 1 2))\n=\u003e (setv clf (SVC :gamma \"scale\" :random-state 0))\n=\u003e (setv classif (OneVsRestClassifier :estimator clf))\n=\u003e (classif.fit x y)\nOneVsRestClassifier(estimator=SVC(random_state=0))\n\n=\u003e (classif.fit x y)\nOneVsRestClassifier(estimator=SVC(random_state=0))\n\n=\u003e (classif.predict x)\narray([0, 0, 1, 1, 2])\n```\n\nIn the above case, the classifier is fit on a 1d array of multiclass labels and\nthe ``predict`` function therefore provides corresponding multiclass predictions.\nIt is also possible to fit upon a 2d array of binary label indicators:\n\n```hy\n=\u003e (setv y (LabelBinarizer))\n=\u003e (setv y (y.fit_transform '(0 0 1 1 2)))\n=\u003e (classif.fit x y)\nOneVsRestClassifier(estimator=SVC(random_state=0))\n\n=\u003e (classif.predict x)\narray([[1, 0, 0],\n       [1, 0, 0],\n       [0, 1, 0],\n       [0, 0, 0],\n       [0, 0, 0]])\n```\n\nHere, the classifier is ``fit``  on a 2d binary label representation of ``y``,\nusing the [LabelBinarizer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelBinarizer.html#sklearn.preprocessing.LabelBinarizer).\nIn this case ``predict`` returns a 2d array representing the corresponding\nmultilabel predictions.\n\nNote that the fourth and fifth instances returned all zeroes, indicating that\nthey matched none of the three labels ``fit`` upon. With multilabel outputs, it\nis similarly possible for an instance to be assigned multiple labels:\n\n```hy\n=\u003e (import [sklearn.preprocessing [MultiLabelBinarizer]])\n=\u003e (setv y (MultiLabelBinarizer))\n=\u003e (setv y (y.fit_transform '((0 1) (0 2) (1 3) (0 2 3) (2 4))))\n=\u003e (classif.fit x y)\nOneVsRestClassifier(estimator=SVC(random_state=0))\n\n=\u003e (classif.predict x)\narray([[1, 1, 0, 0, 0],\n       [1, 0, 1, 0, 0],\n       [0, 1, 0, 1, 0],\n       [1, 0, 1, 0, 0],\n       [1, 0, 1, 0, 0]])\n```\n\nIn this case, the classifier is fit upon instances each assigned multiple labels.\nThe [MultiLabelBinarizer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html#sklearn.preprocessing.MultiLabelBinarizer) is\nused to binarize the 2d array of multilabels to ``fit`` upon. As a result,\n``predict`` returns a 2d array with multiple predicted labels for each instance.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrackendev%2Fscikit-learn-hy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrackendev%2Fscikit-learn-hy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrackendev%2Fscikit-learn-hy/lists"}