{"id":19179232,"url":"https://github.com/melling/data-science-from-scratch-swift","last_synced_at":"2025-04-09T22:17:34.532Z","repository":{"id":136798971,"uuid":"236410921","full_name":"melling/data-science-from-scratch-swift","owner":"melling","description":"Data Science from Scratch Implemented in Swift","archived":false,"fork":false,"pushed_at":"2020-07-24T06:41:04.000Z","size":130,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-09T22:17:28.306Z","etag":null,"topics":["data-science","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/melling.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-27T03:03:03.000Z","updated_at":"2025-04-01T01:43:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"e81031ab-6983-433d-bfab-8b4fa84a803a","html_url":"https://github.com/melling/data-science-from-scratch-swift","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/melling%2Fdata-science-from-scratch-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2Fdata-science-from-scratch-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2Fdata-science-from-scratch-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melling%2Fdata-science-from-scratch-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/melling","download_url":"https://codeload.github.com/melling/data-science-from-scratch-swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119286,"owners_count":21050755,"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":["data-science","swift"],"created_at":"2024-11-09T10:42:30.271Z","updated_at":"2025-04-09T22:17:34.505Z","avatar_url":"https://github.com/melling.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Science from Scratch Implemented in Swift\n\nData Science from Scratch\u003cbr/\u003e\nFirst Principles with ~~Python~~ **Swift**\u003cbr/\u003e\nJoel Grus\n\nBook: https://www.amazon.com/Data-Science-Scratch-Principles-Python/dp/1492041130/\n\nBook Python Source: https://github.com/joelgrus/data-science-from-scratch\n\n--\n\n- [Chapter 4: Linear Algebra](swift/linear_algebra.swift) \n- [Chapter 5: Statistics](swift/statistics.swift)\n- [Chapter 6: Probability](swift/probability.swift)\n- [Chapter 7: Hypothesis and Inference](swift/inference.swift)\n- [Chapter 8: Gradient Descent](swift/gradient_descent.swift)\n- [Chapter 14: Simple Linear Regression](swift/simple_linear_regression.swift)\n- \n## TODO\n- [Chapter 12: k-Nearest Neighbors](swift/k_nearest_neighbors.swift)\n- [Chapter 13: Naive Bayes](swift/naive_bayes.swift)\n- [Chapter 15: Multiple Regression](swift/multiple_regression.swift)\n- [Chapter 16: Logistic Regression](swift/logistic_regression.swift)\n\n\n## Overview\n\nReimplementing in Swift because it's a great modern language.  Any language will do, of course. However, if I did it with Python, I'd probably cheat a little and I would definitely not think about what I'm doing as much.\n \n ## Misc Thoughts and Issues\n \n - Sticking with snake_case variable names for consistency\n - Implemented some functions as Array extensions in Chapter 5.\n    - Which is better variance(xs) vs xs.variance?\n - Used FloatingPoint protocol so I had to force type to float by adding .0 in some instances.\n - Type issues: Tried to use FloatingPoint protocol to make functions more generic.  Had problems casting Element results to Float.\n - Needed two linear algebra functions from Chapter 4: dot(), sum_of_squares().  I simply copied them into the statistics.swift file.\n - Also, copied the Vector type.\n - Need a plotting library.  \n \n ### Python's List Comprehensions\n \n Pyhon's List Comprehension are a nice succinct feature.  In Swift, you'll need to replace them with a combination of map, filter, forEach, for, etc  I've rewritten some of the examples on page 30 in Swift:\n \n #### Python\n \n ```python\neven_numbers = [x for x in range(5) if x % 2 == 0]  # [0, 2, 4]\nsquares      = [x * x for x in range(5)]            # [0, 1, 4, 9, 16]\neven_squares = [x * x for x in even_numbers]        # [0, 4, 16]\n\n\nassert even_numbers == [0, 2, 4]\nassert squares == [0, 1, 4, 9, 16]\nassert even_squares == [0, 4, 16]\n\nsquare_dict = {x: x * x for x in range(5)}  # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\nsquare_set  = {x * x for x in [1, -1]}      # {1}\n\n\nassert square_dict == {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\nassert square_set == {1}\n\nzeros = [0 for _ in even_numbers]      # has the same length as even_numbers\n\n\nassert zeros == [0, 0, 0]\n\n```\n\n \n#### Swift\n\n```swift\nlet even_numbers = (0..\u003c5).filter {$0 % 2 == 0}\nlet squares = (0..\u003c5).map {$0 * $0}\nlet even_squares = (0..\u003c5).map {$0 * $0}.filter {$0 % 2 == 0}\n\nvar square_dict:[Int:Int] = [:]\n(0..\u003c5).forEach {square_dict[$0] = $0 * $0}\n\nlet zeros = even_numbers.map {_ in 0}\n```\n\n ### Simple implementation of Python's Counter (p26,66) class in Swift.\n \n ```swift\n class Counter\u003cT:Hashable\u003e {\n    public var dict:[T:Int] = [:]\n    public let maxCount:Int\n    \n    init(_ xs:[T]) {\n        var max = 0\n        \n        for elem in xs {\n            if let n = dict[elem] {\n                dict[elem] = n + 1\n                if n + 1 \u003e max {\n                    max = n + 1\n                }\n            } else {\n                dict[elem] = 1\n            }\n        }\n        \n        maxCount = max\n    }\n    \n    public func mode_values() -\u003e [T] {\n        var xs:[T] = []\n        for (k,v) in dict {\n            if v == maxCount {\n                xs.append(k)\n            }\n        }\n        return xs\n    }\n}\n```\n \n \n Next I'll continue to work through the book.  Not sure if I'll do it in order.\n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelling%2Fdata-science-from-scratch-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmelling%2Fdata-science-from-scratch-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelling%2Fdata-science-from-scratch-swift/lists"}