{"id":26411494,"url":"https://github.com/kongzii/swiftxgboost","last_synced_at":"2025-09-04T05:18:36.872Z","repository":{"id":54821844,"uuid":"266306441","full_name":"kongzii/SwiftXGBoost","owner":"kongzii","description":"Swift wrapper for XGBoost gradient boosting machine learning framework with Numpy and TensorFlow support.","archived":false,"fork":false,"pushed_at":"2020-08-29T07:01:35.000Z","size":5545,"stargazers_count":26,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-08-21T02:23:45.836Z","etag":null,"topics":["boosting-tree","machine-learning","swift","swift-for-tensorflow","xgboost"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kongzii.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":"2020-05-23T09:43:55.000Z","updated_at":"2025-04-10T15:30:36.000Z","dependencies_parsed_at":"2022-08-14T03:50:41.596Z","dependency_job_id":null,"html_url":"https://github.com/kongzii/SwiftXGBoost","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/kongzii/SwiftXGBoost","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kongzii%2FSwiftXGBoost","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kongzii%2FSwiftXGBoost/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kongzii%2FSwiftXGBoost/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kongzii%2FSwiftXGBoost/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kongzii","download_url":"https://codeload.github.com/kongzii/SwiftXGBoost/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kongzii%2FSwiftXGBoost/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273555459,"owners_count":25126316,"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-09-04T02:00:08.968Z","response_time":61,"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":["boosting-tree","machine-learning","swift","swift-for-tensorflow","xgboost"],"created_at":"2025-03-17T20:34:36.257Z","updated_at":"2025-09-04T05:18:36.821Z","avatar_url":"https://github.com/kongzii.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![codecov](https://codecov.io/gh/kongzii/SwiftXGBoost/branch/master/graph/badge.svg)](https://codecov.io/gh/kongzii/SwiftXGBoost)\n[![Platform](https://img.shields.io/badge/platform-linux%2Cmacos-lightgrey)](https://kongzii.github.io/SwiftXGBoost/)\n[![Swift Version](https://img.shields.io/badge/Swift-5.2-green.svg)]()\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) \n![Ubuntu](https://github.com/kongzii/SwiftXGBoost/workflows/Ubuntu/badge.svg)\n![MacOS](https://github.com/kongzii/SwiftXGBoost/workflows/MacOS/badge.svg)\n\n# XGBoost for Swift\n\nBindings for [the XGBoost system library](https://en.wikipedia.org/wiki/XGBoost). \nThe aim of this package is to mimic [XGBoost Python bindings](https://xgboost.readthedocs.io/en/latest/python/python_intro.html) but, at the same time, utilize the power of Swift and C compatibility. Some things thus behave differently but should provide you maximum flexibility over XGBoost.\n\nCheck out:\n\n- [Examples](https://github.com/kongzii/SwiftXGBoost/tree/master/Examples) \n- [Code documentation](https://kongzii.github.io/SwiftXGBoost/)\n- [ReadTheDocs](https://swiftxgboost.readthedocs.io/)\n\n## Installation\n\n### System library dependency\n\n#### Linux\n\nInstall XGBoost from sources\n\n```\ngit clone https://github.com/dmlc/xgboost\ncd xgboost\ngit checkout tags/v1.1.1\ngit submodule update --init --recursive\nmkdir build\ncd build\ncmake ..\nmake\nmake install\nldconfig\n```\n\nOr you can use provided installation script\n\n```\n./install.sh\n```\n\n#### macOS\n\nYou can build and install similarly as on Linux, or just use brew\n\n```\nbrew install xgboost\n```\n\n##### Note \n\nBefore version 1.1.1, XGBoost did not create pkg-config. This was fixed with PR [Add pkgconfig to cmake #5744](https://github.com/dmlc/xgboost/pull/5744).\n\nIf you are using for some reason older versions, you may need to specify path to the XGBoost libraries while building, e.g.:\n\n```\nswift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib\n```\n\nor create pkg-config file manualy. Example of it for `macOS 10.15` and `XGBoost 1.1.0` is\n\n```\nprefix=/usr/local/Cellar/xgboost/1.1.0\nexec_prefix=${prefix}/bin\nlibdir=${prefix}/lib\nincludedir=${prefix}/include\n\nName: xgboost\nDescription: XGBoost machine learning libarary.\nVersion: 1.1.0\nCflags: -I${includedir}\nLibs: -L${libdir} -lxgboost\n```\n\nand needs to be placed at `/usr/local/lib/pkgconfig/xgboost.pc`\n\n### Package\n\nAdd a dependency in your your `Package.swift`\n\n```swift\n.package(url: \"https://github.com/kongzii/SwiftXGBoost.git\", from: \"0.0.0\"),\n```\n\nImport Swifty XGBoost \n\n```swift\nimport XGBoost\n```\n\nor directly C library \n\n```swift\nimport CXGBoost\n```\n\nboth `Booster` and `DMatrix` classes are exposing pointers to the underlying C,\nso you can utilize C-API directly for more advanced usage.\n\nAs the library is still evolving, there can be incompatible changes between updates, \nthe releases before version 1.0.0 doesn't follow [Semantic Versioning](https://semver.org/).\nPlease use the exact version if you do not want to worry about updating your packages.\n\n```swift\n.package(url: \"https://github.com/kongzii/SwiftXGBoost.git\", .exact(\"0.1.0\")),\n```\n\n## Python compatibility\n\nDMatrix can be created from numpy array just like in Python\n\n```swift\nlet pandas = Python.import(\"pandas\")\nlet dataFrame = pandas.read_csv(\"data.csv\")\nlet data = try DMatrix(\n    name: \"training\",\n    from: dataFrame.values\n)\n```\n\nand the swift array can be converted back to numpy\n\n```swift\nlet predicted = try booster.predict(\n    from: validationData\n)\n\nlet compare = pandas.DataFrame([\n    \"Label lower bound\": yLowerBound[validIndex],\n    \"Label upper bound\": yUpperBound[validIndex],\n    \"Prediced\": predicted.makeNumpyArray(),\n])\n\nprint(compare)\n```\n\nThis is possible thanks to the [PythonKit](https://github.com/pvieito/PythonKit.git). \nFor more detailed usage and workarounds for known issues, check out [examples](https://github.com/kongzii/SwiftXGBoost/tree/master/Examples).\n\n## TensorFlow compability\n\n[Swift4TensorFlow](https://github.com/tensorflow/swift) is a great project from Google. \nIf you are using one of the S4TF swift toolchains, you can combine its power directly with XGBoost.\n\n```swift\nlet tensor = Tensor\u003cFloat\u003e(shape: TensorShape([2, 3]), scalars: [1, 2, 3, 4, 5, 6])\nlet data = try DMatrix(name: \"training\", from: tensor)\n```\n\n### Note\n\n[Swift4TensorFlow](https://github.com/tensorflow/swift) toolchains ships with preinstalled [PythonKit](https://github.com/pvieito/PythonKit.git) and you may run into a problem when using package with extra [PythonKit](https://github.com/pvieito/PythonKit.git) dependency. If so, please just add package version with `-tensorflow` suffix, where [PythonKit](https://github.com/pvieito/PythonKit.git) dependency is removed.\n\n```swift\n.package(url: \"https://github.com/kongzii/SwiftXGBoost.git\", .exact(\"0.7.0-tensorflow\")),\n```\n\nThis bug is known and hopefully will be resolved soon.\n\n## Examples\n\nMore examples can be found in [Examples directory](https://github.com/kongzii/SwiftXGBoost/tree/master/Examples) \nand run inside docker\n\n```\ndocker-compose run swiftxgboost swift run exampleName\n```\n\nor on host\n\n```\nswift run exampleName\n```\n\n### Basic functionality\n\n```swift\nimport XGBoost\n\n// Register your own callback function for log(info) messages\ntry XGBoost.registerLogCallback {\n    print(\"Swifty log:\", String(cString: $0!))\n}\n\n// Create some random features and labels\nlet randomArray = (0 ..\u003c 1000).map { _ in Float.random(in: 0 ..\u003c 2) }\nlet labels = (0 ..\u003c 100).map { _ in Float([0, 1].randomElement()!) }\n\n// Initialize data, DMatrixHandle in the background\nlet data = try DMatrix(\n    name: \"data\",\n    from: randomArray,\n    shape: Shape(100, 10),\n    label: labels,\n    threads: 1\n)\n\n// Slice array into train and test\nlet train = try data.slice(indexes: 0 ..\u003c 90, newName: \"train\")\nlet test = try data.slice(indexes: 90 ..\u003c 100, newName: \"test\")\n\n// Parameters for Booster, check https://xgboost.readthedocs.io/en/latest/parameter.html\nlet parameters = [\n    Parameter(\"verbosity\", \"2\"),\n    Parameter(\"seed\", \"0\"),\n]\n\n// Create Booster model, `with` data will be cached\nlet booster = try Booster(\n    with: [train, test],\n    parameters: parameters\n)\n\n// Train booster, optionally provide callback functions called before and after each iteration\ntry booster.train(\n    iterations: 10,\n    trainingData: train,\n    evaluationData: [train, test]\n)\n\n// Predict from test data\nlet predictions = try booster.predict(from: test)\n\n// Save\ntry booster.save(to: \"model.xgboost\")\n```\n\n## Development\n\n### Documentation\n\n[Jazzy](https://github.com/realm/jazzy) is used for the generation of documentation.\n\nYou can generate documentation locally using \n\n```\nmake documentation\n```\n\nGithub pages will be updated automatically when merged into master.\n\n### Tests\n\nWhere possible, Swift implementation is tested against reference implementation in Python via PythonKit. For example, test of `score` method in `scoreEmptyFeatureMapTest`\n\n```swift\nlet pyFMap = [String: Int](pyXgboost.get_score(\n    fmap: \"\", importance_type: \"weight\"))!\nlet (fMap, _) = try booster.score(featureMap: \"\", importance: .weight)\n\nXCTAssertEqual(fMap, pyFMap)\n```\n\n#### Run locally\n\nOn ubuntu using docker\n\n```\ndocker-compose run test \n```\n\nOn host\n\n```\nswift test\n```\n\n### Code format\n\n[SwiftFormat](https://github.com/nicklockwood/SwiftFormat) is used for code formatting. \n\n```\nmake format\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkongzii%2Fswiftxgboost","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkongzii%2Fswiftxgboost","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkongzii%2Fswiftxgboost/lists"}