{"id":13692267,"url":"https://github.com/pbrusco/crystal-learn","last_synced_at":"2025-05-02T19:31:44.272Z","repository":{"id":148933334,"uuid":"64085111","full_name":"pbrusco/crystal-learn","owner":"pbrusco","description":"Machine Learning in Crystal ","archived":false,"fork":false,"pushed_at":"2017-07-25T18:12:16.000Z","size":216,"stargazers_count":42,"open_issues_count":1,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-12T18:39:11.829Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pbrusco.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-07-24T20:52:37.000Z","updated_at":"2024-07-19T06:29:03.000Z","dependencies_parsed_at":"2024-01-14T19:13:31.953Z","dependency_job_id":"4f7ec11d-ba03-479e-8865-585ec99b4002","html_url":"https://github.com/pbrusco/crystal-learn","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrusco%2Fcrystal-learn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrusco%2Fcrystal-learn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrusco%2Fcrystal-learn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrusco%2Fcrystal-learn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pbrusco","download_url":"https://codeload.github.com/pbrusco/crystal-learn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252095266,"owners_count":21693888,"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","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":[],"created_at":"2024-08-02T17:00:55.381Z","updated_at":"2025-05-02T19:31:44.014Z","avatar_url":"https://github.com/pbrusco.png","language":"Crystal","funding_links":[],"categories":["Machine Learning"],"sub_categories":[],"readme":"## An sklearn-like machine-learning library for Crystal\n\nExample (that can be found in examples folder)\n```crystal\nrequire \"csv\"\nrequire \"../knn\"\nrequire \"../trees\"\nrequire \"../array\"\nrequire \"../ml\"\n\nputs \"Loading IRIS dataset\"\n\nx, y = ML.load_floats_csv(\"iris.csv\")\nputs \"Shapes: X: #{x.shape}, y: #{y.shape}\"\n\ndef folds_accuracy(clf, x, y, *, n_folds k)\n  accuracies = [] of Float64\n  folds = ML.kfold_cross_validation(n_folds: k, dataset_size: y.size)\n\n  folds.each do |train_index, test_index|\n    x_train, y_train = x[train_index], y[train_index]\n    x_test, y_test = x[test_index], y[test_index]\n\n    clf.fit(x_train, y_train)\n    y_pred = clf.predict(x_test)\n\n    accuracies \u003c\u003c ML.accuracy(y_test, y_pred)\n  end\n  accuracies.mean\nend\n\nputs \"------------------ KNN -------------------\"\n\n(5..150).step(10).each do |n|\n  clf = ML::Classifiers::KNeighborsClassifier(typeof(x.first.first), typeof(y.first)).new(n_neighbors: n)\n  folds_acc = folds_accuracy(clf, x, y, n_folds: 10).round(2)\n  puts \"10-folds accuracy #{folds_acc} (KNN - #{n} neighbors)\"\nend\n\nputs \"------------------ TREES -------------------\"\n\n(2..15).each do |max_depth|\n  clf = ML::Classifiers::DecisionTreeClassifier(typeof(x.first.first), typeof(y.first)).new(max_depth: max_depth)\n  folds_acc = folds_accuracy(clf, x, y, n_folds: 10).round(2)\n  puts \"10-folds accuracy #{folds_acc} (DecisionTreeClassifier - max_depth: #{max_depth})\"\nend\n  # uncomment to vizualize the tree:\n  # clf.show_tree(%w(sepal_length sepal_width petal_length petal_width species))\nputs\n\n```\n\n### Output:\n```\nLoading IRIS dataset\nShapes: X: {150, 4}, y: 150\n------------------ KNN -------------------\n10-folds accuracy 0.97 (KNN - 5 neighbors)\n10-folds accuracy 0.96 (KNN - 15 neighbors)\n10-folds accuracy 0.95 (KNN - 25 neighbors)\n10-folds accuracy 0.93 (KNN - 35 neighbors)\n10-folds accuracy 0.95 (KNN - 45 neighbors)\n10-folds accuracy 0.94 (KNN - 55 neighbors)\n10-folds accuracy 0.91 (KNN - 65 neighbors)\n10-folds accuracy 0.88 (KNN - 75 neighbors)\n10-folds accuracy 0.81 (KNN - 85 neighbors)\n10-folds accuracy 0.62 (KNN - 95 neighbors)\n10-folds accuracy 0.43 (KNN - 105 neighbors)\n10-folds accuracy 0.43 (KNN - 115 neighbors)\n10-folds accuracy 0.56 (KNN - 125 neighbors)\n10-folds accuracy 0.28 (KNN - 135 neighbors)\n10-folds accuracy 0.25 (KNN - 145 neighbors)\n------------------ TREES -------------------\n10-folds accuracy 0.89 (DecisionTreeClassifier - max_depth: 2)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 3)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 4)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 5)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 6)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 7)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 8)\n10-folds accuracy 0.95 (DecisionTreeClassifier - max_depth: 9)\n10-folds accuracy 0.94 (DecisionTreeClassifier - max_depth: 10)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 11)\n10-folds accuracy 0.93 (DecisionTreeClassifier - max_depth: 12)\n10-folds accuracy 0.95 (DecisionTreeClassifier - max_depth: 13)\n10-folds accuracy 0.91 (DecisionTreeClassifier - max_depth: 14)\n10-folds accuracy 0.95 (DecisionTreeClassifier - max_depth: 15)\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbrusco%2Fcrystal-learn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpbrusco%2Fcrystal-learn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbrusco%2Fcrystal-learn/lists"}