You know how most programmers find functional coding to be ever so slightly mind bending and how it's somewhat difficult to wrap one's head around working with variables whose state you cannot change and lazy evaluations and all manner of odd things?

The thing I've had most trouble with and still do actually, is coding in a functionally clean manner, using more recursion, cleaner abstractions and so on. Just as I thought I was almost starting to get kind of good at this, a bunch of people proved me wrong when I crowdsourced some elegance.

Yeah, some people are *really* good at this functional stuff.

And then one day ml-class introduced me to mathematical programming with Octave. Sure, I've done some Octave before at school, but that was just enough to get my feet wet - basic syntax and stuff. Or maybe I just paying enough attention to really grasp the awesome things I was being shown.

Either way, I feel as if over the past two weeks, doing machine learning homework in Octave has opened a whole new world of striving for elegance and purity in my code. If I thought functional was mindbending, this stuff is *ripping my face off*.

Apparently when you take a naive loop and make it into something beautiful it's called vectorization in this field. The interesting bit here is that all you really need to perform optimization of epic proportions is some math fu, no translating the problem into something else, no looking at it from five different perspectives ... just maths and then some.

Using the gradient descent algorithm for logistic regression as an example, in particular calculating the cost function:

Although I think this might have been exactly the perfect example for the code below ... it's difficult to search through videos for this stuff.

The naive approach could be something like this (didn't actually run the code):

J = 0;for i = 1:mJ += =y(i)*log(sigmoid(theta*X(i,:))-(1-y(i)*log(1-sigmoid(theta*X(i,:));endJ = J/m;for j = 1:size(theta)grad(j) = 0;for i = 1:mgrad(j) += (sigmoid(theta*X(i,:))-y(i))*X(i,j);endgrad(j) = grad(j)/m;end

But all those lops aren't really necessary, all they are basically doing is matrix multiplication, which gives us a nice way to vectorize the whole thing:

J = (1/m)*sum(-y'*log(sigmoid(X*theta))-(1-y')*log(1-sigmoid(X*theta)));grad = (1/m)*(sigmoid(X*theta)-y)'*X;

The difference in elegance absolutely blows my mind and I can't wait to see what other wonders I discover through this Octave thing in the course of this semester.

Pretty much all my octave can be found in the ml-class-homework repository. But I'm sure I'll end up modeling more algorithms in this thing.

###### Related articles

- 12 steps to running gradient descent in Octave (flowingmotion.jojordan.org)
- Machine Learning Ex4 - Logistic Regression (r-bloggers.com)
- Machine Learning Ex 5.2 - Regularized Logistic Regression (r-bloggers.com)

## Learned something new?

Want to become an expert?

Here's how it works 👇

Leave your email and I'll send you thoughtfully written emails every week about **React**, **JavaScript**, and **your career**. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

### Join Swizec's Newsletter

And get thoughtful letters 💌 on **mindsets, tactics, and technical skills** for your career. Real lessons from building production software. No bullshit.

"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

**Have a burning question that you think I can answer?** I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

**Ready to Stop copy pasting D3 examples and create data visualizations of your own?**
Learn how to build scalable dataviz components your whole team can understand
with React for Data Visualization

**Curious about Serverless and the modern backend?** Check out
Serverless Handbook, modern backend for the frontend engineer.

**Ready to learn how it all fits together and build a modern webapp from scratch?**
Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

**Want to brush up on your modern JavaScript syntax?** Check out my interactive cheatsheet: es6cheatsheet.com

**By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️**