How to build a neural network recommendation system in less than 30 minutes

1462-time2.jpg

Quite often, we find ourselves in the need to build a simple and effective recommendation system, with less than half an hour to do so. Here, we will exemplify how to build a recommendation system with simple predictions on recommendations about which clothes to wear based on the outside temperature.

The input parameter for this recommendation system will be the value of temperature in Fahrenheit. The output parameter is a recommended clothing item from a set of predefined values.

Of course, this example is easy to build yet very valuable if you’d like to extend it for additional input parameters and more complex recommendation algorithms.

The magic of this approach is based on neural networks algorithms. From another point of view, neural algorithms are not simple to create and train, but with the current state of libraries and frameworks available for free, it is now readily accessible to use in production solutions..

Select the right programming language

When creating a neural network, it is crucial to find and select the best tools and languages to get the job done effectively. Selecting the right programming language is crucial, and in this example, the best programming language to start working with neural networks is Python.

Python is a simple and powerful scripting language with outstanding performance. One of the key advantages of Python is its readable, clear, and compact code. Python is available on most computing platforms, such as Platform as a Service (PaaS) cloud services like Heroku and single-board computers like Raspberry Pi.

A decision about which version of Python to use (2.7 or 3.x) is a topic to be discussed separately. For this example, let’s consider installing Python version 3 to support the latest version of libraries.

Tools to install

As previously stated, install Python version 3. Take a look at this nice guide on how to install Python on a Macbook. Additionally, you can search the Internet on how to install Python in your operating system.

Next, you must install Tensorflow as a computation engine for the neural network. Tensorflow is a powerful mathematical framework that supports hardware acceleration such as nVidia CUDA. This helps train bigger neural network systems for complex recommendation systems, as necessary.

sudo python3 -m pip install tensorflow

Next, install the Numpy library to work with numerical data.

pip3 install numpy

Afterward, you must install Keras as the neural network framework. Keras is a top-notch, popular, and free solution.

sudo python3 -m pip install keras

Organize your data

Input data, which can be described as a set of Python values, will include these three types of clothing items for recommendation: parka, jacket, and raincoat.

output_data = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) Code language: PHP (php)

Each of these clothing items will be recommended based on a temperature range, for instance, 40-20 °F, 60-40 °F and 80-60 °F.

temperature = np.array([[40, 20], [60, 40], [80, 60]])Code language: PHP (php)

Create the python script

Now, it’s time to build a simple Python script with Keras, as the one depicted below.

from keras.models import Sequential from keras.layers import Dense import numpy as np from keras.optimizers import SGD # Build dataset input_data = np.array([[40, 20], [60, 40], [80, 60]]) output_data = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) # cloth seed = 7 np.random.seed(seed) model = Sequential() model.add(Dense(8, input_dim=2, activation='sigmoid')) model.add(Dense(256, input_dim=2, activation='sigmoid')) model.add(Dense(3, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model model.fit(input_data, output_data, epochs=1000, batch_size=32, verbose=0) # evaluate the model loss, accuracy = model.evaluate(input_data, output_data) # calculate predictions predictions = model.predict(input_data) print (np.round(predictions)) Subsequently, you must include the predicted output that corresponds to each temperature interval: [[0. 0. 1.] [0. 1. 0.] [1. 0. 0.]] Code language: PHP (php)

The power of neural networks

Simple enough, right? Now, you’ve successfully created a simple neural network as a recommendation system for clothing items, depending on temperature conditions. If you’d like to extend the recommendation system to include more parameters or create a wide range of output results, you can find many online books on the subject, as well as numerous tutorials about Keras, Tensorflow, and Python.

As you deem appropriate, you can use any other programming language such as C++ and C#, as well as any other neural network library, to build your recommendation system.

This simple page of code shows the real power and beauty of neural network programming.