Writing Machine Learning Solutions — First Impressions

Linear regression model created using TensorFlow.

Introduction

You Also Need Other Libraries When Creating ML Models…

Python REPL

>>> 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)
97
>>> 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
(97,)
>>> X_train_bias.shape
(97, 2)
>>> X_train_bias
array([[ 1. , 6.1101],
[ 1. , 5.5277],
[ 1. , 8.5186],

TensorFlow

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()
sess.run(init)
# 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}))

Conclusions

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