Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

How-To Tutorials

7009 Articles
article-image-implement-rnn-tensorflow-spam-prediction-tutorial
Packt Editorial Staff
11 Aug 2018
11 min read
Save for later

Implementing RNN in TensorFlow for spam prediction [Tutorial]

Packt Editorial Staff
11 Aug 2018
11 min read
Artificial neural networks (ANN) are an abstract representation of the human nervous system, which contains a collection of neurons that communicate with each other through connections called axons. A recurrent neural network (RNN) is a class of ANN where connections between units form a directed cycle. RNNs make use of information from the past. That way, they can make predictions in data with high temporal dependencies. This creates an internal state of the network, which allows it to exhibit dynamic temporal behavior. In this article we will look at: Implementation of basic RNNs in TensorFlow. An example of how to implement an RNN in TensorFlow for spam predictions. Train a model that will learn to distinguish between spam and non-spam emails using the text of the email. This article is an extract taken from the book Deep Learning with TensorFlow – Second Edition, written by Giancarlo Zaccone, Md. Rezaul Karim. Implementing basic RNNs in TensorFlow TensorFlow has tf.contrib.rnn.BasicRNNCell and tf.nn.rnn_cell. BasicRNNCell, which provide the basic building blocks of RNNs. However, first let's implement a very simple RNN model, without using either of these. The idea is to have a better understanding of what goes on under the hood. We will create an RNN composed of a layer of five recurrent neurons using the ReLU activation function. We will assume that the RNN runs over only two-time steps, taking input vectors of size 3 at each time step. The following code builds this RNN, unrolled through two-time steps: n_inputs = 3 n_neurons = 5 X1 = tf.placeholder(tf.float32, [None, n_inputs]) X2 = tf.placeholder(tf.float32, [None, n_inputs]) Wx = tf.get_variable("Wx", shape=[n_inputs,n_neurons], dtype=tf. float32, initializer=None, regularizer=None, trainable=True, collections=None) Wy = tf.get_variable("Wy", shape=[n_neurons,n_neurons], dtype=tf. float32, initializer=None, regularizer=None, trainable=True, collections=None) b = tf.get_variable("b", shape=[1,n_neurons], dtype=tf.float32, initializer=None, regularizer=None, trainable=True, collections=None) Y1 = tf.nn.relu(tf.matmul(X1, Wx) + b) Y2 = tf.nn.relu(tf.matmul(Y1, Wy) + tf.matmul(X2, Wx) + b) Then we initialize the global variables as follows: init_op = tf.global_variables_initializer() This network looks much like a two-layer feedforward neural network, but both layers share the same weights and bias vectors. Additionally, we feed inputs at each layer and receive outputs from each layer. X1_batch = np.array([[0, 2, 3], [2, 8, 9], [5, 3, 8], [3, 2, 9]]) # t = 0 X2_batch = np.array([[5, 6, 8], [1, 0, 0], [8, 2, 0], [2, 3, 6]]) # t = 1 These mini-batches contain four instances, each with an input sequence composed of exactly two inputs. At the end, Y1_val and Y2_val contain the outputs of the network at both time steps for all neurons and all instances in the mini-batch. Then we create a TensorFlow session and execute the computational graph as follows: with tf.Session() as sess:        init_op.run()        Y1_val, Y2_val = sess.run([Y1, Y2], feed_dict={X1:        X1_batch, X2: X2_batch}) Finally, we print the result: print(Y1_val) # output at t = 0 print(Y2_val) # output at t = 1 The following is the output: >>> [[ 0. 0. 0. 2.56200171 1.20286 ] [ 0. 0. 0. 12.39334488 2.7824254 ] [ 0. 0. 0. 13.58520699 5.16213894] [ 0. 0. 0. 9.95982838 6.20652485]] [[ 0. 0. 0. 14.86255169 6.98305273] [ 0. 0. 26.35326385 0.66462421 18.31009483] [ 5.12617588 4.76199865 20.55905533 11.71787453 18.92538261] [ 0. 0. 19.75175095 3.38827515 15.98449326]] The network we created is simple, but if you run it over 100 time steps, for example, the graph is going to be very big. Implementing an RNN for spam prediction In this section, we will see how to implement an RNN in TensorFlow to predict spam/ham from texts. Data description and preprocessing The popular spam dataset from the UCI ML repository will be used, which can be downloaded from http://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip. The dataset contains texts from several emails, some of which were marked as spam. Here we will train a model that will learn to distinguish between spam and non-spam emails using only the text of the email. Let's get started by importing the required libraries and model: import os import re import io import requests import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from zipfile import ZipFile from tensorflow.python.framework import ops import warnings Additionally, we can stop printing the warning produced by TensorFlow if you want: warnings.filterwarnings("ignore") os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' ops.reset_default_graph() Now, let's create the TensorFlow session for the graph: sess = tf.Session() The next task is setting the RNN parameters: epochs = 300 batch_size = 250 max_sequence_length = 25 rnn_size = 10 embedding_size = 50 min_word_frequency = 10 learning_rate = 0.0001 dropout_keep_prob = tf.placeholder(tf.float32) Let's manually download the dataset and store it in a text_data.txt file in the temp directory. First, we set the path: data_dir = 'temp' data_file = 'text_data.txt' if not os.path.exists(data_dir):    os.makedirs(data_dir) Now, we directly download the dataset in zipped format: if not os.path.isfile(os.path.join(data_dir, data_file)):    zip_url = 'http://archive.ics.uci.edu/ml/machine-learning- databases/00228/smsspamcollection.zip'    r = requests.get(zip_url)    z = ZipFile(io.BytesIO(r.content))    file = z.read('SMSSpamCollection') We still need to format the data: text_data = file.decode()    text_data = text_data.encode('ascii',errors='ignore')    text_data = text_data.decode().split('\n') Now, store in it the directory mentioned earlier in a text file: with open(os.path.join(data_dir, data_file), 'w') as file_conn:        for text in text_data:            file_conn.write("{}\n".format(text)) else:    text_data = []    with open(os.path.join(data_dir, data_file), 'r') as file_conn:        for row in file_conn:            text_data.append(row)    text_data = text_data[:-1] Let's split the words that have a word length of at least 2: text_data = [x.split('\t') for x in text_data if len(x)>=1] [text_data_target, text_data_train] = [list(x) for x in zip(*text_data)] Now we create a text cleaning function: def clean_text(text_string):    text_string = re.sub(r'([^\s\w]|_|[0-9])+', '', text_string)    text_string = " ".join(text_string.split())    text_string = text_string.lower()    return(text_string) We call the preceding method to clean the text: text_data_train = [clean_text(x) for x in text_data_train] Now we need to do one of the most important tasks, which is creating word embedding –changing text into numeric vectors: vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(max_sequence_length, min_frequency=min_word_frequency) text_processed = np.array(list(vocab_processor.fit_transform(text_data_train))) Now let's shuffle to make the dataset balance: text_processed = np.array(text_processed) text_data_target = np.array([1 if x=='ham' else 0 for x in text_data_target]) shuffled_ix = np.random.permutation(np.arange(len(text_data_target))) x_shuffled = text_processed[shuffled_ix] y_shuffled = text_data_target[shuffled_ix] Now that we have shuffled the data, we can split the data into a training and testing set: ix_cutoff = int(len(y_shuffled)*0.75) x_train, x_test = x_shuffled[:ix_cutoff], x_shuffled[ix_cutoff:] y_train, y_test = y_shuffled[:ix_cutoff], y_shuffled[ix_cutoff:] vocab_size = len(vocab_processor.vocabulary_) print("Vocabulary size: {:d}".format(vocab_size)) print("Training set size: {:d}".format(len(y_train))) print("Test set size: {:d}".format(len(y_test))) Following is the output of the preceding code: >>> Vocabulary size: 933 Training set size: 4180 Test set size: 1394 Before we start training, let's create placeholders for our TensorFlow graph: x_data = tf.placeholder(tf.int32, [None, max_sequence_length]) y_output = tf.placeholder(tf.int32, [None]) Let's create the embedding: embedding_mat = tf.get_variable("embedding_mat", shape=[vocab_size, embedding_size], dtype=tf.float32, initializer=None, regularizer=None, trainable=True, collections=None) embedding_output = tf.nn.embedding_lookup(embedding_mat, x_data) Now it's time to construct our RNN. The following code defines the RNN cell: cell = tf.nn.rnn_cell.BasicRNNCell(num_units = rnn_size) output, state = tf.nn.dynamic_rnn(cell, embedding_output, dtype=tf.float32) output = tf.nn.dropout(output, dropout_keep_prob) Now let's define the way to get the output from our RNN sequence: output = tf.transpose(output, [1, 0, 2]) last = tf.gather(output, int(output.get_shape()[0]) - 1) Next, we define the weights and the biases for the RNN: weight = bias = tf.get_variable("weight", shape=[rnn_size, 2], dtype=tf.float32, initializer=None, regularizer=None, trainable=True, collections=None) bias = tf.get_variable("bias", shape=[2], dtype=tf.float32, initializer=None, regularizer=None, trainable=True, collections=None) The logits output is then defined. It uses both the weight and the bias from the preceding code: logits_out = tf.nn.softmax(tf.matmul(last, weight) + bias) Now we define the losses for each prediction so that later on, they can contribute to the loss function: losses = tf.nn.sparse_softmax_cross_entropy_with_logits_v2(logits=logits_ou t, labels=y_output) We then define the loss function: loss = tf.reduce_mean(losses) We now define the accuracy of each prediction: accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits_out, 1), tf.cast(y_output, tf.int64)), tf.float32)) We then create the training_op with RMSPropOptimizer: optimizer = tf.train.RMSPropOptimizer(learning_rate) train_step = optimizer.minimize(loss) Now let's initialize all the variables using the global_variables_initializer() method: init_op = tf.global_variables_initializer() sess.run(init_op) Additionally, we can create some empty lists to keep track of the training loss, testing loss, training accuracy, and the testing accuracy in each epoch: train_loss = [] test_loss = [] train_accuracy = [] test_accuracy = [] Now we are ready to perform the training, so let's get started. The workflow of the training goes as follows: Shuffle the training data Select the training set and calculate generations Run training step for each batch Run loss and accuracy of training Run the evaluation steps. The following codes include all of the aforementioned steps: shuffled_ix = np.random.permutation(np.arange(len(x_train)))    x_train = x_train[shuffled_ix]    y_train = y_train[shuffled_ix]    num_batches = int(len(x_train)/batch_size) + 1    for i in range(num_batches):        min_ix = i * batch_size        max_ix = np.min([len(x_train), ((i+1) * batch_size)])        x_train_batch = x_train[min_ix:max_ix]        y_train_batch = y_train[min_ix:max_ix]        train_dict = {x_data: x_train_batch, y_output: \ y_train_batch, dropout_keep_prob:0.5}        sess.run(train_step, feed_dict=train_dict)        temp_train_loss, temp_train_acc = sess.run([loss,\                         accuracy], feed_dict=train_dict)    train_loss.append(temp_train_loss)    train_accuracy.append(temp_train_acc)    test_dict = {x_data: x_test, y_output: y_test, \ dropout_keep_prob:1.0}    temp_test_loss, temp_test_acc = sess.run([loss, accuracy], \                    feed_dict=test_dict)    test_loss.append(temp_test_loss)    test_accuracy.append(temp_test_acc)    print('Epoch: {}, Test Loss: {:.2}, Test Acc: {:.2}'.format(epoch+1, temp_test_loss, temp_test_acc)) print('\nOverall accuracy on test set (%): {}'.format(np.mean(temp_test_acc)*100.0)) Following is the output of the preceding code: >>> Epoch: 1, Test Loss: 0.68, Test Acc: 0.82 Epoch: 2, Test Loss: 0.68, Test Acc: 0.82 Epoch: 3, Test Loss: 0.67, Test Acc: 0.82 … Epoch: 997, Test Loss: 0.36, Test Acc: 0.96 Epoch: 998, Test Loss: 0.36, Test Acc: 0.96 Epoch: 999, Test Loss: 0.35, Test Acc: 0.96 Epoch: 1000, Test Loss: 0.35, Test Acc: 0.96 Overall accuracy on test set (%): 96.19799256324768 Well done! The accuracy of the RNN is above 96%, which is outstanding. Now let's observe how the loss propagates across each iteration and over time: epoch_seq = np.arange(1, epochs+1) plt.plot(epoch_seq, train_loss, 'k--', label='Train Set') plt.plot(epoch_seq, test_loss, 'r-', label='Test Set') plt.title('RNN training/test loss') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend(loc='upper left') plt.show() Figure 1: a) RNN training and test loss per epoch b) test accuracy per epoch We also plot the accuracy over time: plt.plot(epoch_seq, train_accuracy, 'k--', label='Train Set') plt.plot(epoch_seq, test_accuracy, 'r-', label='Test Set') plt.title('Test accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.legend(loc='upper left') plt.show() We discussed the implementation of RNNs in TensorFlow. We saw how to make predictions with data that has a high temporal dependency and how to develop real-life predictive models that make the predictive analytics easier using RNNs. If you want to delve into neural networks and implement deep learning algorithms check out this book, Deep learning with TensorFlow - Second Edition. Top 5 Deep Learning Architectures Understanding Sentiment Analysis and other key NLP concepts Facelifting NLP with Deep Learning
Read more
  • 0
  • 0
  • 6196

article-image-four-ibm-facial-recognition-patents-in-2018-we-found-intriguing
Natasha Mathur
11 Aug 2018
10 min read
Save for later

Four IBM facial recognition patents in 2018, we found intriguing

Natasha Mathur
11 Aug 2018
10 min read
The media has gone into a frenzy over Google’s latest facial recognition patent that shows an algorithm can track you across social media and gather your personal details. We thought, we’d dive further into what other patents Google has applied for in facial recognition tehnology in 2018. What we discovered was an eye opener (pun intended). Google is only the 3rd largest applicant with IBM and Samsung leading the patents race in facial recognition. As of 10th Aug, 2018, 1292 patents have been granted in 2018 on Facial recognition. Of those, IBM received 53. Here is the summary comparison of leading companies in facial recognition patents in 2018. Read Also: Top four Amazon patents in 2018 that use machine learning, AR, and robotics IBM has always been at the forefront of innovation. Let’s go back about a quarter of a century, when IBM invented its first general-purpose computer for business. It built complex software programs that helped in launching Apollo missions, putting the first man on the moon. It’s chess playing computer, Deep Blue, back in 1997,  beat Garry Kasparov, in a traditional chess match (the first time a computer beat a world champion). Its researchers are known for winning Nobel Prizes. Coming back to 2018, IBM unveiled the world’s fastest supercomputer with AI capabilities, and beat the Wall Street expectations by making $20 billion in revenue in Q3 2018 last month, with market capitalization worth $132.14 billion as of August 9, 2018. Its patents are a major part of why it continues to be valuable highly. IBM continues to come up with cutting-edge innovations and to protect these proprietary inventions, it applies for patent grants. United States is the largest consumer market in the world, so patenting the technologies that the companies come out with is a standard way to attain competitive advantage. As per the United States Patent and Trademark Office (USPTO), Patent is an exclusive right to invention and “the right to exclude others from making, using, offering for sale, or selling the invention in the United States or “importing” the invention into the United States”. As always, IBM has applied for patents for a wide spectrum of technologies this year from Artificial Intelligence, Cloud, Blockchain, Cybersecurity, to Quantum Computing. Today we focus on IBM’s patents in facial recognition field in 2018. Four IBM facial recognition innovations patented in 2018 Facial recognition is a technology which identifies and verifies a person from a digital image or a video frame from a video source and IBM seems quite invested in it. Controlling privacy in a face recognition application Date of patent: January 2, 2018 Filed: December 15, 2015 Features: IBM has patented for a face-recognition application titled “Controlling privacy in a face recognition application”. Face recognition technologies can be used on mobile phones and wearable devices which may hamper the user privacy. This happens when a "sensor" mobile user identifies a "target" mobile user without his or her consent. The present mobile device manufacturers don’t provide the privacy mechanisms for addressing this issue. This is the major reason why IBM has patented this technology. Editor’s Note: This looks like an answer to the concerns raised over Google’s recent social media profiling facial recognition patent.   How it works? Controlling privacy in a face recognition application It consists of a privacy control system, which is implemented using a cloud computing node. The system uses a camera to find out information about the people, by using a face recognition service deployed in the cloud. As per the patent application “the face recognition service may have access to a face database, privacy database, and a profile database”. Controlling privacy in a face recognition application The facial database consists of one or more facial signatures of one or more users. The privacy database includes privacy preferences of target users. Privacy preferences will be provided by the target user and stored in the privacy database.The profile database contains information about the target user such as name, age, gender, and location. It works by receiving an input which includes a face recognition query and a digital image of a face. The privacy control system then detects a facial signature from the digital image. The target user associated with the facial signature is identified, and profile of the target user is extracted. It then checks the privacy preferences of the user. If there are no privacy preferences set, then it transmits the profile to the sensor user. But, if there are privacy preferences then the censored profile of the user is generated omitting out the private elements in the profile. There are no announcements, as for now, regarding when this technology will hit the market. Evaluating an impact of a user's content utilized in a social network Date of patent: January 30, 2018 Filed: April 11, 2015 Features:  IBM has patented for an application titled “Evaluating an impact of a user's content utilized in a social network”.  With so much data floating around on social network websites, it is quite common for the content of a document (e.g., e-mail message, a post, a word processing document, a presentation) to be reused, without the knowledge of an original author. Evaluating an impact of a user's content utilised in a social network Evaluating an impact of a user's content utilized in a social network Because of this, the original author of the content may not receive any credit, which creates less motivation for the users to post their original content in a social network. This is why IBM has decided to patent for this application. Evaluating an impact of a user's content utilized in a social network As per the patent application, the method/system/product  “comprises detecting content in a document posted on a social network environment being reused by a second user. The method further comprises identifying an author of the content. The method additionally comprises incrementing a first counter keeping track of a number of times the content has been adopted in derivative works”. There’s a processor, which generates an “impact score” which  represents the author's ability to influence other users to adopt the content. This is based on the number of times the content has been adopted in the derivative works. Also, “the method comprises providing social credit to the author of the content using the impact score”. Editor’s Note: This is particularly interesting to us as IBM, unlike other tech giants, doesn’t own a popular social network or media product. (Google has Google+, Microsoft has LinkedIn, Facebook and Twitter are social, even Amazon has stakes in a media entity in the form of Washington Post). No information is present about when or if this system will be used among social network sites. Spoof detection for facial recognition Date of patent: February 20, 2018 Filed: December 10, 2015 Features: IBM patented an application named “Spoof detection for facial recognition”.  It provides a method to determine whether the image is authentic or not. As per the patent “A facial recognition system is a computer application for automatically identifying or verifying a person from a digital image or a video frame from a video source.” Editor’s Note: This seems to have a direct impact on the work around tackling deepFakes, which incidentally is something DARPA is very keen on. Could IBM be vying for a long term contract with the government? How it works? The patent consists of a system that helps detect “if a face in a facial recognition authentication system is a three-dimensional structure based on multiple selected images from the input video”.                                      Spoof detection for facial recognition There are four or more two-dimensional feature points which are located via an image processing device connected to the camera. Here the two-dimensional feature points do not lie on the same two-dimensional plane. The patent reads that “one or more additional images of the user's face can be received with the camera; and, the at least four two-dimensional feature points can be located on each additional image with the image processor. The image processor can identify displacements between the two-dimensional feature points on the additional image and the two-dimensional feature points on the first image for each additional image” Spoof detection for facial recognition There is also a processor connected to the image processing device that helps figure out whether the displacements conform to a three-dimensional surface model. The processor can then determine whether to authenticate the user depending on whether the displacements conform to the three-dimensional surface model. Facial feature location using symmetry line Date of patent: June 5, 2018 Filed: July 20, 2015 Features: IBM patented for an application titled “Facial feature location using symmetry line”. As per the patent, “In many image processing applications, identifying facial features of the subject may be desired. Currently, location of facial features require a search in four dimensions using local templates that match the target features. Such a search tends to be complex and prone to errors because it has to locate both (x, y) coordinates, scale parameter and rotation parameter”. Facial feature location using symmetry line Facial feature location using symmetry line The application consists of a computer-implemented method that obtains an image of the subject’s face. After that it automatically detects a symmetry line of the face in the image, where the symmetry line intersects at least a mouth region of the face. It then automatically locates a facial feature of the face using the symmetry line. There’s also a computerised apparatus with a processor which performs the steps of obtaining an image of a subject’s face and helps locate the facial feature.  Editor’s note: Atleast, this patent makes direct sense to us. IBM is majorly focusing on bring AI to healthcare. A patent like this can find a lot of use in not just diagnostics and patient care, but also in cutting edge areas like robotics enabled surgeries. IBM is continually working on new technologies to provide the world with groundbreaking innovations. Its big investments in facial recognition technology speaks volumes about how IBM is well-versed with its endless possibilities. With the facial recognition technological progress,  come the privacy fears. But, IBM’s facial recognition application patent has got it covered as it lets the users set privacy preferences. This can be a great benchmark for IBM as no many existing applications are currently doing it. The social credit score evaluating app can really help bring the voice back to the users interested in posting content on social media platforms. The spoof detection application will help maintain authenticity by detecting forged images. Lastly, the facial feature detection can act as a great additional feature for image processing applications. IBM has been heavily investing in facial recognition technology. There are no guarantees by IBM as to whether these patents will ever make it to practical applications, but it does say a lot about how IBM thinks about the technology. Four interesting Amazon patents in 2018 that use machine learning, AR, and robotics Facebook patents its news feed filter tool to provide more relevant news to its users Google’s new facial recognition patent uses your social network to identify you!  
Read more
  • 0
  • 0
  • 24018

article-image-time-series-modeling-what-is-it-why-it-matters-how-its-used
Sunith Shetty
10 Aug 2018
11 min read
Save for later

Time series modeling: What is it, Why it matters and How it's used

Sunith Shetty
10 Aug 2018
11 min read
A series can be defined as a number of events, objects, or people of a similar or related kind coming one after another; if we add the dimension of time, we get a time series. A time series can be defined as a series of data points in time order. In this article, we will understand what time series is and why it is one of the essential characteristics for forecasting. This article is an excerpt from a book written by Harish Gulati titled SAS for Finance. The importance of time series What importance, if any, does time series have and how will it be relevant in the future? These are just a couple of fundamental questions that any user should find answers to before delving further into the subject. Let's try to answer this by posing a question. Have you heard the terms big data, artificial intelligence (AI), and machine learning (ML)? These three terms make learning time series analysis relevant. Big data is primarily about a large amount of data that may be analyzed computationally to reveal patterns, trends, and associations, especially relating to human behavior and interaction. AI is a kind of technology that is being developed by data scientists, computational experts, and others to enable processes to become more intelligent, while ML is an enabler that is helping to implement AI. All three of these terms are interlinked with the data they use, and a lot of this data is time series in its nature. This could be either financial transaction data, the behavior pattern of individuals during various parts of the day, or related to life events that we might experience. An effective mechanism that enables us to capture the data, store it, analyze it, and then build algorithms to predict transactions, behavior (and life events, in this instance) will depend on how big data is utilized and how AI and MI are leveraged. A common perception in the industry is that time series data is used for forecasting only. In practice, time series data is used for: Pattern recognition Forecasting Benchmarking Evaluating the influence of a single factor on the time series Quality control For example, a retailer may identify a pattern in clothing sales every time it gets a celebrity endorsement, or an analyst may decide to use car sales volume data from 2012 to 2017 to set a selling benchmark in units. An analyst might also build a model to quantify the effect of Lehman's crash at the height of the 2008 financial crisis in pushing up the price of gold. Variance in the success of treatments across time periods can also be used to highlight a problem, the tracking of which may enable a hospital to take remedial measures. These are just some of the examples that showcase how time series analysis isn't limited to just forecasting. In this chapter, we will review how the financial industry and others use forecasting, discuss what a good and a bad forecast is, and hope to understand the characteristics of time series data and its associated problems. Forecasting across industries Since one of the primary uses of time series data is forecasting, it's wise that we learn about some of its fundamental properties. To understand what the industry means by forecasting and the steps involved, let's visit a common misconception about the financial industry: only lending activities require forecasting. We need forecasting in order to grant personal loans, mortgages, overdrafts, or simply assess someone's eligibility for a credit card, as the industry uses forecasting to assess a borrower's affordability and their willingness to repay the debt. Even deposit products such as savings accounts, fixed-term savings, and bonds are priced based on some forecasts. How we forecast and the rationale for that methodology is different in borrowing or lending cases, however. All of these areas are related to time series, as we inevitably end up using time series data as part of the overall analysis that drives financial decisions. Let's understand the forecasts involved here a bit better. When we are assessing an individual's lending needs and limits, we are forecasting for a single person yet comparing the individual to a pool of good and bad customers who have been offered similar products. We are also assessing the individual's financial circumstances and behavior through industry-available scoring models or by assessing their past behavior, with the financial provider assessing the lending criteria. In the case of deposit products, as long as the customer is eligible to transact (can open an account and has passed know your customer (KYC), anti-money laundering (AML), and other checks), financial institutions don't perform forecasting at an individual level. However, the behavior of a particular customer is primarily driven by the interest rate offered by the financial institution. The interest rate, in turn, is driven by the forecasts the financial institution has done to assess its overall treasury position. The treasury is the department that manages the central bank's money and has the responsibility of ensuring that all departments are funded, which is generated through lending and attracting deposits at a lower rate than a bank lends. The treasury forecasts its requirements for lending and deposits, while various teams within the treasury adhere to those limits. Therefore, a pricing manager for a deposit product will price the product in such a way that the product will attract enough deposits to meet the forecasted targets shared by the treasury; the pricing manager also has to ensure that those targets aren't overshot by a significant margin, as the treasury only expects to manage a forecasted target. In both lending and deposit decisions, financial institutions do tend to use forecasting. A lot of these forecasts are interlinked, as we saw in the example of the treasury's expectations and the subsequent pricing decision for a deposit product. To decide on its future lending and borrowing positions, the treasury must have used time series data to determine what the potential business appetite for lending and borrowing in the market is and would have assessed that with the current cash flow situation within the relevant teams and institutions. Characteristics of time series data Any time series analysis has to take into account the following factors: Seasonality Trend Outliers and rare events Disruptions and step changes Seasonality Seasonality is a phenomenon that occurs each calendar year. The same behavior can be observed each year. A good forecasting model will be able to incorporate the effect of seasonality in its forecasts. Christmas is a great example of seasonality, where retailers have come to expect higher sales over the festive period. Seasonality can extend into months but is usually only observed over days or weeks. When looking at time series where the periodicity is hours, you may find a seasonality effect for certain hours of the day. Some of the reasons for seasonality include holidays, climate, and changes in social habits. For example, travel companies usually run far fewer services on Christmas Day, citing a lack of demand. During most holidays people love to travel, but this lack of demand on Christmas Day could be attributed to social habits, where people tend to stay at home or have already traveled. Social habit becomes a driving factor in the seasonality of journeys undertaken on Christmas Day. It's easier for the forecaster when a particular seasonal event occurs on a fixed calendar date each year; the issue comes when some popular holidays depend on lunar movements, such as Easter, Diwali, and Eid. These holidays may occur in different weeks or months over the years, which will shift the seasonality effect. Also, if some holidays fall closer to other holiday periods, it may lead to individuals taking extended holidays and travel sales may increase more than expected in such years. The coffee shop near the office may also experience lower sales for a longer period. Changes in the weather can also impact seasonality; for example, a longer, warmer summer may be welcome in the UK, but this would impact retail sales in the autumn as most shoppers wouldn't need to buy a new wardrobe. In hotter countries, sales of air-conditioners would increase substantially compared to the summer months' usual seasonality. Forecasters could offset this unpredictability in seasonality by building in a weather forecast variable. We will explore similar challenges in the chapters ahead. Seasonality shouldn't be confused with a cyclic effect. A cyclic effect is observed over a longer period of generally two years or more. The property sector is often associated with having a cyclic effect, where it has long periods of growth or slowdown before the cycle continues. Trend A trend is merely a long-term direction of observed behavior that is found by plotting data against a time component. A trend may indicate an increase or decrease in behavior. Trends may not even be linear, but a broad movement can be identified by analyzing plotted data. Outliers and rare events Outliers and rare events are terminologies that are often used interchangeably by businesses. These concepts can have a big impact on data, and some sort of outlier treatment is usually applied to data before it is used for modeling. It is almost impossible to predict an outlier or rare event but they do affect a trend. An example of an outlier could be a customer walking into a branch to deposit an amount that is 100 times the daily average of that branch. In this case, the forecaster wouldn't expect that trend to continue. Disruptions Disruptions and step changes are becoming more common in time series data. One reason for this is the abundance of available data and the growing ability to store and analyze it. Disruptions could include instances when a business hasn't been able to trade as normal. Flooding at the local pub may lead to reduced sales for a few days, for example. While analyzing daily sales across a pub chain, an analyst may have to make note of a disruptive event and its impact on the chain's revenue. Step changes are also more common now due to technological shifts, mergers and acquisitions, and business process re-engineering. When two companies announce a merger, they often try to sync their data. They might have been selling x and y quantities individually, but after the merger will expect to sell x + y + c (where c is the positive or negative effect of the merger). Over time, when someone plots sales data in this case, they will probably spot a step change in sales that happened around the time of the merger, as shown in the following screenshot: In the trend graph, we can see that online travel bookings are increasing. In the step change and disruptions chart, we can see that Q1 of 2012 saw a substantive increase in bookings, where Q1 of 2014 saw a substantive dip. The increase was due to the merger of two companies that took place in Q1 of 2012. The decrease in Q1 of 2014 was attributed to prolonged snow storms in Europe and the ash cloud disruption from volcanic activity over Iceland. While online bookings kept increasing after the step change, the disruption caused by the snow storm and ash cloud only had an effect on sales in Q1 of 2014. In this case, the modeler will have to treat the merger and the disruption differently while using them in the forecast, as disruption could be disregarded as an outlier and treated accordingly. Also note that the seasonality chart shows that Q4 of each year sees almost a 20% increase in travel bookings, and this pattern continues each calendar year. In this article, we defined time series and learned why it is important for forecasting. We also looked at the characteristics of time series data. To know more how to leverage the analytical power of SAS to perform financial analysis efficiently, you can check out the book SAS for Finance. Read more Getting to know SQL Server options for disaster recovery Implementing a simple Time Series Data Analysis in R Training RNNs for Time Series Forecasting
Read more
  • 0
  • 0
  • 35859

article-image-send-email-notifications-using-sendgrid
Packt Editorial Staff
10 Aug 2018
6 min read
Save for later

How to send email Notifications using SendGrid

Packt Editorial Staff
10 Aug 2018
6 min read
SendGrid is one of the popular services that allow the audience to send emails for different purposes. In today’s tutorial we will explore to: Create SendGrid account Generate SendGrid API Key Configure SendGrid API key with Azure function app Send an email notification to the website administrator Here, we will learn how to create a SendGrid output binding and send an email notification to the administrator with a static content. In general there would be only administrators so we will be hard coding the email address of the administrator in the To address field of the SendGrid output binding Getting ready Create a SendGrid account API Key from the Azure Management Portal. Generate an API Key from the SendGrid Portal. Create a SendGrid account Navigate to Azure Management Portal and create a SendGrid Email Delivery account by searching for the same in the Marketplace shown as follows: In the SendGrid Email Delivery blade, click on Create button to navigate to the Create a new SendGrid Account. Please select Free tier in the Pricing tier and provide all other details and click on the Create button shown as follows: Once the account is created successfully, navigate to the SendGrid account. You can use the search box available in the top which is shown as follows: Navigate to the Settings, choose configurations and grab the username and SmtpServer from the Configurations blade. Generate SendGrid API key In order to utilize SendGrid account by the Azure Functions runtime, we need to provide the SendGrid API key as input to the Azure Functions. You can generate an API Key from the SendGrid portal. Let's navigate to the SendGrid portal by clicking on the Manage button in the Essentials blade of the SendGrid account shown as follows: In the SendGrid portal, click on the API Keys under Settings section of the Left hand side menu shown as follows: In the API Keys page, click on Create API Key shown as follows: In the Create API Key popup, provide a name and choose the API Key Permissions and click on Create & View button. After a moment you will be able to see the API key. Click on the key to copy the same to the clipboard: Configure SendGrid API key with Azure Function app Create a new app setting in the Azure Function app by navigating to the Application Settings blade under the Platform features section of the function app shown as follows: Click on Save button after adding the app settings in the preceding step. How to do it... Navigate to the Integrate tab of the RegisterUser function and click on New Output button to add a new output binding. Choose the SendGrid output binding and click on Select button to add the binding. Please provide the following parameters in the SendGrid output binding: Message parameter name - leave the default value - message. We will be using this parameter in the run method in a moment. SendGrid API key: Please provide the app settings key that you have created in the application settings. To address: Please provide the email address of the administrator. From address: Please provide the email address from where you would like to send the email. In general, it would be kind of donotreply@example.com. Message subject: Please provide the subject that you would like to have in the email subject. Message Text: Please provide the email body text that you would like to have in the email body. Below is how the SendGrid output binding should look like after providing all the fields: Once you review the values, click on Save to save the changes. Navigate to Run method and make the following changes: Add a new reference for SendGrid and also the namespace Add a new out parameter message of type Mail. Create an object of type Mail. Following is the complete code of the Run method: #r  "Microsoft.WindowsAzure.Storage" #r "SendGrid" using  System.Net; using SendGrid.Helpers.Mail; using  Microsoft.WindowsAzure.Storage.Table; using  Newtonsoft.Json; public  static  void  Run(HttpRequestMessage  req, TraceWriter  log, CloudTable  objUserProfileTable, out  string  objUserProfileQueueItem, out Mail message ) { var  inputs  =  req.Content.ReadAsStringAsync().Result; dynamic  inputJson  =  JsonConvert.DeserializeObject<dynamic>(inputs); string  firstname=  inputJson.firstname; string  lastname=inputJson.lastname; string  profilePicUrl  =  inputJson.ProfilePicUrl; objUserProfileQueueItem  =  profilePicUrl; UserProfile  objUserProfile  =  new  UserProfile(firstname,  lastname); TableOperation  objTblOperationInsert  = TableOperation.Insert(objUserProfile); objUserProfileTable.Execute(objTblOperationInsert); message = new Mail(); } public  class  UserProfile  :  TableEntity { public  UserProfile(string  lastName,  string  firstname,string profilePicUrl) { this.PartitionKey  =  "p1"; this.RowKey  =  Guid.NewGuid().ToString();; this.FirstName  =  firstName; this.LastName  =  lastName; this.ProfilePicUrl  =  profilePicUrl; } public  UserProfile()  {  } public  string  FirstName  {  get;  set;  } public  string  LastName  {  get;  set;  } public  string  ProfilePicUrl  {get;  set;} } Now, let's test the functionality of sending the email by navigating to the RegisterUser function and submit a request with the some test values: { "firstname": "Bill", "lastname": "Gates", "ProfilePicUrl":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/ Bill_Gates_June_2015.jpg/220px-Bill_Gates_June_2015.jpg" } How it works... The aim here is to send a notification via email to an administrator updating that a new registration got created successfully. We have used the one of the Azure Function experimental templates named SendGrid as a SMTP server for sending the emails by hard coding the following properties in the SendGrid output bindings: From email address To email address Subject of the email Body of the email SendGrid output bindings will use the API key provided in the app settings to invoke the required APIs of the SendGrid library for sending the emails. To summarize, we learnt about sending an email notification using SendGrid service. [box type="shadow" align="" class="" width=""]This article is an excerpt from the book, Azure Serverless Computing Cookbook, written by Praveen Kumar Sriram. It contains over 50 recipes to help you build applications hosted on Serverless architecture using Azure Functions.[/box] 5 reasons why your business should adopt cloud computing Alibaba Cloud partners with SAP to provide a versatile, one-stop cloud computing environment Top 10 IT certifications for cloud and networking professionals in 2018    
Read more
  • 0
  • 0
  • 18175

article-image-visualizing-data-r-and-python-using-anaconda
Natasha Mathur
09 Aug 2018
7 min read
Save for later

Visualizing data in R and Python using Anaconda [Tutorial]

Natasha Mathur
09 Aug 2018
7 min read
It is said that a picture is worth a thousand words. Through various pictures and graphical presentations, we can express many abstract concepts, theories, data patterns, or certain ideas much clearer. Data can be messy at times, and simply showing the data points would confuse audiences further. If we could have a simple graph to show its main characteristics, properties, or patterns, it would help greatly. In this tutorial, we explain why we should care about data visualization and then we will discuss techniques used for data visualization in R and Python. This article is an excerpt from a book 'Hands-On Data Science with Anaconda' written by Dr. Yuxing Yan, James Yan. Data visualization in R Firstly, let's see the simplest graph for R. With the following one-line R code, we draw a cosine function from -2π to 2π: > plot(cos,-2*pi,2*pi) The related graph is shown here: Histograms could also help us understand the distribution of data points. The previous graph is a simple example of this. First, we generate a set of random numbers drawn from a standard normal distribution. For the purposes of illustration, the first line of set.seed() is actually redundant. Its existence would guarantee that all users would get the same set of random numbers if the same seed was used ( 333 in this case). In other words, with the same set of input values, our histogram would look the same. In the next line, the rnorm(n) function draws n random numbers from a standard normal distribution. The last line then has the hist() function to generate a histogram: > set.seed(333) > data<-rnorm(5000) > hist(data) The associated histogram is shown here: Note that the code of rnorm(5000) is the same as rnorm(5000,mean=0,sd=1), which implies that the default value of the mean is 0 and the default value for sd is 1. The next R program would shade the left-tail for a standard normal distribution: x<-seq(-3,3,length=100) y<-dnorm(x,mean=0,sd=1) title<-"Area under standard normal dist & x less than -2.33" yLabel<-"standard normal distribution" xLabel<-"x value" plot(x,y,type="l",lwd=3,col="black",main=title,xlab=xLabel,ylab=yLabel) x<-seq(-3,-2.33,length=100) y<-dnorm(x,mean=0,sd=1) polygon(c(-4,x,-2.33),c(0,y,0),col="red") The related graph is shown here: Note that according to the last line in the preceding graph, the shaded area is red. In terms of exploring the properties of various datasets, the R package called rattle is quite useful. If the rattle package is not preinstalled, we could run the following code to install it: > install.packages("rattle") Then, we run the following code to launch it; > library(rattle) > rattle() After hitting the Enter key, we can see the following: As our first step, we need to import certain datasets. For the sources of data, we choose from seven potential formats, such as File, ARFF, ODBC, R Dataset, and RData File, and we can load our data from there. The simplest way is using the Library option, which would list all the embedded datasets in the rattle package. After clicking Library, we can see a list of embedded datasets. Assume that we choose acme:boot:Monthly Excess Returns after clicking Execute in the top left. We would then see the following: Now, we can study the properties of the dataset. After clicking Explore, we can use various graphs to view our dataset. Assume that we choose Distribution and select the Benford check box. We can then refer to the following screenshot for more details: After clicking Execute, the following would pop up. The top red line shows the frequencies for the Benford Law for each digits of 1 to 9, while the blue line at the bottom shows the properties of our data set. Note that if you don't have the reshape package already installed in your system, then this either won't run or will ask for permission to install the package to your computer: The dramatic difference between those two lines indicates that our data does not follow a distribution suggested by the Benford Law. In our real world, we know that many people, events, and economic activities are interconnected, and it would be a great idea to use various graphs to show such a multi-node, interconnected picture. If the qgraph package is not preinstalled, users have to run the following to install it: > install.packages("qgraph") The next program shows the connection from a to b, a to c, and the like: library(qgraph) stocks<-c("IBM","MSFT","WMT") x<-rep(stocks, each = 3) y<-rep(stocks, 3) correlation<-c(0,10,3,10,0,3,3,3,0) data <- as.matrix(data.frame(from =x, to =y, width =correlation)) qgraph(data, mode = "direct", edge.color = rainbow(9)) If the data is shown, the meaning of the program will be much clearer. The correlation shows how strongly those stocks are connected. Note that all those values are randomly chosen with no real-world meanings: > data from to width [1,] "IBM" "IBM" " 0" [2,] "IBM" "MSFT" "10" [3,] "IBM" "WMT" " 3" [4,] "MSFT" "IBM" "10" [5,] "MSFT" "MSFT" " 0" [6,] "MSFT" "WMT" " 3" [7,] "WMT" "IBM" " 3" [8,] "WMT" "MSFT" " 3" [9,] "WMT" "WMT" " 0" A high value for the third variable suggests a stronger correlation. For example, IBM is more strongly correlated with MSFT, with a value of 10, than its correlation with WMT, with a value of 3. The following graph shows how strongly those three stocks are correlated: The following program shows the relationship or interconnection between five factors: library(qgraph) data(big5) data(big5groups) title("Correlations among 5 factors",line = 2.5) qgraph(cor(big5),minimum = 0.25,cut = 0.4,vsize = 1.5, groups = big5groups,legend = TRUE, borders = FALSE,theme = 'gray') The related graph is shown here: Data visualization in Python The most widely used Python package for graphs and images is called matplotlib. The following program can be viewed as the simplest Python program to generate a graph since it has just three lines: import matplotlib.pyplot as plt plt.plot([2,3,8,12]) plt.show() The first command line would upload a Python package called matplotlib.pyplot and rename it to plt. Note that we could even use other short names, but it is conventional to use plt for the matplotlib package. The second line plots four points, while the last one concludes the whole process. The completed graph is shown here: For the next example, we add labels for both x and y, and a title. The function is the cosine function with an input value varying from -2π to 2π: import scipy as sp import matplotlib.pyplot as plt x=sp.linspace(-2*sp.pi,2*sp.pi,200,endpoint=True) y=sp.cos(x) plt.plot(x,y) plt.xlabel("x-value") plt.ylabel("Cosine function") plt.title("Cosine curve from -2pi to 2pi") plt.show() The nice-looking cosine graph is shown here: If we received $100 today, it would be more valuable than what would be received in two years. This concept is called the time value of money, since we could deposit $100 today in a bank to earn interest. The following Python program uses size to illustrate this concept: import matplotlib.pyplot as plt fig = plt.figure(facecolor='white') dd = plt.axes(frameon=False) dd.set_frame_on(False) dd.get_xaxis().tick_bottom() dd.axes.get_yaxis().set_visible(False) x=range(0,11,2) x1=range(len(x),0,-1) y = [0]*len(x); plt.annotate("$100 received today",xy=(0,0),xytext=(2,0.15),arrowprops=dict(facecolor='black',shrink=2)) plt.annotate("$100 received in 2 years",xy=(2,0),xytext=(3.5,0.10),arrowprops=dict(facecolor='black',shrink=2)) s = [50*2.5**n for n in x1]; plt.title("Time value of money ") plt.xlabel("Time (number of years)") plt.scatter(x,y,s=s); plt.show() The associated graph is shown here. Again, the different sizes show their present values in relative terms: To summarize, we discussed ways data visualization works in Python and R.  Visual presentations can help our audience understand data better. If you found this post useful, check out the book 'Hands-On Data Science with Anaconda' to learn about different types of visual representation written in languages such as R, Python, Julia, etc. A tale of two tools: Tableau and Power BI Anaconda Enterprise version 5.1.1 released! 10 reasons why data scientists love Jupyter notebooks
Read more
  • 0
  • 0
  • 52606

article-image-apple-joins-the-thread-group-signaling-its-smart-home-ambitions-with-homekit-siri-and-other-iot-products
Bhagyashree R
09 Aug 2018
3 min read
Save for later

Apple joins the Thread Group, signaling its Smart Home ambitions with HomeKit, Siri and other IoT products

Bhagyashree R
09 Aug 2018
3 min read
Apple is now a part of the Thread Group’s list of members, alongside its top rivals - Nest (a subsidiary of Google) and Amazon. This indicates some advancements in their HomeKit software framework and inter-device communication between iOS devices. Who is the Thread Group? The Thread Group is a non-profit company who have developed the network protocol Thread, with the aim of being the best way to connect and control IoT products. These are the features that enable them to do so: Mesh networking: It uses mesh network design, connecting hundreds of products securely and reliably, which also means no single point of failure. Secure: It provides security at network and application layers. To ensure only authorized devices join the network, it uses product install codes. They use AES encryption to close security holes that exist in other wireless protocols and smartphone-era authentication scheme. Battery friendly: Based on the power efficient IEEE 802.15.4 MAC/PHY, it ensures extremely low power consumption. Short messaging, streamlined routing protocol, use of low power wireless system-on-chips also makes it battery friendly. Based on IPv6: It is interoperable by design using proven, open standards and IPv6 technology with 6LoWPAN (short for, IPv6 over Low-Power Wireless Personal Area Networks) as the foundation. 6LoWPAN is an IPv6 based low-power wireless personal area network which is comprises of devices that conform to the IEEE 802.15.4-2003 standard Scalable: It can scale upto 250+ devices into a single network supporting multiple hops. What this membership brings to Apple? The company has not revealed their plans yet, but nothing is stopping us from imagining what they possibly could do with Thread. According to a redditor, the following are some potential use of Thread by Apple HomeKit by Apple uses WiFi and Bluetooth as its wireless protocols. WiFi is very power hungry and Bluetooth is short-ranged. With Thread’s mesh network and power-efficient design this problem could be solved. Apple only allows certain products to operate on battery, requiring others to be plugged into power constantly, HomeKit cameras, for instance. Critical to both Apple and extended-use home devices, Thread promises “extremely low power consumption.” Apple could have plans to provide support for the number of IoT smart home devices the HomePod is capable of connecting to with Thread. With the support of Thread, iOS devices could guarantee better inter-device Siri communications, more reliable continuity features, and secure geo-fencing. Apple joining the group could mean that it may become open to more hardware when it comes to its HomeKit and also become reasonable from a cost perspective in the smart home area. Apple releases iOS 12 beta 2 with screen time and battery usage updates among others macOS Mojave: Apple updates the Mac experience for 2018 Apple stocks soar just shy of $1 Trillion market cap as revenue hits $53.3 Billion in Q3 earnings 2018
Read more
  • 0
  • 0
  • 20585
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime
article-image-why-golan-is-the-fastest-growing-language-on-github
Sugandha Lahoti
09 Aug 2018
4 min read
Save for later

Why Golang is the fastest growing language on GitHub

Sugandha Lahoti
09 Aug 2018
4 min read
Google’s Go language or alternatively Golang is currently one of the fastest growing programming languages in the software industry. Its speed, simplicity, and reliability make it the perfect choice for all kinds of developers. Now, its popularity has further gained momentum. According to a report, Go is the fastest growing language on GitHub in Q2 of 2018. Go has grown almost 7% overall with a 1.5% change from the previous Quarter. Source: Madnight.github.io What makes Golang so popular? A person was quoted on Reddit saying, “What I would have done in Python, Ruby, C, C# or C++, I'm now doing in Go.” Such is the impact of Go. Let’s see what makes Golang so popular. Go is cross-platform, so you can target an operating system of your choice when compiling a piece of code. Go offers a native concurrency model that is unlike most mainstream programming languages. Go relies on a concurrency model called CSP ( Communicating Sequential Processes). Instead of locking variables to share memory, Golang allows you to communicate the value stored in your variable from one thread to another. Go has a fairly mature package of its own. Once you install Go, you can build production level software that can cover a wide range of use cases from Restful web APIs to encryption software, before needing to consider any third party packages. Go code typically compiles to a single native binary, which basically makes deploying an application written in Go as easy as copying the application file to the destination server. Go is also being rapidly being adopted as the go-to cloud native language and by leading projects like Docker and Ethereum. It’s concurrency feature and easy deployment make it a popular choice for cloud development. Can Golang replace Python? Reddit is abuzz with people sharing their thoughts about whether Golang would replace Python. A user commented that “Writing a utility script is quicker in Go than in Python or JS. Not quicker as in performance, but in terms of raw development speed.” Another Reddit user pointed out three reasons not to use Python in a Reddit discussion, Why are people ditching python for go?: Dynamic compilation of Python can result in errors that exist in code, but they are in fact not detected. CPython really is very slow; very specifically, procedures that are invoked multiple times are not optimized to run more quickly in future runs (like pypy); they always run at the same slow speed. Python has a terrible distribution story; it's really hard to ship all your Python dependencies onto a new system. Go addresses those points pretty sharply. It has a good distribution story with static binaries. It has a repeatable build process, and it's pretty fast. In the same discussion, however, a user nicely sums it up saying, “There is nothing wrong with python except maybe that it is not statically typed and can be a bit slow, which also depends on the use case. Go is the new kid on the block, and while Go is nice, it doesn't have nearly as many libraries as python does. When it comes to stable, mature third-party packages, it can't beat python at the moment.” If you’re still thinking about whether or not to begin coding with Go, here’s a quirky rendition of the popular song Let it Go from Disney’s Frozen to inspire you. Write in Go! Write in Go! Go Cloud is Google’s bid to establish Golang as the go-to language of cloud Writing test functions in Golang [Tutorial] How Concurrency and Parallelism works in Golang [Tutorial]
Read more
  • 0
  • 0
  • 56943

article-image-share-projects-and-environment-on-anaconda
Natasha Mathur
09 Aug 2018
7 min read
Save for later

Share projects and environment on Anaconda cloud [Tutorial]

Natasha Mathur
09 Aug 2018
7 min read
When a small group of developers works on a same project, there is a need to share programs, command datasets, and working environments, and Anaconda Cloud could be used for this.  Usually, we can save our data on other people's servers. For Anaconda Cloud, users can use the platform to save and share packages, notebooks, projects, and environments. The public projects and notebooks are free. At the moment, private plans start at $7 per month. Anaconda Cloud allows users to create or distribute software packages. In this article, we will learn about Anaconda cloud and how to share projects and environment on Anaconda. This article is an excerpt from a book 'Hands-On Data Science with Anaconda' written by Dr. Yuxing Yan, James Yan. So, let's get started! Firstly, for a Windows version of Anaconda, click All Programs | Anaconda, and then choose Anaconda Cloud. After double-clicking on Cloud, the welcome screen will appear. Based on the information presented by the welcome screen, we know that we need an account with Anaconda before we can use it. After login, we will see the following screen: For example, if you double-click on Installing your first package, you will get more information on Anaconda Cloud. We do not need to be logged in, or even need a cloud account, to search for public packages, download, and install them. We need an account only to access private packages without a token or to share your packages with others. For Anaconda Cloud, users can use the platform to save and share projects and environments. Sharing projects in Anaconda First, let's look at the definition of a project. A project is a folder that contains an anaconda-project.yml configuration file together with scripts (code), notebooks, datasets, and other files. We can add a folder into a project by adding a configuration file named anaconda-project.yml to the folder. The configuration file can include the following sections: commands, variables, services, downloads, packages, channels, and environment specifications. Data scientists can use projects to encapsulate data science projects and make them easily portable. A project is usually compressed into a .tar.bz2 file for sharing and storing. Anaconda Project automates setup steps so that people with whom you share projects can run your projects with the following single command: anaconda-project run To install Anaconda Project, type the following: conda install anaconda-project Anaconda Project encapsulates data science projects and makes them easily portable. It automates setup steps such as installing the right packages, downloading files, setting environment variables, and running commands. Project makes it easy to reproduce your work, share projects with others, and run them on different platforms. It also simplifies deployment to servers. Anaconda projects run the same way on your machine, on another user's machine, or when deployed to a server. Traditional build scripts such as setup.py automate the building of the project – going from source code to something runnable – while Project automates running the project, taking build artifacts, and doing any necessary setup before executing them. We can use Project on Windows, macOS, and Linux. Project is supported and offered by Anaconda Inc® and contributors under a three-clause BSD license. Project sharing will save us a great deal of time since other developers will not spend too much time on the work done already. Here is the procedure: Build up your project Log in to Anaconda From the project's directory on your computer, type the following command: anaconda-project upload Alternatively, from Anaconda Navigator, in the Projects tab, upload via the bottom-right Upload to Anaconda Cloud. Projects can be any directory of code and assets. Often, projects will contain notebooks or Bokeh applications, for example. Here, we show how to generate a project called project01. First, we want to move to the correct location. Assume that we choose c:/temp/. The key command is given here: anaconda-project init --directory project01 Next, both commands are shown side by side as well: $ cd c:/temp/ $ anaconda-project init --directory project01 Create directory 'c:tempproject01'? y Project configuration is in c:tempproject01iris/anaconda-project.yml The corresponding output is shown here: We can also turn any existing directory into a project by switching to the directory and then running anaconda-project init without options or arguments. We can use MS Word to open anaconda-project.yml (see the first couple of lines shown here): # This is an Anaconda project file. # # Here you can describe your project and how to run it. # Use `anaconda-project run` to run the project. # The file is in YAML format, please see http://www.yaml.org/start.html for more. # # Set the 'name' key to name your project # name: project01 # # Set the 'icon' key to give your project an icon # icon: # # Set a one-sentence-or-so 'description' key with project details # description: # # In the commands section, list your runnable scripts, notebooks, and other code. # Use `anaconda-project add-command` to add commands. # There are two ways to share our projects with others. First, we archive the project by issuing the following command: anaconda-project archive project01.zip Then, we email the ZIP file to our colleague or others. The second way of sharing a project is to use Anaconda Cloud. Log in to Anaconda Cloud first. From the project's directory on our computer, type anaconda-project upload, or, from Anaconda Navigator, in the Projects tab, upload via the bottom-right Upload to Anaconda Cloud. Now that we're done looking at how you can share projects. Let's find out how you can share environments with your partner. Sharing of environments In terms of computer software, an operating environment or integrated applications environment is the environment in which users can execute software. Usually, such an environment consists of a user interface and an API. To a certain degree, the term platform could be viewed as its synonym. There are many reasons why we want to share our environment with someone else. For example, they can re-create a test that we have done. To allow them to quickly reproduce our environment with all of its packages and versions, give them a copy of your environment.yml file. Depending on the operating system, we have the following methods to export our environment file. Note that if we already have an environment.yml file in our current directory, it will be overwritten during this task. There are different ways to activate the myenv environment file depending on the systems used. For Windows users, in our Anaconda prompt, type the following command: activate myenv On macOS and Linux, in our Terminal window, issue the following command: source activate myenv Note that we replace myenv with the name of the environment. To export our active environment to a new file, type the following: conda env export > environment.yml To share, we can simply email or copy the exported environment.yml file to the other person. On the other hand, in order to remove an environment, run the following code in our Terminal window or at an Anaconda prompt: conda remove --name myenv --all Alternatively, we can specify the name, as shown here: conda env remove --name myenv To verify that the environment was removed, run the following command line: conda info --envs In this tutorial, we discussed Anaconda Cloud. Some topics included how to share different projects over different platforms and how to share your working environments. If you found this post useful, be sure to check out the book 'Hands-On Data Science with Anaconda' to learn further about replicating others' environments locally, and downloading a package from Anaconda. Anaconda 5.2 releases! Anaconda Enterprise version 5.1.1 released! 10 reasons why data scientists love Jupyter notebooks
Read more
  • 0
  • 0
  • 15759

article-image-create-unity-character-animations-and-avatars
Amarabha Banerjee
08 Aug 2018
10 min read
Save for later

Creating interactive Unity character animations and avatars [Tutorial]

Amarabha Banerjee
08 Aug 2018
10 min read
The early Unity versions' Legacy Animation System is used in Unity for a wide range of things, such as animating the color of a light or other simple animations on 3D objects in a scene, as well as animating skinned characters for certain kinds of games. In this tutorial, we will look at the basic settings for the Legacy Animation System. Then, we will step into the new animation system, gaining an understanding of the ThirdPersonCharacter prefab, and looking at the difference between the in-place and Root Motion animation methods available within Animator. If you want to dive deep into developing cutting-edge modern day Unity 2D games then this piece is for you. We will deal with character animations using Unity today. This article is an excerpt from the book Unity 2017 Game Development Essentials written by Tommaso Lintrami.  Importing character models and animations To import a model rig or an animation, just drag the model file to the Assets folder of your project. When you select the file in the Project view, you can edit the Import Settings in the Inspector panel: Please refer to the updated Unity online manual for a full description of the available import options: https://docs.unity3d.com/Manual/FBXImporter-Model.html. Importing animations using multiple model files The common way to import animations in Unity is to follow a naming convention scheme that is recognized automatically. You basically create, or ask the artist to create, separate model files and name them with the modelName@animationName.fbx convention. For example, for a model called Warrior_legacy, you could import separate idle, walk, jump, and attack animations using files named Warrior_legacy@idle.fbx, Warrior_legacy@jump.fbx, Warrior_legacy@standard_run_inPlace.fbx, and Warrior_legacy@walking_inPlace.fbx. Only the animation data from these files will be used, even if the original files are exported with mesh data from the animation software package: In the editor's Project view, the .fbx suffix is not shown in the preview, but can still be seen in the bottom line of the view. Unity automatically imports all the files, collects all the animation clips from them, and associates them with the file without the @ symbol. In the example above, the Warrior_legacy.fbx file will be set up to reference offensive_idle, jumping, running_inPlace, and sword_and_shield_walk_inPlace. To export the base rig, simply export a model file from your favorite digital content creation package with no animations ticked in the FBX exporter (for example, Warrior_legacy.fbx)  and the four animation clips as Warrior_legacy@animname.fbx by exporting the desired keyframes for each one of them (enabling animation in the graphic package's FBX export dialog). When imported in Unity, we will select the main rig file ( Warrior_legacy.fbx) and set its Rig type to Legacy: Setting up the animation We need to instruct Unity on how we want to play these animation clips, for instance, we certainly want the walk, idle, and running animation clips to play in a loop, while the jump and the attack animation clips should play in a single shot. Choose the Idle animation clip in the Project view folder where the legacy animation resides and then switch to the Animations tab in the Inspector: Set the Wrap Mode to PingPong in both the top and bottom parts of the panel, as shown in the preceding image. In many cases, you might also want to create an additional in-between loop frame, checking the Add Loop Frame option. This is needed to avoid an ugly animation loop being performed because the first and last frame of the animation are too different from each other. Click on the Apply button at the bottom of the panel to apply the changes. This will be required if the first and last frames of the animation are much different and require an additional frame in-between to interpolate between the two in order to obtain a good loop for this Animation Clip. Now, drag the Warrior_legacy.fbx main file into the scene. You should see a new GameObject with an Animations component attached, with all the reference clips already set up, and with the first specified to play at start when the Play On Awake checkbox is selected in the component (default). You can look at the final result for this part in the Chapter5_legacy Unity scene in the book's code project folder. Building the Player with Animator The Animator component was introduced in Unity 4 to replace the older Legacy Animation System. If you are completely new to Unity, you should start directly with the new animation system and consider the old one as still being good for many things, not only related to character animation. Animator introduced many cool things that were only partially available (and only through coding) with the old animation system. In the code folder, under Chapter 5-6-7/Models/Characters, you will find three folders for the warrior model rig. One is meant for the old Legacy Animation component, and the other two are for use with the Animator. The new system is made by a new animation component, the Animator, a powerful state-machine that controls the whole animation process and the Avatar configuration system. The Animator component will be mapped to a corresponding avatar and to an Animator Controller asset file, which can be created, like other files, from the Project view and edited in the Animator window. What is an avatar in Unity? When an .fbx 3D model file with a skeleton made of joints/bones is imported in Unity, if you expand the file in the Project view, you will see, among the various parts of it, an avatar's icon. The following screenshot represents the Warrior_Final.fbx rigged model automatically created by the Warrior_FinalAvatar component: When importing a rigged model instead (an FBX model with a skeleton or bones and, optionally, animations), Unity will configure it automatically for a Generic avatar. A Generic avatar is meant for any kind of non-human character rig, such as animals, non-biped monsters, plants, and so on. Typically, for your biped/humanoid characters, you want to switch the default import flag for the Rig Animation Type to Humanoid: This term comes from the Latin word bi (two) and ped (foot); this 3D animation-specific term indicates an anthropomorphic/humanoid character standing and walking on two legs. This name was introduced into 3D animation by 3D Studio Max, where Biped was the term for Character Studio to manage a rigged human character and its animations. As the default import setting for the Rig is Generic, we will switch to Humanoid for all the .fbx files in the Warrior_Mecanim_InPlace folder with the only exclusion being the non-rigged Warrior_final_non-rigged.fbx sample model mentioned earlier. Configuring an avatar Now, hit the Configure button and the actual scene and the Inspector will be temporarily replaced with the avatar, (as in the following screenshot), until the Done button is clicked and the editor returns to the previously loaded scene. Because the model included in the book codes is already a Mecanim-ready rig, you can just click on the Done button. On the left, the Scene view switched temporary for showing Avatar configuration results and the Inspector on the right showing configuration options. Most of the time, and in this case, you will not set the mapping for hands, fingers, and eyes separately, so the first of the four tabs (Body, Head, Left Arm, and Right Arm) will be enough for our purpose. The head is usually mapped for independent eyeball movement and/or jaw movement to allow a basic character speech movement whenever your game needs any of these features. The head part of the avatar configuration Inspector panel is shown as follows: A quick note on lip sync. Lip sync is an advanced technique, where a 3D character's face will change and animate its mouth and eyes when a certain audio file is playing. Unity doesn't support lip sync out of the box, but it can be done in many ways with external libraries and an appropriate model rig. Since Unity 4.5 onward, animation Blend Shapes are supported, allowing facial muscles' gestures to be embedded in the .fbx model and used in real time by the application. This technique is more modern than standard lip sync for game character's speeches; in both cases, a library or a discreet amount of coding would be needed to make the character speak correctly when the corresponding audio file is played. Hands mapping will be used only when your characters need fine finger movements hence will not be covered. The best scenario for this is a game where characters perform a lot of specific actions with many different tools (guns, hammers, knives, hacking, or maybe just making gestures while talking during a cinematic cut scene). Another example would be an avatar for virtual reality, where the Leap Motion, Data Gloves, or similar devices are used to track the hands of users with the 3 phalanges of their 10 fingers. If the rig you are importing is not Mecanim-ready, this is the place to map your bones to the appropriate spots on the green diagram in the Inspector, which is subdivided into body, head, left hand, and right hand. To configure an Avatar from a model that was not rigged following Mecanim's skeleton rules, we have the following two options: Using the auto-mapping feature available, which will try to automatically map the bones for you Manually map the bones of your model to the corresponding spots on the diagram The avatar configuration Inspector panel shows the skeleton's main bones mapped to the avatar: The Automap feature, accessible from the drop-down menu at the bottom-left part of the avatar Inspector, can automatically assign the bones of your models to the correct ones for a mecanim rig. This is mainly performed by reading bone names and analyzing the structure of the skeleton, and it is not 100% rig proof. So, you might need some tweaking (manual mapping) of your custom character models. As you can see, there are also Load and Save options to store this mapping. This is useful if you have a whole bunch of rigged character models all done with the same skeleton naming convention. The Clear option will clear up all the current bone mapping. The Pose drop-down menu is needed only if you want to enforce the T-pose, or sample the actual pose, and is rarely needed, but can help fix eventual modeler/3D artist mistakes or to make variations of an avatar. We discussed about creating Unity Character animations and how it will help you build interactive games with unity. Check out the book Unity 2017 Game Development Essentials for hands on game development in Unity 2017. Unite Berlin 2018 Keynote: Unity partners with Google, launches Ml-Agents ToolKit 0.4, Project MARS AI for Unity game developers: How to emulate real-world senses in your NPC agent Working with Unity Variables to script powerful Unity 2017 games
Read more
  • 0
  • 0
  • 28726

article-image-diffractive-deep-neural-network-d2nn-ucla-developed-ai-device-can-identify-objects-at-the-speed-of-light
Bhagyashree R
08 Aug 2018
3 min read
Save for later

Diffractive Deep Neural Network (D2NN): UCLA-developed AI device can identify objects at the speed of light

Bhagyashree R
08 Aug 2018
3 min read
Researchers at the University of California, Los Angeles (UCLA) have developed a 3D-printed all-optical deep learning architecture called Diffractive Deep Neural Network (D2NN). D2NN is a deep learning neural network physically formed by multiple layers of diffractive surfaces that work in collaboration to optically perform an arbitrary function. While the inference/prediction of the physical network is all-optical, the learning part that leads to its design is done through a computer. How does D2NN work? A computer-simulated design was created first, then the researchers with the help of a 3D printer created very thin polymer wafers. The uneven surface of the wafers helped diffract light coming from the object in different directions. The layers are composed of tens of thousands of artificial neurons or tiny pixels from which the light travels through. These layers together, form an “optical network” that shapes how incoming light travels through them. The network is able to identify an object because the light coming from the object is diffracted mostly toward a single pixel that is assigned to that type of object. The network was then trained using a computer to identify the objects in front of it by learning the pattern of diffracted light each object produced as the light from that object passes through the device. What are its advantages? Scalable: It can easily be scaled up using numerous high-throughput and large-area 3D fabrication methods, such as, soft-lithography, additive manufacturing, and wide-field optical components and detection systems. Easily reconfigurable: D2NN can be easily improved by additional 3D printed layers or replacing some of the existing layers with newly trained ones. Lightening speed: Once the device is trained, it works at the speed of light. Efficient: No energy is consumed to run the device. Cost-effective: The device can be reproduced for less than $50, making it very cost-effective. What are the areas it can be used in? Image analysis Feature detection Object classification Can also enable new microscope or camera designs that can perform unique imaging tasks This new AI device could find applications in the area of medical technologies, data intensive tasks, robotics, security, and or any application where image and video data are essential. Refer to UCLA’s official news article to know more in detail. Also, you can refer to this paper  All-optical machine learning using diffractive deep neural Networks. OpenAI builds reinforcement learning based system giving robots human like dexterity Datasets and deep learning methodologies to extend image-based applications to videos AutoAugment: Google’s research initiative to improve deep learning performance
Read more
  • 0
  • 0
  • 14700
article-image-unity-game-engine-assets-2d-game-development
Amarabha Banerjee
08 Aug 2018
9 min read
Save for later

Implementing Unity game engine and assets for 2D game development [Tutorial]

Amarabha Banerjee
08 Aug 2018
9 min read
The rise of mobile platforms has been in part thanks to its popularity with indie developers, who prefer the short development cycles. The most prevalent medium on mobile is 2D and Unity has a host of features that support 2D game development, including Sprite Editing and Packing, as well as physics specifically designed for 2D games. In this tutorial, we will look at creating Unity game engine and assets for 2D games. This article is an excerpt from the book Unity 2017 Game Development Essentials written by Tommaso Lintrami.  Setting up the scene and preparing game assets Create a new scene from the main menu by navigating to Assets | Create | Scene, and name it ParallaxGame. In this new scene, we will set up, step by step, all the elements for our 2D game prototype. First of all, we will switch the camera setting in the Scene view to 2D by clicking on the button as shown by the red arrow in the following screenshot: As you can see, now the Scene view camera is orthographic. You can't rotate it as you wish, as you can do with the 3D camera. Of course, we will want to change this setting on our Main Camera as well. Also, we want to change the Orthographic size to 4.5 to have the correct view of the scene. Instead, for the Skybox, we will choose a very dark or black color as clear color in the depth setting. This is how the Inspector should look when these settings are done: While the Clipping Planes distances are important for setting the size of the frustum cone of a 3D, for the Perspective camera (inside which everything will be rendered by the engine), we should only set the Orthographic Size to 4.5, to have the correct distance of the 2D camera from the scene. When these settings are done, proceed by importing Chapter2-3-4.unitypackage into the project. You can either double-click on the package file with Unity open, or use the top menu: Assets | Import | Custom Package. If you haven't imported all the materials from the book's code already, be sure to include the Sprites subfolder. After the import, look in the Sprites/Parallax/DarkCave folder in the Project view and you will find some images imported as textures (as per default). The first thing we want to do now is to change the import settings of these images, in the Inspector, from Texture to Sprite (2D and UI). To do so, select all the images in the Project view in the Sprites/Parallax/DarkCave folder, all except the _reference_main_post file. Which is just a picture used as a reference of what the game level should look like: The Import Settings shown in the Inspector after selecting the seven images in the Project view The Max Size setting is hidden (-) because we have a multi-selection of image files. After having made the multiple selections, again, in the Inspector, we will do the following: Set the Texture Type option to Sprites (2D and UI). By default, images are imported as textures; to import them as Sprites, this type must be set. Uncheck the Generate Mip Maps option as we don't need MIP maps for this project as we are not going to look at the Sprites from a distant point of view, for example, games with the zoom-in/zoom-out feature (like the original Grand Theft Auto 2D game) would need this setting checked. Set Max Size to the maximum allowed. To ensure that you import all the images at their maximum resolution, set this to 8192. This is the maximum resolution size for an image on a modern PC, imported as a Sprite or texture. We set it so high because most of the background images we have in the collection are around 6,000 pixels wide. Click on the Apply button to apply these changes to all the images that were selected: The Project view showing the content of the folder after the images have been set to Sprite in the Import Settings Placing the prefabs in the game Unity can place the prefabs in the game in many ways, the usual, visual method is to drag a stored prefab or another kind of file/object directly into the scene. Before dragging in the Sprites we imported, we will create an empty GameObject and rename it ParallaxCave. We will drag the layer images we just imported as Sprites, one by one, from the Project view (pointing at the Assets/Chapters2-3-4/Sprites/Background/DarkCave folder) into the Scene view, or more simply, directly in the Hierarchy view as the children of our ParallaxCave GameObject, resulting in a scene Hierarchy like the one illustrated here: You can't drag all of them instantly because Unity will prompt you to save an animation filename for the selected collection of Sprites; we will see this later for our character and for the collectable graphics. The ParallaxCave GameObject and its children are in blue because this GameObject is stored as a prefab. When the link with the prefab is broken for a modification, the GameObject in the Hierarchy will become black again. When you see a red GameObject in the scene, it means that the prefab file that was linked to that GameObject was deleted. Importing and placing background layers In any game engine, 2D elements, such as Sprites, are rendered following a sort order; this order is also called the z-order because it is a way to express the depth or to cope with the missing z axis in a two-dimensional context. The sort order is assigned an integer number which can be positive or negative; 0 is the middle point of this draw order. Ideally, a sort order of zero expresses the middle ground, where the player will act, or near its layer. Look at this image: All positive numbers will render the Sprite element in front of the other elements with a lower number. The graphic set we are going to use was taken from the Open Game Art website at http://opengameart.org. For simplicity, the provided background image files are named with a number within parentheses, for example, middleground(z1), which means that this image should be rendered with a z sort order of 1. Change the sort order property of the Sprite component on each child object under ParallaxCave according to the value in the parentheses at the end of their filenames. This will rearrange the graphics into the appropriately sorted order. After we place and set the correct layer order for all the images, we should arrange and scale the layers in a proper manner to end as something like the reference image furnished in the Assets/Chapters2-3-4/Sprites/Background/DarkCave/ folder. You can take a look at the final result for this part anytime, by saving the current scene and loading the Chapter3_start.unity scene. On the optimization side, Sprites can be packed together in a single atlas texture with the Sprite Packer into a single image atlas (a single image containing a whole group of Sprites). Implementing parallax scrolling Parallax scrolling is a graphic technique where the background content (that is, an image) is moved at a different speed than the foreground content while scrolling. The technique was derived from the multiplane camera technique used in traditional animation since the 1930s. Parallax scrolling was popular in the 1980s and early 1990s and started to see light with video games such as Moon Patrol and Jungle Hunt, both released in 1982. On such a display system, a game can produce parallax by simply changing each layer's position by a different amount in the same direction. Layers that move more quickly are perceived to be closer to the virtual camera. Layers can be placed in front of the playfield, the layer containing the objects with which the player interacts, for various reasons, such as to provide increased dimension, obscure some of the action of the game, or distract the player. Here follows a short list of the first parallax scrolling games which made the history of video games: Moon Patrol (Atari, 1982) https://youtu.be/HBOKWCpwGfM https://en.wikipedia.org/wiki/Moon_Patrol Shadow of the Beast (Psygnosis, 1989) https://youtu.be/w6Osnolfxqw https://en.wikipedia.org/wiki/Shadow_of_the_Beast Super Mario World (Nintendo, 1990) https://www.youtube.com/watch?v=htFJTiVH5Ao https://en.wikipedia.org/wiki/Super_Mario_World Sonic The Hedgehog (Sega, 1991) https://youtu.be/dws4ij2IFH4 https://en.wikipedia.org/wiki/Sonic_the_Hedgehog_(1991_video_game) Making it last forever There are many roads we could take to make the hero run last forever and to achieve parallax scrolling. You can find a lot of different ready-made solutions in the Asset Store and there are also many General Public License (GPL) open source pieces of code written in C that we could take inspiration from. Using the Asset Store I chose FreeParallax from the Asset Store because it is powerful, free, and a well-written piece of code. Also, the modifications needed to achieve our game prototype on this class are very few. Let's download and import the system from the Asset Store. First, navigate to http://u3d.as/bvv: Click on the Open in Unity button to allow Unity to open this entry in the Asset Store window. You can, alternatively, search for the package directly in Unity  by opening the store from the top menu: Windows | Asset Store (recommended). In the search box type: parallax; also choose FREE ONLY like in this screenshot: You should now find the correct entry, the Free Parallax for Unity(2D) package. You can now download the package and import it into your project straight away. We saw how to create Unity game engine and assets for 2D games. Check out the book Unity 2017 Game Development Essentials to know more ways of creating interactive 2D games. Unite Berlin 2018 Keynote: Unity partners with Google, launches Ml-Agents ToolKit 0.4, Project MARS AI for Unity game developers: How to emulate real-world senses in your NPC agent Working with Unity Variables to script powerful Unity 2017 games
Read more
  • 0
  • 0
  • 20093

article-image-create-machine-learning-pipelines-using-unsupervised-automl
Sunith Shetty
07 Aug 2018
11 min read
Save for later

Create machine learning pipelines using unsupervised AutoML [Tutorial]

Sunith Shetty
07 Aug 2018
11 min read
AutoML uses unsupervised algorithms for performing an automated process of algorithm selection, hyperparameter tuning, iterative modeling, and model assessment.  When your dataset doesn't have a target variable, you can use clustering algorithms to explore it, based on different characteristics. These algorithms group examples together, so that each group will have examples as similar as possible to each other, but dissimilar to examples in other groups. Since you mostly don't have labels when you are performing such analysis, there is a performance metric that you can use to examine the quality of the resulting separation found by the algorithm. It is called the Silhouette Coefficient. The Silhouette Coefficient will help you to understand two things: Cohesion: Similarity within clusters Separation: Dissimilarity among clusters It will give you a value between 1 and -1, with values close to 1 indicating well-formed clusters. Clustering algorithms are used to tackle many different tasks such as finding similar users, songs, or images, detecting key trends and changes in patterns, understanding community structures in social networks. This tutorial deals with using unsupervised machine learning algorithms for creating machine learning pipelines. The code files for this article are available on Github. This article is an excerpt from a book written by Sibanjan Das, Umit Mert Cakmak titled Hands-On Automated Machine Learning.  Commonly used clustering algorithms There are two types of commonly used clustering algorithms: distance-based and probabilistic models. For example, k-means and Density-Based Spatial Clustering of Applications with Noise (DBSCAN) are distance-based algorithms, whereas the Gaussian mixture model is probabilistic. Distance-based algorithms may use a variety of distance measures where Euclidean distance metrics are usually used. Probabilistic algorithms will assume that there is a generative process with a mixture of probability distributions with unknown parameters and the goal is to calculate these parameters from the data. Since there are many clustering algorithms, picking the right one depends on the characteristics of your data. For example, k-means will work with centroids of clusters and this requires clusters in your data to be evenly sized and convexly shaped. This means that k-means will not work well on elongated clusters or irregularly shaped manifolds. When your clusters in your data are not evenly sized or convexly shaped, you many want to use DBSCAN to cluster areas of any shape. Knowing a thing or two about your data will bring you closer to finding the right algorithms, but what if you don't know much about your data? Many times when you are performing exploratory analysis, it might be hard to get your head around what's happening. If you find yourself in this kind of situation, an automated unsupervised ML pipeline can help you to understand the characteristics of your data better. Be careful when you perform this kind of analysis, though; the actions you will take later will be driven by the results you will see and this could quickly send you down the wrong path if you are not cautious. Creating sample datasets with sklearn In sklearn, there are some useful ways to create sample datasets for testing algorithms: # Importing necessary libraries for visualization import matplotlib.pyplot as plt import seaborn as sns # Set context helps you to adjust things like label size, lines and various elements # Try "notebook", "talk" or "paper" instead of "poster" to see how it changes sns.set_context('poster') # set_color_codes will affect how colors such as 'r', 'b', 'g' will be interpreted sns.set_color_codes() # Plot keyword arguments will allow you to set things like size or line width to be used in charts. plot_kwargs = {'s': 10, 'linewidths': 0.1} import numpy as np import pandas as pd # Pprint will better output your variables in console for readability from pprint import pprint # Creating sample dataset using sklearn samples_generator from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import StandardScaler # Make blobs will generate isotropic Gaussian blobs # You can play with arguments like center of blobs, cluster standard deviation centers = [[2, 1], [-1.5, -1], [1, -1], [-2, 2]] cluster_std = [0.1, 0.1, 0.1, 0.1] # Sample data will help you to see your algorithms behavior X, y = make_blobs(n_samples=1000, centers=centers, cluster_std=cluster_std, random_state=53) # Plot generated sample data plt.scatter(X[:, 0], X[:, 1], **plot_kwargs) plt.show() We get the following plot from the preceding code: cluster_std will affect the amount of dispersion. Change it to [0.4, 0.5, 0.6, 0.5] and try again: cluster_std = [0.4, 0.5, 0.6, 0.5] X, y = make_blobs(n_samples=1000, centers=centers, cluster_std=cluster_std, random_state=53) plt.scatter(X[:, 0], X[:, 1], **plot_kwargs) plt.show() We get the following plot from the preceding code: Now it looks more realistic! Let's write a small class with helpful methods to create unsupervised experiments. First, you will use the fit_predict method to apply one or more clustering algorithms on the sample dataset: class Unsupervised_AutoML: def __init__(self, estimators=None, transformers=None): self.estimators = estimators self.transformers = transformers pass Unsupervised_AutoML class will initialize with a set of estimators and transformers. The second class method will be fit_predict: def fit_predict(self, X, y=None): """ fit_predict will train given estimator(s) and predict cluster membership for each sample """ # This dictionary will hold predictions for each estimator predictions = [] performance_metrics = {} for estimator in self.estimators: labels = estimator['estimator'](*estimator['args'], **estimator['kwargs']).fit_predict(X) estimator['estimator'].n_clusters_ = len(np.unique(labels)) metrics = self._get_cluster_metrics(estimator['estimator'].__name__, estimator['estimator'].n_clusters_, X, labels, y) predictions.append({estimator['estimator'].__name__: labels}) performance_metrics[estimator['estimator'].__name__] = metrics self.predictions = predictions self.performance_metrics = performance_metrics return predictions, performance_metrics The fit_predict method uses the _get_cluster_metrics method to get the performance metrics, which is defined in the following code block: # Printing cluster metrics for given arguments def _get_cluster_metrics(self, name, n_clusters_, X, pred_labels, true_labels=None): from sklearn.metrics import homogeneity_score, completeness_score, v_measure_score, adjusted_rand_score, adjusted_mutual_info_score, silhouette_score print("""################## %s metrics #####################""" % name) if len(np.unique(pred_labels)) >= 2: silh_co = silhouette_score(X, pred_labels) if true_labels is not None: h_score = homogeneity_score(true_labels, pred_labels) c_score = completeness_score(true_labels, pred_labels) vm_score = v_measure_score(true_labels, pred_labels) adj_r_score = adjusted_rand_score(true_labels, pred_labels) adj_mut_info_score = adjusted_mutual_info_score(true_labels, pred_labels) metrics = {"Silhouette Coefficient": silh_co, "Estimated number of clusters": n_clusters_, "Homogeneity": h_score, "Completeness": c_score, "V-measure": vm_score, "Adjusted Rand Index": adj_r_score, "Adjusted Mutual Information": adj_mut_info_score} for k, v in metrics.items(): print("t%s: %0.3f" % (k, v)) return metrics metrics = {"Silhouette Coefficient": silh_co, "Estimated number of clusters": n_clusters_} for k, v in metrics.items(): print("t%s: %0.3f" % (k, v)) return metrics else: print("t# of predicted labels is {}, can not produce metrics. n".format(np.unique(pred_labels))) The _get_cluster_metrics method calculates metrics, such as homogeneity_score, completeness_score, v_measure_score, adjusted_rand_score, adjusted_mutual_info_score, and silhouette_score. These metrics will help you to assess how well the clusters are separated and also measure the similarity within and between clusters. K-means algorithm in action You can now apply the KMeans algorithm to see how it works: from sklearn.cluster import KMeans estimators = [{'estimator': KMeans, 'args':(), 'kwargs':{'n_clusters': 4}}] unsupervised_learner = Unsupervised_AutoML(estimators) You can see the estimators: unsupervised_learner.estimators These will output the following: [{'args': (), 'estimator': sklearn.cluster.k_means_.KMeans, 'kwargs': {'n_clusters': 4}}] You can now invoke fit_predict to obtain predictions and performance_metrics: predictions, performance_metrics = unsupervised_learner.fit_predict(X, y) Metrics will be written to the console: ################## KMeans metrics ##################### Silhouette Coefficient: 0.631 Estimated number of clusters: 4.000 Homogeneity: 0.951 Completeness: 0.951 V-measure: 0.951 Adjusted Rand Index: 0.966 Adjusted Mutual Information: 0.950 You can always print metrics later: pprint(performance_metrics) This will output the name of the estimator and its metrics: {'KMeans': {'Silhouette Coefficient': 0.9280431207593165, 'Estimated number of clusters': 4, 'Homogeneity': 1.0, 'Completeness': 1.0, 'V-measure': 1.0, 'Adjusted Rand Index': 1.0, 'Adjusted Mutual Information': 1.0}} Let's add another class method to plot the clusters of the given estimator and predicted labels: # plot_clusters will visualize the clusters given predicted labels def plot_clusters(self, estimator, X, labels, plot_kwargs): palette = sns.color_palette('deep', np.unique(labels).max() + 1) colors = [palette[x] if x >= 0 else (0.0, 0.0, 0.0) for x in labels] plt.scatter(X[:, 0], X[:, 1], c=colors, **plot_kwargs) plt.title('{} Clusters'.format(str(estimator.__name__)), fontsize=14) plt.show() Let's see the usage: plot_kwargs = {'s': 12, 'linewidths': 0.1} unsupervised_learner.plot_clusters(KMeans, X, unsupervised_learner.predictions[0]['KMeans'], plot_kwargs) You get the following plot from the preceding block: In this example, clusters are evenly sized and clearly separate from each other but, when you are doing this kind of exploratory analysis, you should try different hyperparameters and examine the results. You will write a wrapper function later in this article to apply a list of clustering algorithms and their hyperparameters to examine the results. For now, let's see one more example with k-means where it does not work well. When clusters in your dataset have different statistical properties, such as differences in variance, k-means will fail to identify clusters correctly: X, y = make_blobs(n_samples=2000, centers=5, cluster_std=[1.7, 0.6, 0.8, 1.0, 1.2], random_state=220) # Plot sample data plt.scatter(X[:, 0], X[:, 1], **plot_kwargs) plt.show() We get the following plot from the preceding code: Although this sample dataset is generated with five centers, it's not that obvious and there might be four clusters, as well: from sklearn.cluster import KMeans estimators = [{'estimator': KMeans, 'args':(), 'kwargs':{'n_clusters': 4}}] unsupervised_learner = Unsupervised_AutoML(estimators) predictions, performance_metrics = unsupervised_learner.fit_predict(X, y) Metrics in the console are as follows: ################## KMeans metrics ##################### Silhouette Coefficient: 0.549 Estimated number of clusters: 4.000 Homogeneity: 0.729 Completeness: 0.873 V-measure: 0.795 Adjusted Rand Index: 0.702 Adjusted Mutual Information: 0.729 KMeans clusters are plotted as follows: plot_kwargs = {'s': 12, 'linewidths': 0.1} unsupervised_learner.plot_clusters(KMeans, X, unsupervised_learner.predictions[0]['KMeans'], plot_kwargs) We get the following plot from the preceding code: In this example, points between red (dark gray) and bottom-green clusters (light gray) seem to form one big cluster. K-means is calculating the centroid based on the mean value of points surrounding that centroid. Here, you need to have a different approach. The DBSCAN algorithm in action DBSCAN is one of the clustering algorithms that can deal with non-flat geometry and uneven cluster sizes. Let's see what it can do: from sklearn.cluster import DBSCAN estimators = [{'estimator': DBSCAN, 'args':(), 'kwargs':{'eps': 0.5}}] unsupervised_learner = Unsupervised_AutoML(estimators) predictions, performance_metrics = unsupervised_learner.fit_predict(X, y) Metrics in the console are as follows: ################## DBSCAN metrics ##################### Silhouette Coefficient: 0.231 Estimated number of clusters: 12.000 Homogeneity: 0.794 Completeness: 0.800 V-measure: 0.797 Adjusted Rand Index: 0.737 Adjusted Mutual Information: 0.792 DBSCAN clusters are plotted as follows: plot_kwargs = {'s': 12, 'linewidths': 0.1} unsupervised_learner.plot_clusters(DBSCAN, X, unsupervised_learner.predictions[0]['DBSCAN'], plot_kwargs) We get the following plot from the preceding code: Conflict between red (dark gray) and bottom-green (light gray) clusters from the k-means case seems to be gone, but what's interesting here is that some small clusters appeared and some points were not assigned to any cluster based on their distance. DBSCAN has the eps(epsilon) hyperparameter, which is related to proximity for points to be in same neighborhood; you can play with that parameter to see how the algorithm behaves. When you are doing this kind of exploratory analysis where you don't know much about the data, visual clues are always important, because metrics can mislead you since not every clustering algorithm can be assessed using similar metrics. To summarize we learned about many different aspects when it comes to choosing a suitable ML pipeline for a given problem. You gained a better understanding of how unsupervised algorithms may suit your needs for a given problem. To have a clearer understanding of the different aspects of automated Machine Learning, and how to incorporate automation tasks using practical datasets, check out this book Hands-On Automated Machine Learning. Read more Google announces Cloud TPUs on the Cloud Machine Learning Engine (ML Engine) How machine learning as a service is transforming cloud Selecting Statistical-based Features in Machine Learning application
Read more
  • 0
  • 0
  • 24434

article-image-build-java-ee-containers-using-docker-tutorial
Aaron Lazar
07 Aug 2018
7 min read
Save for later

Build Java EE containers using Docker [Tutorial]

Aaron Lazar
07 Aug 2018
7 min read
Containers are changing the way we build and deliver software. They are also the essential glue for DevOps and the way to take CI/CD to another level. Put them together and you will have one of the most powerful environments in IT. But can Java EE take advantage of it? Of course! If an application server is an abstraction of Java EE applications, containers are an abstraction of the server, and once you have them built into a standard such as Docker, you have the power to use such tools to manage an application server. This article is an extract from the book Java EE 8 Cookbook, authored by Elder Moraes. This article will show you how to put your Java EE application inside a container. Since day one, Java EE has been based on containers. If you doubt it, just have a look at this diagram: Java EE architecture: https://docs.oracle.com/javaee/6/tutorial/doc/bnacj.html It belongs to Oracle's official documentation for Java EE 6 and, actually, has been much the same architecture since the times of Sun. If you pay attention, you will notice that there are different containers: a web container, an EJB container, and an application client container. In this architecture, it means that the applications developed with those APIs will rely on many features and services provided by the container. When we take the Java EE application server and put it inside a Docker container, we are doing the same thing— it is relying on some of the features and services provided by the Docker environment. This recipe will show you how to deliver a Java EE application in a container bundle, which is called an appliance. Installing Docker First, of course, you need the Docker platform installed in your environment. There are plenty of options, so I suggest you check this link and get more details: And if you are not familiar with Docker commands, I recommend you have a look at this beautiful cheat sheet: You'll also need to create an account at Docker Hub so you can store your own images. Check it out. It's free for public images. Building Java EE Container To build your Java EE container, you'll first need a Docker image. To build it, you'll need a Dockerfile such as this: FROM openjdk:8-jdk ENV GLASSFISH_HOME /usr/local/glassfish ENV PATH ${GLASSFISH_HOME}/bin:$PATH ENV GLASSFISH_PKG latest-glassfish.zip ENV GLASSFISH_URL https://download.oracle.com/glassfish/5.0/nightly/latest-glassfish.zip RUN mkdir -p ${GLASSFISH_HOME} WORKDIR ${GLASSFISH_HOME} RUN set -x && curl -fSL ${GLASSFISH_URL} -o ${GLASSFISH_PKG} && unzip -o $GLASSFISH_PKG && rm -f $GLASSFISH_PKG && mv glassfish5/* ${GLASSFISH_HOME} && rm -Rf glassfish5 RUN addgroup glassfish_grp && adduser --system glassfish && usermod -G glassfish_grp glassfish && chown -R glassfish:glassfish_grp ${GLASSFISH_HOME} && chmod -R 777 ${GLASSFISH_HOME} COPY docker-entrypoint.sh / RUN chmod +x /docker-entrypoint.sh USER glassfish ENTRYPOINT ["/docker-entrypoint.sh"] EXPOSE 4848 8080 8181 CMD ["asadmin", "start-domain", "-v"] This image will be our base image from which we will construct other images in this chapter. Now we need to build it: docker build -t eldermoraes/gf-javaee-jdk8 . Go ahead and push it to your Docker Registry at Docker Hub: docker push eldermoraes/gf-javaee-jdk8 Now you can create another image by customizing the previous one, and then put your app on it: FROM eldermoraes/gf-javaee-jdk8 ENV DEPLOYMENT_DIR ${GLASSFISH_HOME}/glassfish/domains/domain1/autodeploy/ COPY app.war ${DEPLOYMENT_DIR} In the same folder, we have a Java EE application file (app.war) that will be deployed inside the container. Check the See also section to download all the files. Once you save your Dockerfile, you can build your image: docker build -t eldermoraes/gf-javaee-cookbook . Now you can create the container: docker run -d --name gf-javaee-cookbook -h gf-javaee-cookbook -p 80:8080 -p 4848:4848 -p 8686:8686 -p 8009:8009 -p 8181:8181 eldermoraes/gf-javaee-cookbook Wait a few seconds and open this URL in your browser: http://localhost/app How to work with Dockerfile Let's understand our first Dockerfile: FROM openjdk:8-jdk This FROM keyword will ask Docker to pull the openjdk:8-jdk image, but what does it mean? It means that there's a registry somewhere where your Docker will find prebuilt images. If there's no image registry in your local environment, it will search for it in Docker Hub, the official and public Docker registry in the cloud. And when you say that you are using a pre-built image, it means that you don't need to build, in our case, the whole Linux container from scratch. There's already a template that you can rely on: ENV GLASSFISH_HOME /usr/local/glassfish ENV PATH ${GLASSFISH_HOME}/bin:$PATH ENV GLASSFISH_PKG latest-glassfish.zip ENV GLASSFISH_URL https://download.oracle.com/glassfish/5.0/nightly/latest-glassfish.zip RUN mkdir -p ${GLASSFISH_HOME} WORKDIR ${GLASSFISH_HOME} Here are just some environment variables to help with the coding. RUN set -x && curl -fSL ${GLASSFISH_URL} -o ${GLASSFISH_PKG} && unzip -o $GLASSFISH_PKG && rm -f $GLASSFISH_PKG && mv glassfish5/* ${GLASSFISH_HOME} && rm -Rf glassfish5 The RUN clause in Dockerfiles execute some bash commands inside the container when it has been created. Basically, what is happening here is that GlassFish is being downloaded and then prepared in the container: RUN addgroup glassfish_grp && adduser --system glassfish && usermod -G glassfish_grp glassfish && chown -R glassfish:glassfish_grp ${GLASSFISH_HOME} && chmod -R 777 ${GLASSFISH_HOME} For safety, we define the user that will hold the permissions for GlassFish files and processes: COPY docker-entrypoint.sh / RUN chmod +x /docker-entrypoint.sh Here we are including a bash script inside the container to perform some GlassFish administrative tasks: #!/bin/bash if [[ -z $ADMIN_PASSWORD ]]; then ADMIN_PASSWORD=$(date| md5sum | fold -w 8 | head -n 1) echo "##########GENERATED ADMIN PASSWORD: $ADMIN_PASSWORD ##########" fi echo "AS_ADMIN_PASSWORD=" > /tmp/glassfishpwd echo "AS_ADMIN_NEWPASSWORD=${ADMIN_PASSWORD}" >> /tmp/glassfishpwd asadmin --user=admin --passwordfile=/tmp/glassfishpwd change-admin-password --domain_name domain1 asadmin start-domain echo "AS_ADMIN_PASSWORD=${ADMIN_PASSWORD}" > /tmp/glassfishpwd asadmin --user=admin --passwordfile=/tmp/glassfishpwd enable-secure-admin asadmin --user=admin stop-domain rm /tmp/glassfishpwd exec "$@" After copying the bash file into the container, we go to the final block: USER glassfish ENTRYPOINT ["/docker-entrypoint.sh"] EXPOSE 4848 8080 8181 CMD ["asadmin", "start-domain", "-v"] The USER clause defines the user that will be used from this point in the file. It's great because from there, all the tasks will be done by the glassfish user. The ENTRYPOINT clause will execute the docker-entrypoint.sh script. The EXPOSE clause will define the ports that will be available for containers that use this image. And finally, the CMD clause will call the GlassFish script that will initialize the container. Now let's understand our second Dockerfile: FROM eldermoraes/gf-javaee-jdk8 We need to take into account the same considerations about the prebuilt image, but now the image was made by you. Congratulations! ENV DEPLOYMENT_DIR ${GLASSFISH_HOME}/glassfish/domains/domain1/autodeploy/ Here, we are building an environment variable to help with the deployment. It's done in the same way as for Linux systems: COPY app.war ${DEPLOYMENT_DIR} This COPY command will literally copy the app.war file to the folder defined in the DEPLOYMENT_DIR environment variable. From here, you are ready to build an image and create a container. The image builder is self-explanatory: docker build -t eldermoraes/gf-javaee-cookbook . Let's check the docker run command: docker run -d --name gf-javaee-cookbook -h gf-javaee-cookbook -p 80:8080 -p 4848:4848 -p 8686:8686 -p 8009:8009 -p 8181:8181 eldermoraes/gf-javaee-cookbook If we break it down, this is what the various elements of the command mean: -h: Defines the host name of the container. -p: Defines which ports will be exposed and how it will be done. It is useful, for example, when more than one container is using the same port by default—you just use them differently. eldermoraes/gf-javaee-cookbook: The reference to the image you just built. So now you've successfully built a container for your Java EE application, in Docker. If you found this tutorial helpful and would like to learn more, head over to the Packt store and get the book Java EE 8 Cookbook, authored by Elder Moraes. Oracle announces a new pricing structure for Java Design a RESTful web API with Java [Tutorial] How to convert Java code into Kotlin
Read more
  • 0
  • 0
  • 53120
article-image-gradle-build-script-to-start-automating-project
Savia Lobo
06 Aug 2018
15 min read
Save for later

Write your first Gradle build script to start automating your project [Tutorial]

Savia Lobo
06 Aug 2018
15 min read
When we develop a software, we write, compile, test, package, and finally, distribute the code. We can automate these steps by using a build system. The big advantage is that we have a repeatable sequence of steps. The build system will always follow the steps that we have defined, so we can concentrate on writing the actual code and don't have to worry about the other steps. Gradle is one such build system. It is a tool for build automation. With Gradle, we can automate compiling, testing, packaging, and deployment of our software or any other types of projects. Gradle is flexible, but has sensible defaults for most projects. This means that we can rely on the defaults if we don't want something special, but we can still can use the flexibility to adapt a build to certain custom needs. Gradle is already used by large open source projects such as Spring, Hibernate, and Grails. Enterprise companies such as LinkedIn and Netflix also use Gradle. In this article, we will explain what Gradle is and how to use it in our development projects. This Gradle tutorial is an excerpt taken from, 'Gradle Effective Implementations Guide - Second Edition' written by Hubert Klein Ikkink. Take a look at some of Gradle features. Declarative builds and convention over configuration Gradle uses a Domain Specific Language (DSL) based on Groovy to declare builds. The DSL provides a flexible language that can be extended by us. As the DSL is based on Groovy, we can write Groovy code to describe a build and use the power and expressiveness of the Groovy language. Groovy is a language for the Java Virtual Machine (JVM), such as Java and Scala. Groovy makes it easy to work with collections, has closures, and a lot of useful features. The syntax is closely related to the Java syntax. In fact, we could write a Groovy class file with Java syntax and it will compile. However, using the Groovy syntax makes it easier to express the code intent and we need less boilerplate code than with Java. To get the most out of Gradle, it is best to also learn the basics of the Groovy language, but it is not necessary to start writing Gradle scripts. Gradle is designed to be a build language and not a rigid framework. The Gradle core itself is written in Java and Groovy. To extend Gradle, we can use Java and Groovy to write our custom code. We can even write our custom code in Scala if we want to. Gradle provides support for Java, Groovy, Scala, web, and OSGi projects out of the box. These projects have sensible convention-over-configuration settings that we probably already use ourselves. However, we have the flexibility to change these configuration settings if required for our projects. Support for Ant Tasks and Maven repositories Gradle supports Ant Tasks and projects. We can import an Ant build and reuse all the tasks. However, we can also write Gradle tasks dependent on Ant Tasks. The integration also applies for properties, paths, and so on. Maven and Ivy repositories are supported to publish or fetch dependencies. So, we can continue to use any repository infrastructure that we already have. Incremental builds in Gradle With Gradle, we have incremental builds. This means the tasks in a build are only executed if necessary. For example, a task to compile source code will first check whether the sources have changed since the last execution of the task. If the sources have changed, the task is executed; but if the sources haven't changed, the execution of the task is skipped and the task is marked as being up to date. Gradle supports this mechanism for a lot of provided tasks. However, we can also use this for tasks that we write ourselves. Multi-project builds Gradle has great support for multi-project builds. A project can simply be dependent on other projects or be a dependency of other projects. We can define a graph of dependencies among projects, and Gradle can resolve these dependencies for us. We have the flexibility to define our project layout as we want. Gradle has support for partial builds. This means that Gradle will figure out whether a project, which our project depends on, needs to be rebuild or not. If the project needs rebuilding, Gradle will do this before building our own project. Gradle Wrapper The Gradle Wrapper allows us to execute Gradle builds even if Gradle is not installed on a computer. This is a great way to distribute source code and provide the build system with it so that the source code can be built. Also in an enterprise environment, we can have a zero-administration way for client computers to build the software. We can use the wrapper to enforce a certain Gradle version to be used so that the whole team is using the same version. We can also update the Gradle version for the wrapper, and the whole team will use the newer version as the wrapper code is checked in to version control. Free and open source Gradle is an open source project and it is licensed under the Apache License (ASL). Getting started with Gradle In this section, we will download and install Gradle before writing our first Gradle build script. Before we install Gradle, we must make sure that we have a Java Development SE Kit (JDK) installed on our computer. Gradle requires JDK 6 or higher. Gradle will use the JDK found on the path of our computer. We can check this by running the following command on the command line: $ java -version Although Gradle uses Groovy, we don't have to install Groovy ourselves. Gradle bundles the Groovy libraries with the distribution and will ignore a Groovy installation that is already available on our computer. Gradle is available on the Gradle website at http://www.gradle.org/downloads. From this page, we can download the latest release of Gradle. We can also download an older version if we want. We can choose among three different distributions to download. We can download the complete Gradle distribution with binaries, sources, and documentation; or we can only download the binaries; or we can only download the sources. To get started with Gradle, we will download the standard distribution with the binaries, sources, and documentation. At the time of writing this book, the current release is 2.12. Installing Gradle Gradle is packaged as a ZIP file for one of the three distributions. So when we have downloaded the Gradle full-distribution ZIP file, we must unzip the file. After unpacking the ZIP file we have: Binaries in the bin directory Documentation with the user guide, Groovy DSL, and API documentation in the doc directory A lot of samples in the samples directory Source code of Gradle in the src directory Supporting libraries for Gradle in the lib directory A directory named init.d, where we can store Gradle scripts that need to be executed each time we run Gradle Once we have unpacked the Gradle distribution to a directory, we can open a command prompt. We go to the directory where we have installed Gradle. To check our installation, we run gradle -v and get an output with the used JDK and library versions of Gradle, as follows: $ gradle -v ------------------------------------------------------------ Gradle 2.12 ------------------------------------------------------------ Build time: 2016-03-14 08:32:03 UTC Build number: none Revision: b29fbb64ad6b068cb3f05f7e40dc670472129bc0 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December23 2013 JVM: 1.8.0_66 (Oracle Corporation 25.66-b17) OS: Mac OS X 10.11.3 x86_64 Here, we can check whether the displayed version is the same as the distribution version that we have downloaded from the Gradle website. To run Gradle on our computer, we have to only add $GRADLE_HOME/bin to our PATH environment variable. Once we have done that, we can run the gradle command from every directory on our computer. If we want to add JVM options to Gradle, we can use the JAVA_OPTS and GRADLE_OPTS environment variables. JAVA_OPTS is a commonly used environment variable name to pass extra parameters to a Java application. Gradle also uses the GRADLE_OPTS environment variable to pass extra arguments to Gradle. Both environment variables are used, so we can even set them both with different values. This is mostly used to set, for example, an HTTP proxy or extra memory options. Installing with SKDMAN! Software Development Kit Manager (SDKMAN!) is a tool to manage versions of software development kits such as Gradle. Once we have installed SKDMAN!, we can simply use the install command and SDKMAN! downloads Gradle and makes sure that it is added to our $PATH variable. SDKMAN! is available for Unix-like systems, such as Linux, Mac OSX, and Cygwin (on Windows). First, we need to install SDKMAN! with the following command in our shell: $ curl -s get.sdkman.io | bash Next, we can install Gradle with the install command: $ sdk install gradle Downloading: gradle 2.12 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 354 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 42.6M 100 42.6M 0 0 1982k 0 0:00:22 0:00:22 --:--:-- 3872k Installing: gradle 2.12 Done installing! Do you want gradle 2.12 to be set as default? (Y/n): Y Setting gradle 2.12 as default. If we have multiple versions of Gradle, it is very easy to switch between versions with the use command: $ sdk use gradle 2.12 Using gradle version 2.12 in this shell. Writing our first build script We now have a running Gradle installation. It is time to create our first Gradle build script. Gradle uses the concept of projects to define a related set of tasks. A Gradle build can have one or more projects. A project is a very broad concept in Gradle, but it is mostly a set of components that we want to build for our application. A project has one or more tasks. Tasks are a unit of work that need to be executed by the build. Examples of tasks are compiling source code, packaging class files into a JAR file, running tests, and deploying the application. We now know a task is a part of a project, so to create our first task, we also create our first Gradle project. We use the gradle command to run a build. Gradle will look for a file named build.gradle in the current directory. This file is the build script for our project. We define our tasks that need to be executed in this build script file. We create a new build.gradle file and open this in a text editor. We type the following code to define our first Gradle task: task helloWorld << { println 'Hello world.' } With this code, we will define a helloWorld task. The task will print the words Hello world. to the console. The println is a Groovy method to print text to the console and is basically a shorthand version of the System.out.println Java method. The code between the brackets is a closure. A closure is a code block that can be assigned to a variable or passed to a method. Java doesn't support closures, but Groovy does. As Gradle uses Groovy to define the build scripts, we can use closures in our build scripts. The << syntax is, technically speaking, an operator shorthand for the leftShift()method, which actually means add to. Therefore, here we are defining that we want to add the closure (with the println 'Hello world' statement) to our task with the helloWorld name. First, we save build.gradle, and with the gradle helloWorld command, we execute our build: $ gradle helloWorld :helloWorld Hello world. BUILD SUCCESSFUL Total time: 2.384 secs This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.12/userguide/gradle_daemon.html The first line of output shows our line Hello world. Gradle adds some more output such as the fact that the build was successful and the total time of the build. As Gradle runs in the JVM, every time we run a Gradle build, the JVM must be also started. The last line of the output shows a tip that we can use the Gradle daemon to run our builds. The Gradle daemon keeps Gradle running in memory so that we don't get the penalty of starting the JVM each time we run Gradle. This drastically speeds up the execution of tasks. We can run the same build again, but only with the output of our task using the Gradle --quiet or -q command-line option. Gradle will suppress all messages except error messages. When we use the --quiet (or -q) option, we get the following output: $ gradle --quiet helloWorld Hello world. Understanding the Gradle graphical user interface Finally, we take a look at the --gui command-line option. With this option, we start a graphical shell for our Gradle builds. Until now, we used the command line to start a task. With the Gradle GUI, we have a graphical overview of the tasks in a project and we can execute them by simply clicking on the mouse. To start the GUI, we invoke the following command: $ gradle --gui A window is opened with a graphical overview of our task tree. We only have one task that one is shown in the task tree, as we can see in the following screenshot: The output of running a task is shown at the bottom of the window. When we start the GUI for the first time, the tasks task is executed and we see the output in the window. Task tree The Task Tree tab shows projects and tasks found in our build project. We can execute a task by double-clicking on the task name. By default, all the tasks are shown, but we can apply a filter to show or hide certain projects and tasks. The Edit filter button opens a new dialog window where we can define the tasks and properties that are a part of the filter. The Toggle filter button makes the filter active or inactive. We can also right-click on the project and task names. This opens a context menu where we can choose to execute the task, add it to the favorites, hide it (adds it to the filter), or edit the build file. If we have associated the .gradle extension to a text editor in our operating system, then the editor is opened with the content of the build script. These options can be seen in the following screenshot: Favorites The Favorites tab stores tasks we want to execute regularly. We can add a task by right-clicking on the task in the Task Tree tab and selecting the Add To Favorites menu option, or if we have opened the Favorites tab, we can select the Add button and manually enter the project and task name that we want to add to our favorites list. We can see the Add Favorite dialog window in the following screenshot: Command line On the Command Line tab, we can enter any Gradle command that we normally would enter on the command prompt. The command can be added to Favorites as well. We see the Command Line tab contents in the following image: Setup The last tab is the Setup tab. Here, we can change the project directory, which is set to the current directory by default. With the GUI, we can select the logging level from the Log Level select box with the different log levels. We can choose to debug, info, Lifecycle, and error as log levels. The error log level only shows errors and is the least verbose, while debug is the most verbose log level. The lifecycle log level is the default log level. Here, we also can set how detailed the exception stack trace information should be. In the Stack Trace Output section, we can choose from the following three options: Exceptions Only: This is for only showing the exceptions when they occur, which is the default value Standard Stack Trace (-s): This is for showing more stack trace information for the exceptions -S): This is for the most verbose stack trace information for exceptions If we enable the Only Show Output When Error Occurs option, then we only get output from the build process if the build fails. Otherwise, we don't get any output. Finally, we can define a different way to start Gradle for the build with the Use Custom Gradle Executor option. For example, we can define a different batch or script file with extra setup information to run the build process. The following screenshot shows the Setup tab page and all the options that we can set: We learned to install Gradle on our computers and write our first Gradle build script with a simple task.  We also looked at the Gradle GUI and how we can use it to run Gradle build scripts. If you've enjoyed this post, do check out this book 'Gradle Effective Implementations Guide - Second Edition'to know more about how to use Grade for Java Projects. That ’70s language: AWK programming 26 new Java 9 enhancements you will love Slow down to learn how to code faster
Read more
  • 0
  • 0
  • 11836

article-image-statistical-tools-in-wireshark-for-packet-analysis
Vijin Boricha
06 Aug 2018
9 min read
Save for later

Using statistical tools in Wireshark for packet analysis [Tutorial]

Vijin Boricha
06 Aug 2018
9 min read
One of Wireshark's strengths is its statistical tools. When using Wireshark, we have various types of tools, starting from the simple tools for listing end-nodes and conversations, to the more sophisticated tools such as flow and I/O graphs. In this article, we will look at the simple tools in Wireshark that provide us with basic network statistics i.e; who talks to whom over the network, what are the chatty devices, what packet sizes run over the network, and so on. To start statistics tools, start Wireshark, and choose Statistics from the main menu. This article is an excerpt from Network Analysis using Wireshark 2 Cookbook - Second Edition written by Nagendra Kumar Nainar, Yogesh Ramdoss, Yoram Orzach. Using the statistics for capture file properties menu In this recipe, we will learn how to get general information from the data that runs over the network. The capture file properties in Wireshark 2 replaces the summary menu in Wireshark 1. Start Wireshark, click on Statistics. How to do it... From the Statistics menu, choose Capture File Properties: What you will get is the Capture File Properties window (displayed in the following screenshot). As you can see in the following screenshot, we have the following: File: Provides file data, such as filename and path, length, and so on Time: Start time, end time, and duration of capture Capture: Hardware information for the PC that Wireshark is installed on Interfaces: Interface information—the interface registry identifier on the left, if capture filter is turned on, interface type and packet size limit Statistics: General capture statistics, including captured and displayed packets: How it works... This menu simply gives a summary of the filtered data properties and the capture statistics (average packets or bytes per second) when someone wants to learn the capture statistics. Using the statistics for protocol hierarchy menu In this recipe, we will learn how to get protocol hierarchy information of the data that runs over the network. Start Wireshark, click on Statistics. How to do it... From the Statistics menu, choose Protocol Hierarchy: What you will get is data about the protocol distribution in the captured file. You will get the protocol distribution of the captured data. The partial screenshot displayed here depicts the statistics of packets captured on a per-protocol basis: What you will get is the Protocol Hierarchy window: Protocol: The protocol name Percent Packets: The percentage of protocol packets from the total captured packets Packets: The number of protocol packets from the total captured packets Percent Bytes: The percentage of protocol bytes from the total captured packets Bytes: The number of protocol bytes from the total captured packets Bit/s: The bandwidth of this protocol, in relation to the capture time End Packets: The absolute number of packets of this protocol (for the highest protocol in the decode file) End Bytes: The absolute number of bytes of this protocol (for the highest protocol in the decode file) End Bit/s: The bandwidth of this protocol, relative to the capture packets and time (for the highest protocol in the decode file) The end columns counts when the protocol is the last protocol in the packet (that is, when the protocol comes at the end of the frame). These can be TCP packets with no payload (for example, SYN packets) which carry upper layer protocols. That is why you see a zero count for Ethernet, IPv4, and UDP end packets; there are no frames where those protocols are the last protocol in the frame. In this file example, we can see two interesting issues: We can see 1,842 packets of DHCPv6. If IPv6 and DHCPv6 are not required, disable it. We see more than 200,000 checkpoint high availability (CPHA) packets, 74.7% of which are sent over the network we monitored. These are synchronization packets that are sent between two firewalls working in a cluster, updating session tables between the firewalls. Such an amount of packets can severely influence performance. The solution for this problem is to configure a dedicated link between the firewalls so that session tables will not influence the network. How it works... Simply, it calculates statistics over the captured data. Some important things to notice: The percentage always refers to the same layer protocols. For example, in the following screenshot, we see that logical link control has 0.5% of the packets that run over Ethernet, IPv6 has 1.0%, IPv4 has 88.8% of the packets, ARP has 9.6% of the packets and even the old Cisco ISK has 0.1 %—a total of 100 % of the protocols over layer 2 Ethernet. On the other hand, we see that TCP has 75.70% of the data, and inside TCP, only 12.74% of the packets are HTTP, and that is almost it. This is because Wireshark counts only the packets with the HTTP headers. It doesn't count, for example, the ACK packets, data packets, and so on: Using the statistics for conversations menu In this recipe, we will learn how to get conversation information of the data that runs over the network. Start Wireshark, click on Statistics. How to do it... From the Statistics menu, choose Conversations: The following window will come up: You can choose between layer 2 Ethernet statistics, layer 3 IP statistics, or layer 4 TCP or UDP statistics. You can use this statistics tools for: On layer 2 (Ethernet): To find and isolate broadcast storms On layer 3/layer 4 (TCP/IP): To connect in parallel to the internet router port, and check who is loading the line to the ISP If you see that there is a lot of traffic going out to port 80 (HTTP) on a specific IP address on the internet, you just have to copy the address to your browser and find the website that is most popular with your users. If you don't get anything, simply go to a standard DNS resolution website (search Google for DNS lookup) and find out what is loading your internet line. For viewing IP addresses as names, you can check the Name resolution checkbox for name resolution (1 in the previous screenshot). For seeing the name resolution, you will first have to enable it by choosing View | Name Resolution | Enable for Network layer. You can also limit the conversations statistics to a display filter by checking the Limit to display filter checkbox (2). In this way, statistics will be presented on all the packets passing the display filter. A new feature in Wireshark version 2 is the graph feature, marked as (5) in the previous screenshot. When you choose a specific line in the TCP conversations statistics and click Graph..., it brings you to the TCP time/sequence (tcptrace) stream graph. To copy table data, click on the Copy button (3). In TCP or UDP, you can mark a specific line, and then click on the Follow Stream... button (4). This will define a display filter that will show you the specific stream of data. As you can see in the following screenshot, you can also right-click a line and choose to prepare or apply a filter, or to colorize a data stream: We also see that, unlike the previous Wireshark version, in which we saw all types of protocols in the upper tabs, here we can choose which protocols to see when only the identified protocols are presented by default. How it works... A network conversation is the traffic between two specific endpoints. For example, an IP conversation is all the traffic between two IP addresses, and TCP conversations present all TCP connections. Using the statistics for endpoints menu In this recipe, we will learn how to get endpoint statistics information of the captured data. Start Wireshark and click on Statistics. How to do it... To view the endpoint statistics, follow these steps: From the Statistics menu, choose Endpoints: The following window will come up: In this window, you will be able to see layer 2, 3, and 4 endpoints, which is Ethernet, IP, and TCP or UDP. From the left-hand side of the window you can see (here is an example for the TCP tab): Endpoint IP address and port number on this host Total packets sent, and bytes received from and to this host Packets to the host (Packets A → B) and bytes to host (Bytes A → B) Packets to the host (Packets B → A) and bytes to host (Bytes B → A) The Latitude and Longitude columns applicable with the GeoIP configured At the bottom of the window we have the following checkboxes: Name resolution: Provide name resolution in cases where it is configured in the name resolution under the view menu. Limit to display filter: To show statistics only for the display filter configured on the main window. Copy: Copy the list values to the clipboard in CSV or YAML format. Map: In cases where GeoIP is configured, shows the geographic information on the geographical map. How it works... Quite simply, it gives statistics on all the endpoints Wireshark has discovered. It can be any situation, such as the following: Few Ethernet (even on) end nodes (that is, MAC addresses), with many IP end nodes (that is, IP addresses)—this will be the case where, for example, we have a router that sends/receives packets from many remote devices. Few IP end nodes with many TCP end nodes—this will be the case for many TCP connections per host. Can be a regular operation of a server with many connections, and it could also be a kind of attack that comes through the network (SYN attack). We learned about Wireshark's basic statistic tools and how you can leverage those for network analysis. Get over 100 recipes to analyze and troubleshoot network problems using Wireshark 2 from this book Network Analysis using Wireshark 2 Cookbook - Second Edition. What’s new in Wireshark 2.6 ? Wireshark for analyzing issues & malicious emails in POP, IMAP, and SMTP  [Tutorial] Capturing Wireshark Packets
Read more
  • 0
  • 5
  • 71406
Modal Close icon
Modal Close icon