Welcome to an introductory tutorial into Data Science with Python. I will cover the basics of how to use Numpy, Pandas, Scikit-Learn, and Keras.

Used for multidimensional array manipulation. Arrays are used to store data and Numpy is used to manipulate the data.

Used to analyze data using DataFrames. DataFrames contain Series which contains Scalars. Pandas allows you to fill in missing data, find statistics, and analyze your data.

Used for Data Mining and Data Analysis. Very helpful to use shallow learning algorithms on data.

A High Level API for Tensorflow. Used to make using Tensorflow easy. Recently, a lot of Tensorflow support has emerged for Keras.

I think there's no better introduction to Numpy than the tutorial made by Justin Johnson for CS231n at Stanford. The following Numpy tutorial is adapted directly from his work, available here: http://cs231n.github.io/python-numpy-tutorial/

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the *rank* of the array; the *shape* of an array is a tuple of integers giving the size of the array along each dimension.

We can initialize numpy arrays from nested Python lists, and access elements using square brackets:

Numpy also provides many functions to create arrays:

You can read about other methods of array creation in the documentation.

Numpy offers several ways to index into arrays.

**Slicing:** Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank than the original array. Note that this is quite different from the way that MATLAB handles array slicing:

**Integer array indexing:** When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. Here is an example:

One useful trick with integer array indexing is selecting or mutating one element from each row of a matrix:

**Boolean array indexing:** Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition. Here is an example:

For brevity we have left out a lot of details about numpy array indexing; if you want to know more you should read the documentation.

Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype. Here is an example:

You can read all about numpy datatypes in the documentation.

Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module:

Note that unlike MATLAB, `*`

is elementwise multiplication, not matrix multiplication. We instead use the `dot`

function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices. `dot`

is available both as a function in the numpy module and as an instance method of array objects:

Numpy provides many useful functions for performing computations on arrays; one of the most useful is `sum`

:

You can find the full list of mathematical functions provided by numpy in the documentation.

Apart from computing mathematical functions using arrays, we frequently need to reshape or otherwise manipulate data in arrays. The simplest example of this type of operation is transposing a matrix; to transpose a matrix, simply use the `T`

attribute of an array object:

Numpy provides many more functions for manipulating arrays; you can see the full list in the documentation.

Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array.

For example, suppose that we want to add a constant vector to each row of a matrix. We could do it like this:

This works; however when the matrix `x`

is very large, computing an explicit loop in Python could be slow. Note that adding the vector `v`

to each row of the matrix `x`

is equivalent to forming a matrix `vv`

by stacking multiple copies of `v`

vertically, then performing elementwise summation of `x`

and `vv`

. We could implement this approach like this:

Numpy broadcasting allows us to perform this computation without actually creating multiple copies of `v`

. Consider this version, using broadcasting:

The line `y = x + v`

works even though `x`

has shape `(4, 3)`

and `v`

has shape `(3,)`

due to broadcasting; this line works as if `v`

actually had shape `(4, 3)`

, where each row was a copy of `v`

, and the sum was performed elementwise.

Broadcasting two arrays together follows these rules:

- If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The two arrays are said to be
*compatible*in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension. - The arrays can be broadcast together if they are compatible in all dimensions.
- After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension

If this explanation does not make sense, try reading the explanation from the documentation or this explanation.

Functions that support broadcasting are known as *universal functions*. You can find the list of all universal functions in the documentation.

Here are some applications of broadcasting:

Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible.

This brief overview has touched on many of the important things that you need to know about numpy, but is far from complete. Check out the numpy reference to find out much more about numpy.

Numpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. SciPy builds on this, and provides a large number of functions that operate on numpy arrays and are useful for different types of scientific and engineering applications.

The best way to get familiar with SciPy is to browse the documentation. We will highlight some parts of SciPy that you might find useful for this class.

SciPy provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images. Here is a simple example that showcases these functions:

Left: The original image. Right: The tinted and resized image.

The functions `scipy.io.loadmat`

and `scipy.io.savemat`

allow you to read and write MATLAB files. You can read about them in the documentation.

SciPy defines some useful functions for computing distances between sets of points.

The function `scipy.spatial.distance.pdist`

computes the distance between all pairs of points in a given set:

You can read all the details about this function in the documentation.

A similar function (`scipy.spatial.distance.cdist`

) computes the distance between all pairs across two sets of points; you can read about it in the documentation.

Matplotlib is a plotting library. In this section give a brief introduction to the `matplotlib.pyplot`

module, which provides a plotting system similar to that of MATLAB.

The most important function in matplotlib is `plot`

, which allows you to plot 2D data. Here is a simple example:

Running this code produces the following plot:

With just a little bit of extra work we can easily plot multiple lines at once, and add a title, legend, and axis labels:

You can read much more about the `plot`

function in the documentation.

You can plot different things in the same figure using the `subplot`

function. Here is an example:

You can read much more about the `subplot`

function in the documentation.

You can use the `imshow`

function to show images. Here is an example:

As you can tell, many of the other concepts/tutorials are missing.

This was caused by a fatal server error on 8/23/2018 for various reasons.

The rest of the content on this blog post as well as others will be fixed in the upcoming weeks.

Surya Dantuluri writes articles on Machine Learning, Full Stack Development, and Insightful Topics