In [31]:
%matplotlib inline

import sys
import IPython
import sklearn
import matplotlib

import numpy as np
import pandas as pd
import scipy as sp

import matplotlib.pyplot as plt

from scipy import sparse

In [2]:
x = np.array([[1,2,3], [4,5,6]])
print('x:\n{}'.format(x))


x:
[[1 2 3]
 [4 5 6]]

In [4]:
# Create a 2D NumPy array with a diagonal of ones, and zeros everywhere else
eye = np.eye(4)
print('NumPy array:\n{}'.format(eye))


NumPy array:
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

In [5]:
# Convert the Numpy array to a SciPy sparse matrix in CSR format
# Only the nonzero entries are stored
sparse_matrix = sparse.csr_matrix(eye)
print('Scipy sparse CSR matrix:\n{}'.format(sparse_matrix))


Scipy sparse CSR matrix:
  (0, 0)	1.0
  (1, 1)	1.0
  (2, 2)	1.0
  (3, 3)	1.0

In [10]:
data = np.ones(4)
row_indices = np.arange(4)
column_indices = np.arange(4)
eye_coo = sparse.coo_matrix((data, (row_indices, column_indices)))
print('COO representation:\n{}'.format(eye_coo))


COO representation:
  (0, 0)	1.0
  (1, 1)	1.0
  (2, 2)	1.0
  (3, 3)	1.0

In [14]:
# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)

# create second array using sine
y = np.sin(x)

# The plot function makes a line chart of one array against another
plt.plot(x, y, marker='x')


Out[14]:
[<matplotlib.lines.Line2D at 0x7f68fca7a3c8>]

In [17]:
# create a simple dataset of people
data = {
    'Name': ['John', 'Anna', 'Peter', 'Linda'],
    'Location': ['New York', 'Paris', 'Berlin', 'London'],
    'Age': [24, 13, 53, 33]
}

data_pandas = pd.DataFrame(data)

data_pandas


Out[17]:
Name Location Age
0 John New York 24
1 Anna Paris 13
2 Peter Berlin 53
3 Linda London 33

In [19]:
data_pandas[data_pandas.Age > 30]


Out[19]:
Name Location Age
2 Peter Berlin 53
3 Linda London 33

In [32]:
print("Python version: {}".format(sys.version))
print("pandas version: {}".format(pd.__version__))
print("matplotlib version: {}".format(matplotlib.__version__))
print("NumPy version: {}".format(np.__version__))
print("SciPy version: {}".format(sp.__version__))
print("IPython version: {}".format(IPython.__version__))
print("scikit-learn version: {}".format(sklearn.__version__))


Python version: 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0]
pandas version: 0.23.4
matplotlib version: 2.2.3
NumPy version: 1.15.0
SciPy version: 1.1.0
IPython version: 6.5.0
scikit-learn version: 0.19.2

A First Application: Classifying Iris Species


In [ ]: