luwill/Machine_Learning_Code_Implementation: A Companion Code Repository for Two Chinese ML Textbooks
Mathematical derivation and pure Python code implementation of machine learning algorithms.
At a glance
- What is it?
- This repository holds the code that accompanies a book deriving 26 classical machine learning algorithms, built on the theoretical frameworks of Li Hang's Statistical Learning Methods and Zhou Zhihua's Machine Learning. It is teaching material first and production code second, and the README is explicit about that ordering.
- Who is it for?
- Adopt this if you are working through Li Hang or Zhou Zhihua and want a second pass at the derivations in runnable form, or if you teach an algorithms course and want the PPT material the README points to. Do not adopt it as a dependency: there is no package on any index, no versioned release, and the licence is CC BY-NC-SA 4.0, which bars commercial use.
- Can I use it commercially?
- Not without permission. GitHub finds no licence file in the repository, and without a licence all rights are reserved by default: you may read the code but not reuse it. Check the README, or ask the authors, before using it.
- Is it still maintained?
- Yes. The repository last received commits 140 days ago.
- What is it written in?
- Mainly Jupyter Notebook, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on September 15, 2026, and from our analysis. They are not legal advice.
DEEP OPEN-SOURCE ANALYSIS
What the Repository Actually Contains
The README describes the project as a companion code repository for a printed book, and the book itself as supplementary material to two Chinese-language textbooks: Li Hang's Statistical Learning Methods and Zhou Zhihua's Machine Learning, known in Chinese as the watermelon book. The repository organises code by book chapter, and the README states that readers can click into a chapter directory and use that chapter's code directly. The scope is stated as 26 classical algorithms split across four groups: supervised single models, supervised ensemble models, unsupervised models, and probabilistic models. Formula derivation is part of the deliverable, not an afterthought. The primary language listed for the repository is Jupyter Notebook, which fits that framing: derivations and plots sit next to the implementation in the same file. The README says only the code has been open-sourced so far and that the full book content is planned for the repository later, so at present you get implementations and derivations without the surrounding prose.
Why Derivations and Code Sit in the Same Notebook
Most algorithm repositories hand you a function signature and a docstring. This one is built the other way around: the derivation comes first, and the code is meant to be read as the derivation's execution. That ordering is the whole point of the project, and it explains the notebook-first layout. It also explains why the README treats the printed book as the primary artefact and the repository as the thing that stays current. The README says plainly that compared with the code in the book, the repository code is kept updated and iterated on. That sentence is the repository's real value proposition. A printed derivation is frozen; a notebook directory can be corrected when a bug is found. The 2026.04 revision notes are the evidence that this happens in practice rather than as a promise.
The mlbook Shared Library and the 2026.04 Restructuring
The README's code revision section for 2026.04 documents two rounds of review and optimisation. The structural change matters most for anyone reading the tree: a shared library named mlbook/ was created to remove duplicated utils.py and cart.py files from chapters 7, 11, 12 and 15. Six redundant files became two shared ones. Infrastructure files were added at the same time: requirements.txt, .gitignore and pyproject.toml. A tests/ directory was added with what the README describes as 25 test cases covering the shared library and key algorithms. That is the shape of a repository that started as loose chapter scripts and is being pulled toward a single installable tree. It is not there yet. The chapter directories still hold the algorithms, and mlbook/ holds only the pieces that were duplicated across chapters. If you want to read the decision tree material, you will be moving between a chapter notebook and a shared module, and the README does not give a map for that.
Concrete Bug Fixes Worth Knowing About Before You Read
The revision notes list specific corrections, and several of them are the kind that would silently mislead a reader who trusted the earlier version. In chapter 5, the LDA implementation had an error in calc_cov where incorrect standardisation produced a wrong within-class scatter matrix; the README states accuracy moved from 0.85 to 1.0 after the fix. In chapter 25, the Gibbs sampling logic in the MCMC chapter was passing y=-1 into p_xy instead of the state transition value. In chapter 23, the forward algorithm and the Viterbi algorithm had a hardcoded state count of N=4, which would break on any sequence with a different number of hidden states. In chapter 3, the accuracy function used an O(n squared) loop and the cross-entropy calculation needed log clipping to avoid NaN. A missing initialize_with_zeros method was added to perceptron.py. These are not stylistic notes. A hardcoded N=4 in Viterbi is the sort of thing you copy into your own code and only discover when your state space changes.
API Modernisation and What It Tells You About Age
The same revision section lists fixes for deprecated APIs: sklearn.datasets.samples_generator replaced with sklearn.datasets.make_blobs, np.float replaced with np.float64, np.matrix replaced with standard arrays, and matplotlib's normed=1 replaced with density=True. A max_iter=200 was added to the chapter 1 LogisticRegression to suppress a convergence warning, and a hardcoded Windows path in the chapter 19 SVD code was replaced with os.path.join. Every one of those items dates the original code to a period when those APIs were current, and every one is the kind of breakage that makes a tutorial repository unusable a few years after publication. The fact that they are being fixed is a point in the project's favour. The fact that they needed fixing across so many chapters is a fair warning about how much of the rest of the tree has been exercised on a modern interpreter. The README does not claim a full audit, only the changes listed.
Getting It Running
The README does not give an install command, a clone command or a Python version. What it does give is the infrastructure the 2026.04 revision added: a requirements.txt at the repository root, a pyproject.toml, and a tests/ directory with 25 test cases. The practical path is to clone the repository, install from requirements.txt, and run the tests before opening any chapter notebook, since the tests are the only executable statement of what currently works. There is no package published under the mlbook name as far as the material shows, so expect to work inside the checkout rather than pip installing it. The README also points to a companion PPT deck, obtained by contacting the author through a WeChat account called the machine learning laboratory, available to readers who bought the printed book. One chapter of video is linked, chapter 1 on Bilibili, with the rest described as in progress.
Licence and the Commercial-Use Boundary
The README states the project uses the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International licence, linking to the CC BY-NC-SA 4.0 deed. Two consequences follow directly from the licence name, though this is not legal advice and you should read the deed yourself. NonCommercial means you cannot use the code in a commercial product or a paid internal tool without a separate arrangement with the author. ShareAlike means derivative works must carry the same licence, which is unusual for code and is a real constraint if you plan to adapt a chapter implementation into something you distribute. The repository carries no separate software licence file that the README mentions, so the CC licence is the only stated terms. For a textbook companion this is a coherent choice. For anyone who found a chapter implementation and wanted to drop it into a product, it is a stop sign.
Where This Is the Wrong Tool, and What to Read Instead
If you need a working classifier today, this is the wrong repository. scikit-learn gives you the same algorithms with a stable API, a test suite, and a permissive licence, and the README's own revision notes show this project chasing scikit-learn's API changes rather than setting them. The difference in approach is the point: scikit-learn hides the estimator's internals behind fit and predict, while this repository exposes the update rule, the gradient, and the derivation that justifies it. That makes it a poor library and a good study companion. The same logic rules out using it for anything where correctness is load-bearing without reading the code first. The README's own bug list is the argument: a hardcoded N=4 and a wrong covariance standardisation both survived long enough to reach a revision pass. The repository is also Chinese-language throughout, including the README and the book it accompanies, so an English-only reader will get the code and the comments but not the surrounding explanation.
Who Should Adopt It and What to Check First
The natural reader is someone working through Li Hang or Zhou Zhihua who wants the derivations in executable form, or an instructor who wants chapter-aligned notebooks and the PPT material the README describes. The natural non-reader is anyone who wants a dependency, and anyone whose project is commercial, given the NonCommercial term. Before trusting a chapter, check three things in the repository itself: the Errata file at Errata/Errata.md, which the README links and which records known errors in the book text; the tests/ directory, which the README says covers the shared library and key algorithms with 25 cases; and the commit history, which the README cites as the record of the two review passes. If a chapter you care about is not covered by those tests and not mentioned in the revision notes, treat its implementation as unverified and read the derivation against the code line by line.
Editorial conclusion
Adopt this if you are working through Li Hang or Zhou Zhihua and want a second pass at the derivations in runnable form, or if you teach an algorithms course and want the PPT material the README points to. Do not adopt it as a dependency: there is no package on any index, no versioned release, and the licence is CC BY-NC-SA 4.0, which bars commercial use. Before relying on any chapter, read the Errata file at Errata/Errata.md and the tests/ directory, since the README states the code has been corrected twice and the book text has not kept pace.
Community notes