please see attachment
{
“cells”: [
{
“cell_type”: “code”,
“execution_count”: 1,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “_mH0rAuvUju8”
},
“outputs”: [],
“source”: [
“# Import libraries:\n”,
“import numpy as np\n”,
“import pandas as pd\n”,
“import seaborn as sns # optional, but recommended\n”,
“import matplotlib.pyplot as plt \n”,
“from sklearn import datasets”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“metadata”: {},
“outputs”: [],
“source”: [
“cancer = datasets.load_breast_cancer()”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “fxzWh2YoU8Un”
},
“outputs”: [],
“source”: [
“# enter your code here”
]
}
],
“metadata”: {
“colab”: {
“name”: “hw0_template.ipynb”,
“provenance”: [],
“version”: “0.3.2”
},
“kernelspec”: {
“display_name”: “Python 3”,
“language”: “python”,
“name”: “python3”
},
“language_info”: {
“codemirror_mode”: {
“name”: “ipython”,
“version”: 3
},
“file_extension”: “.py”,
“mimetype”: “text/x-python”,
“name”: “python”,
“nbconvert_exporter”: “python”,
“pygments_lexer”: “ipython3”,
“version”: “3.7.3”
}
},
“nbformat”: 4,
“nbformat_minor”: 1
}
CS Machine Learning
Homework 0 – Programming
Keywords: Jupyter, scikit-
learn
We will use Jupyter Notebook with Python 3 in this course. Install Python 3.7, jupyter, scikit-
learn, pandas, and matplotlib. We also recommend installing seaborn, a data visualization library.
You may find it convenient to use Anaconda to manage your Python setup.
Load the Wisconsin breast cancer data set, which is available as part of the sklearn datasets.
You can read more about this dataset here.
from sklearn import datasets
cancer = datasets.load_breast_cancer()
The object cancer is a dictionary, you can check the keys using
cancer.keys()
and check the details by calling the keys, like by using
cancer.feature_names
to see the feature names. You can also view a more detailed description of the dataset:
print(cancer.DESCR)
Please read the description! On your own, explore the other functions related to the keys. You
will also want to load the data (from cancer.data) into a pandas DataFrame.
df = pd.DataFrame(cancer.data, columns=cancer.feature_names)
You can select the column(s) of the DataFrame by indexing on the column name(s). E.g., to
see the ‘mean radius’ column of df, simply type
df[‘mean radius’]
Notice that invoking the above command returns a Series, a fundamental Pandas data-structure.
You can read more about this online.
You can select the row(s) of the DataFrame in multiple ways. One way is by slicing. For
example, if you want to see the top
1
00 rows of df, simply type
df[:100]
Or if you want to see the 100th, simply type
1
https://scikit-learn.org/stable/datasets/toy_dataset.html#breast-cancer-dataset
df[99:100]
Another way would be
df.ix[99]
You can read more about the ix method online.
The questions below involve exploring the data by plotting some figures, and you will find
libraries like numpy, matplotlib and seaborn very useful. You may want to read up on built in
functions like where from numpy for finding indices (or index from pandas) and show, savefig
from matplotlib to show and save your plots.
(a) How many rows and columns are there in this data set? What do the rows and columns
represent?
(b) How many malignant cases are there in total?
(c) Make a scatterplot of ‘mean compactness’ and ‘mean perimeter’. Try to color the points by
their label (i.e. malignant or benign). You may find scatter (from matplotlib) or jointplot
(from seaborn) useful for this.
(d) Make pairwise scatterplots of ‘mean texture’, ‘mean perimeter’, ‘mean compactness’, and ‘mean
radius’. You may find scatter matrix (from matplotlib) or pairplot (from seaborn) useful
for this. Do you notice that two of these features seem to be particularly correlated? Why
might this be?
2
CS Machine Learning
Homework
1
– Programming
Keywords: decision trees
1. Read the online documentation on decision trees and random forests in scikit-learn to find
out how to use decision trees and random forests. Notice that training a classifier is done
using the fit method, and that for decision trees this is done using a more sophisticated
evolution (known as CART) of the ID3 algorithm covered in class.
(a) Use the breast cancer data set from Homework 0 to create a training set. Recall that the
label is 0 if the patient’s data indicates a malignant cancer and 1 otherwise. Compute
the base rate of malignant cancer occurrence over the entire data set.
(b) The goal is to build a decision tree that, based on the other features in the set, predicts
whether or not a patient has malignant cancer. So this is a classification problem. Using
tree.DecisionTreeClassifier and other functions in the scikit-learn library, one can
build a decision tree and calculate both its training accuracy when fitted to the entire
data set as well as its accuracy using 10-fold cross validation (which gives a better idea
of true accuracy).
Vary the depth of your decision tree (use max depth = 1,2,. . . ,10 ) and plot both training
accuracy and cross-validated accuracy (as a function of the depth, on the x-axis). Plot
both curves on the same plot and use a legend to label them.
(c) Now try the random forest classifier of the scikit-learn library and use the best depth
you get from (b) as max depth. Vary the number of trees in the forest via the param-
eter n estimators and plot its 10-fold cross-validated accuracy (use n estimators =
1, 2, . . . , 20). Do you see an improvement using random forests versus using a single tree?
(Note: use the n estimators=1 result as the result for a single tree.)
(d) Using the method for building a decision tree you used in part (b), build a tree but
randomly hold out a .2, .4, .6, and .8 fraction of the data set (so you will need to build
4 different trees for each depth value). For each fraction held out, plot a curve of the
test accuracy (the accuracy on the held-out set) against depth. You should have four
curves. Plot them all on the same plot and use a legend to label them.
1
https://scikit-learn.org/stable/modules/tree.html
https://scikit-learn.org/stable/modules/ensemble.html#forests-of-randomized-trees
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.