<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Week 1
Step1:  Make a vector with 6 elements
Step2:  Get some information about the vector
Step3:  Create a matrix like this
Step4:  Get some information about the matrix
Step5:  A very powerful feature of NumPy and Python are List Comprehensions. These can replace many for loops and are much more efficient to run. Here we square every element in the vector a from above
Step6:  Using NumPy we can select rows and columns of data very easily (known as array slicing). 
For example, we can print the first row of the matrix, m
Step7:  Or we can slice the first column only. Using the , symbol we can ask for specfic rows and columns. The first integer always specifies the rows, which is followed by , and the second integer specifies the columns. The colon character 
Step8:  We can select a specific element using the matrix's column and row index, for example we want to select the item in the second column's second row
Step9:  Entire books have been written about NumPy. Let's move on to Matplotlib.
Matplotlib
First we will load Matplotlib, a 2D plotting library, and run %matplotlib inline. This magic function (functions beginning with the % symbol are called magic functions) will display any plots inline in the notebook.
Step10:  Using the plt.plot() function, you can plot many types of data and Matplotlib will try to figure out what you want to do with the data
Step11:  The plt.plot() function will take an arbritrary number of x y argument pairs
Step12:  Or plot three lines at once by supplying three pairs of x y values
Step13:  Combined with functions, more complex curves can be plotted. Let's plot the sigmoid curve
Step14:  Pandas
Let's now import Pandas. This library provides R-style dataframe table functionality. Like NumPy, it is convention to import the Pandas library as pd for brevity.
Step15:  Let's load the well known Wisconsin Breast Cancer dataset
Step16:  You can view a summary of the data using the describe() function
Step17:  Let's rename the column names
Step18:  See the first few rows
Step19:  Notice how the Class column (the last column in the table) consists of 2s and 4s. In this case 2 stands for malignant and 4 stands for benign. You can check this quickly using
Step20:  So you see only 2s and 4s are contained in this column. See a breakdown of the counts using the value_counts() function
Step21:  However, it is convention in machine learning to use a 0-based index to represent classes. Let's replace the 2s with 0s and the 4s with 1s
Step22:  Perhaps now we would like to drop the ID column
Step23:  As you can see you can use Pandas to quickly manipulate and access tabular data. Here we access the first 10 rows of the Size_Uniformity column
Step24:  Columns can also be accessed using the name of the column as an index
Step25:  You can examine the data types (Pandas dataframes can contain multiple types)
Step26:  The column Bare_Nucleoli appears as type object as it contains some missing data, which appear as ? in the dataset. Later in the course we will learn how to handle missing data.
You can also use Pandas to perform quick statistical analyses. Here we calculate the standard deviation for each column
Step27:  Or calculate the standard deviation for a certain column
Step28:  Pandas also provides useful plotting tools. To look for correlations in data, a scatter matrix is often useful. 
Here we will plot only three columns of the data, and only the first 100 rows of the data, as a scatter plot with so many columns can take some time to render and can result in a very large plot.
Step29:  SciKit-Learn
Last but not least, we shall import some modules from SciKit-Learn. SciKit-Learn is the main machine learning library for Python. It is a large library and is not normally loaded entirely; in general you load only the modules you need from the main library. Here we will load the datasets module and the k-nearest neighbours module (KNeighborsClassifier)
Step30:  Load the Iris dataset (a flower data set often used for demonstration purposes)
Step31:  Convention states that matrices are represented using uppercase letters, often the letter X, and label vectors are represented using lower case letters, often y
Step32:  The k-Nearest Neighbour algorithm is possibly the simplest classifier. Given a new observation, take the label of the sample closest to it in the n-dimensional feature space. 
First we must randomise the data, but we must ensure we randomise the labels as well in sync. We can use a NumPy feature to create indices that then correlate to both the targets and the data
Step33:  Then, the data must be split into a test set and a training set (again we are using naming conventions here for the training and test data X_train and X_test and their labels y_train and y_test)
Step34:  Now we will try to fit the k-nearest neighbours classifier to the training data
Step35:  The classifier has now been trained on the training data (X_train). We can now check how well it predicts newly seen data (using our test set, X_test)
Step36:  We can now element-wise compare our predicted results, in y_pred, with the true labels stored in y_test
<ASSISTANT_TASK:>
Python Code:
import numpy as np
Explanation: Week 1: Getting Started with Jupyter Notebooks
In this notebook, we will make sure all the packages required for this course are properly installed and working. 
To use this notebook, select the input cells (shown as In [x]) in order and press Shift-Enter to execute the code. 
Your installation is properly working if none of the cells below return any errors.
Loading and Testing the Course's Required Packages
In this notebook we will load and test each of packages that we will mainly be using during the course: Numpy, Matplotlib, Pandas, and SciKit-Learn.
Numpy
First, we will import NumPy. NumPy is a linear algebra library, and provides useful vector and matrix functionality, similar to MATLAB. It is convention to define NumPy as np for the sake of brevity:
End of explanation
a = np.array([1,2,3,4,5,6])
# Print the contents of a
a
Explanation: Make a vector with 6 elements:
End of explanation
print("The vector a has " + str(a.ndim) + " dimension(s) and has the shape " + str(a.shape) + ".")
Explanation: Get some information about the vector:
End of explanation
m = np.array([[1,2,3], [4,5,6]])
m
Explanation: Create a matrix like this:
End of explanation
print("The matrix m has " + str(m.ndim) + " dimension(s) and has the shape " + str(m.shape) + ".")
Explanation: Get some information about the matrix:
End of explanation
a_squared = [i**2 for i in a]
a_squared
Explanation: A very powerful feature of NumPy and Python are List Comprehensions. These can replace many for loops and are much more efficient to run. Here we square every element in the vector a from above:
End of explanation
m[0]
Explanation: Using NumPy we can select rows and columns of data very easily (known as array slicing). 
For example, we can print the first row of the matrix, m:
End of explanation
m[:,0]
Explanation: Or we can slice the first column only. Using the , symbol we can ask for specfic rows and columns. The first integer always specifies the rows, which is followed by , and the second integer specifies the columns. The colon character : is shorthand for all rows or all columns. Here we select all rows of matrix m using : and select the first column, using 0:
End of explanation
m[1,1]
Explanation: We can select a specific element using the matrix's column and row index, for example we want to select the item in the second column's second row:
End of explanation
import matplotlib.pyplot as plt
%matplotlib inline
Explanation: Entire books have been written about NumPy. Let's move on to Matplotlib.
Matplotlib
First we will load Matplotlib, a 2D plotting library, and run %matplotlib inline. This magic function (functions beginning with the % symbol are called magic functions) will display any plots inline in the notebook.
End of explanation
plt.plot([1,2,3,4,5])
Explanation: Using the plt.plot() function, you can plot many types of data and Matplotlib will try to figure out what you want to do with the data:
End of explanation
# x**y is shorthand for x to the power of y
plt.plot([1,2,3,4,5],[1**2,2**2,3**2,4**2,5**2]) 
Explanation: The plt.plot() function will take an arbritrary number of x y argument pairs:
End of explanation
p = np.arange(1,10) # Get a range of numbers form 1 to 10
plt.plot(p, p, p, p**2, p, p**3)
Explanation: Or plot three lines at once by supplying three pairs of x y values:
End of explanation
import math
def sigmoid(x):
    a = []
    for item in x:
        a.append(1/(1+math.exp(-item)))
    return a
x = np.arange(-10., 10., 0.1)
sig = sigmoid(x)
plt.plot(x,sig)
Explanation: Combined with functions, more complex curves can be plotted. Let's plot the sigmoid curve:
End of explanation
import pandas as pd
Explanation: Pandas
Let's now import Pandas. This library provides R-style dataframe table functionality. Like NumPy, it is convention to import the Pandas library as pd for brevity.
End of explanation
df = pd.read_csv("https://raw.githubusercontent.com/mdbloice/Machine-Learning-for-Health-Informatics/master/data/breast-cancer-wisconsin.csv")
Explanation: Let's load the well known Wisconsin Breast Cancer dataset:
End of explanation
df.describe()
Explanation: You can view a summary of the data using the describe() function:
End of explanation
df.columns = ["ID","Clump_Thickness","Size_Uniformity","Shape_Uniformity","Marginal_Adhesion","Epithelial_Size","Bare_Nucleoli","Bland_Chromatin","Normal_Nucleoli","Mitoses","Class"]
# Print the new header names:
df.columns
Explanation: Let's rename the column names:
End of explanation
df.head()
Explanation: See the first few rows:
End of explanation
df.Class.unique()
Explanation: Notice how the Class column (the last column in the table) consists of 2s and 4s. In this case 2 stands for malignant and 4 stands for benign. You can check this quickly using:
End of explanation
df.Class.value_counts()
Explanation: So you see only 2s and 4s are contained in this column. See a breakdown of the counts using the value_counts() function:
End of explanation
df = df.replace({"Class": {2: 0, 4: 1}})
df.Class.value_counts()
Explanation: However, it is convention in machine learning to use a 0-based index to represent classes. Let's replace the 2s with 0s and the 4s with 1s:
End of explanation
df = df.drop(["ID"], axis=1)
df.describe()
Explanation: Perhaps now we would like to drop the ID column:
End of explanation
df.Size_Uniformity[0:10]
Explanation: As you can see you can use Pandas to quickly manipulate and access tabular data. Here we access the first 10 rows of the Size_Uniformity column:
End of explanation
df['Size_Uniformity'][0:10]
Explanation: Columns can also be accessed using the name of the column as an index:
End of explanation
df.dtypes
Explanation: You can examine the data types (Pandas dataframes can contain multiple types):
End of explanation
df.std()
Explanation: The column Bare_Nucleoli appears as type object as it contains some missing data, which appear as ? in the dataset. Later in the course we will learn how to handle missing data.
You can also use Pandas to perform quick statistical analyses. Here we calculate the standard deviation for each column:
End of explanation
df.Clump_Thickness.std()
Explanation: Or calculate the standard deviation for a certain column:
End of explanation
from pandas.tools.plotting import scatter_matrix
# Manually select three of the table's columns by passing an array of column names:
df_subset = df[['Clump_Thickness','Size_Uniformity', 'Shape_Uniformity']]
# The semi colon at the end of this line is to suppress informational output (we only want to see the plot)
scatter_matrix(df_subset.head(100), alpha=0.2, figsize=(6,6), diagonal='kde');
Explanation: Pandas also provides useful plotting tools. To look for correlations in data, a scatter matrix is often useful. 
Here we will plot only three columns of the data, and only the first 100 rows of the data, as a scatter plot with so many columns can take some time to render and can result in a very large plot.
End of explanation
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
Explanation: SciKit-Learn
Last but not least, we shall import some modules from SciKit-Learn. SciKit-Learn is the main machine learning library for Python. It is a large library and is not normally loaded entirely; in general you load only the modules you need from the main library. Here we will load the datasets module and the k-nearest neighbours module (KNeighborsClassifier):
End of explanation
iris = datasets.load_iris()
Explanation: Load the Iris dataset (a flower data set often used for demonstration purposes):
End of explanation
X = iris.data
y = iris.target
Explanation: Convention states that matrices are represented using uppercase letters, often the letter X, and label vectors are represented using lower case letters, often y:
End of explanation
np.random.seed(376483)
random_indices = np.random.permutation(len(y))
random_indices
Explanation: The k-Nearest Neighbour algorithm is possibly the simplest classifier. Given a new observation, take the label of the sample closest to it in the n-dimensional feature space. 
First we must randomise the data, but we must ensure we randomise the labels as well in sync. We can use a NumPy feature to create indices that then correlate to both the targets and the data:
End of explanation
X_train = X[random_indices[:-10]]
X_test  = X[random_indices[-10:]]
y_train = y[random_indices[:-10]]
y_test  = y[random_indices[-10:]]
print("Number of training samples: %d. Number of test samples: %d." % (len(X_train), len(X_test)) )
Explanation: Then, the data must be split into a test set and a training set (again we are using naming conventions here for the training and test data X_train and X_test and their labels y_train and y_test):
End of explanation
knn = KNeighborsClassifier() # Initialise the classifier.
knn.fit(X_train, y_train)    # Fit the classifier.
Explanation: Now we will try to fit the k-nearest neighbours classifier to the training data:
End of explanation
y_pred = knn.predict(X_test)
# The classifier's predicted labels are now contained in y_pred:
y_pred
Explanation: The classifier has now been trained on the training data (X_train). We can now check how well it predicts newly seen data (using our test set, X_test):
End of explanation
y_pred == y_test
Explanation: We can now element-wise compare our predicted results, in y_pred, with the true labels stored in y_test:
End of explanation
<END_TASK>