<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 2A.ML101.1
Step1:  Numpy Arrays
Manipulating numpy arrays is an important part of doing machine learning
(or, really, any type of scientific computation) in Python.  This will likely
be review for most
Step2:  There is much, much more to know, but these few operations are fundamental to what we'll
do during this tutorial.
Scipy Sparse Matrices
We won't make very much use of these in this tutorial, but sparse matrices are very nice
in some situations.  For example, in some machine learning tasks, especially those associated
with textual analysis, the data may be mostly zeros.  Storing all these zeros is very
inefficient.  We can create and manipulate sparse matrices as follows
Step3:  Matplotlib
Another important part of machine learning is visualization of data.  The most common
tool for this in Python is matplotlib.  It is an extremely flexible package, but
we will go over some basics here.
First, something special to IPython notebook.  We can turn on the "IPython inline" mode,
which will make plots show up inline in the notebook.
Step4:  There are many, many more plot types available.  One useful way to explore these is by
looking at the matplotlib gallery
<ASSISTANT_TASK:>
Python Code:
# Start pylab inline mode, so figures will appear in the notebook
%matplotlib inline
Explanation: 2A.ML101.1: Introduction to data manipulation with scientific Python
In this section we'll go through the basics of the scientific Python stack for data manipulation: using numpy and matplotlib.
Source: Course on machine learning with scikit-learn by Gaël Varoquaux
You can skip this section if you already know the scipy stack.
To learn the scientific Python ecosystem: http://scipy-lectures.org
End of explanation
import numpy as np
# Generating a random array
X = np.random.random((3, 5))  # a 3 x 5 array
print(X)
# Accessing elements
# get a single element
print(X[0, 0])
# get a row
print(X[1])
# get a column
print(X[:, 1])
# Transposing an array
print(X.T)
# Turning a row vector into a column vector
y = np.linspace(0, 12, 5)
print(y)
# make into a column vector
print(y[:, np.newaxis])
Explanation: Numpy Arrays
Manipulating numpy arrays is an important part of doing machine learning
(or, really, any type of scientific computation) in Python.  This will likely
be review for most: we'll quickly go through some of the most important features.
End of explanation
from scipy import sparse
# Create a random array with a lot of zeros
X = np.random.random((10, 5))
print(X)
# set the majority of elements to zero
X[X < 0.7] = 0
print(X)
# turn X into a csr (Compressed-Sparse-Row) matrix
X_csr = sparse.csr_matrix(X)
print(X_csr)
# convert the sparse matrix to a dense array
print(X_csr.toarray())
Explanation: There is much, much more to know, but these few operations are fundamental to what we'll
do during this tutorial.
Scipy Sparse Matrices
We won't make very much use of these in this tutorial, but sparse matrices are very nice
in some situations.  For example, in some machine learning tasks, especially those associated
with textual analysis, the data may be mostly zeros.  Storing all these zeros is very
inefficient.  We can create and manipulate sparse matrices as follows:
End of explanation
%matplotlib inline
# Here we import the plotting functions
import matplotlib.pyplot as plt
# plotting a line
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x));
# scatter-plot points
x = np.random.normal(size=500)
y = np.random.normal(size=500)
plt.scatter(x, y);
# showing images
x = np.linspace(1, 12, 100)
y = x[:, np.newaxis]
im = y * np.sin(x) * np.cos(y)
print(im.shape)
# imshow - note that origin is at the top-left by default!
plt.imshow(im);
# Contour plot - note that origin here is at the bottom-left by default!
plt.contour(im);
Explanation: Matplotlib
Another important part of machine learning is visualization of data.  The most common
tool for this in Python is matplotlib.  It is an extremely flexible package, but
we will go over some basics here.
First, something special to IPython notebook.  We can turn on the "IPython inline" mode,
which will make plots show up inline in the notebook.
End of explanation
# %load http://matplotlib.org/mpl_examples/pylab_examples/ellipse_collection.py
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EllipseCollection
x = np.arange(10)
y = np.arange(15)
X, Y = np.meshgrid(x, y)
XY = np.hstack((X.ravel()[:, np.newaxis], Y.ravel()[:, np.newaxis]))
ww = X/10.0
hh = Y/15.0
aa = X*9
fig, ax = plt.subplots()
ec = EllipseCollection(ww, hh, aa, units='x', offsets=XY,
                       transOffset=ax.transData)
ec.set_array((X + Y).ravel())
ax.add_collection(ec)
ax.autoscale_view()
ax.set_xlabel('X')
ax.set_ylabel('y')
cbar = plt.colorbar(ec)
cbar.set_label('X+Y');
Explanation: There are many, many more plot types available.  One useful way to explore these is by
looking at the matplotlib gallery: http://matplotlib.org/gallery.html
You can test these examples out easily in the notebook: simply copy the Source Code
link on each page, and put it in a notebook using the %load magic.
For example:
End of explanation
<END_TASK>