Writing Machine Learning Solutions — First Impressions

Image for post
Image for post
Linear regression model created using TensorFlow.


As I described in my previous blog article Studying Machine Learning — First Impressions I decided to dedicate this winter’s studies in Machine learning. I have been doing the excellent Machine learning Coursera course and working with the exercises using Octave. This was basically the first step (theory) in my Machine learning study path.

You Also Need Other Libraries When Creating ML Models…

When I was doing the first exercise Linear regression in TensorFlow I soon realized that you have to learn two other Python libraries as well:

  • Matplotlib (Python plotting library for creating graphics). It is important to visualize the data you are working with, and when working in Python Matplotlib is your friend. The graphic in the beginning of this blog article was generated using Matplotlib.

Python REPL

Let’s add a short Python REPL session example to visualize how you can use Python REPL to learn a new Python library and experiment with it before writing the actual code in your Python editor (by the way, my choice of Python IDE is excellent PyCharm).

>>> import src.ml_course_ex1a as ex1a
>>> model = ex1a.ProfitPopulationLinearRegression("ml_course_ex1a.ini", True)
>>> (populations,profits) = model.readCsvFile("data/ex1a-profit-population.csv")
>>> len(populations)
>>> import numpy as np
>>> populations_array = np.asarray(populations)
>>> populations_array
array([ 6.1101, 5.5277, 8.5186, 7.0032, 5.8598, ...
>>> X_train = np.asarray(populations)
>>> X_train_bias = model.appendBias(X_train)
>>> X_train.shape
>>> X_train_bias.shape
(97, 2)
>>> X_train_bias
array([[ 1. , 6.1101],
[ 1. , 5.5277],
[ 1. , 8.5186],


The real reason for this blog article was TensorFlow so let’s focus on that. The code snippet below shows a typical TensorFlow usage. In TensorFlow you have tensors (matrices of various order) and operations which let tensors to flow from one step to another, hence name “TensorFlow”. Basically tensors and operations provide a higher level abstraction for using matrices and also auxiliary functions for various Machine learning algorithms.

X = tf.placeholder(tf.float32, [None, n])
y = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.ones([n,1]), name="weights")
init = tf.global_variables_initializer()
y_prediction = tf.matmul(X, W) # As matrix multiplication in ex1.
# Cost
J = (1 / (2 * m)) * tf.reduce_sum(tf.pow(y_prediction - y, 2))
step = tf.train.GradientDescentOptimizer(alpha).minimize(J)
sess = tf.Session()
# For recording cost history.
J_history = np.empty(shape=[1],dtype=float)
# Train iterations.
for i in range(iterations):
sess.run(step,feed_dict={X:X_train_normalized_bias, y:y_train})
J_history = np.append(J_history,sess.run(J,feed_dict={X:X_train_normalized_bias,y:y_train}))


I’m pretty sure this combination is going to be a killer knowledge for a future developer:

  • Experience to implement transaction intensive and parallel backend applications.
  • Understanding and experience to create Big data systems.
  • Knowledge to create Cloud infrastructure for your systems.
  • Analytics and data science.
  • Machine learning and Artificial intelligence.

Written by

I’m a Software architect and developer. Currently implementing systems on AWS / GCP / Azure / Docker / Kubernetes using Java, Python, Go and Clojure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store