Chapter 15, Machine Learning: Classification, Regression and Clustering 15
a. The k in the k-nearest neighbors algorithm is a hyperparameter of the algo-
rithm.
b. Hyperparameters are set after using the algorithm to train your model.
c. In real-world machine learning studies, you’ll want to use hyperparameter tun-
ing to choose hyperparameter values that produce the best possible predictions.
d. To determine the best value for k in the kNN algorithm, try different odd values
of k then compare the estimator’s performance with each.
15.3 Q10: Consider the following code and output:
In [57]: for k in range(1, 20, 2):
…: kfold = KFold(n_splits=10, random_state=11, shuffle=True)
…: knn = KNeighborsClassifier(n_neighbors=k)
…: scores = cross_val_score(estimator=knn,
…: X=digits.data, y=digits.target, cv=kfold)
…: print(f‘k={k:<2}; mean accuracy={scores.mean():.2%}; ‘ +
…: f‘standard deviation={scores.std():.2%}‘)
…:
k=1 ; mean accuracy=98.83%; standard deviation=0.58%
k=3 ; mean accuracy=98.78%; standard deviation=0.78%
k=5 ; mean accuracy=98.72%; standard deviation=0.75%
k=7 ; mean accuracy=98.44%; standard deviation=0.96%
k=9 ; mean accuracy=98.39%; standard deviation=0.80%
k=11; mean accuracy=98.39%; standard deviation=0.80%
k=13; mean accuracy=97.89%; standard deviation=0.89%
k=15; mean accuracy=97.89%; standard deviation=1.02%
k=17; mean accuracy=97.50%; standard deviation=1.00%
k=19; mean accuracy=97.66%; standard deviation=0.96%
Which of the following statements is false?
a. The loop creates KNeighborsClassifiers with odd k values from 1 through
19 and performs k-fold cross-validation on each.
b. The k value 7 in kNN produces the most accurate predictions for the Digits da-
taset.
c. The accuracy tends to decrease for higher k values.
d. Compute time grows with k, because k-NN needs to perform many more calcu-
lations to find the nearest neighbors.
Chapter 15, Machine Learning: Classification, Regression and Clustering 17
a. The following code tests a linear regression model using the data in X_test
and checks some of the predictions throughout the dataset by displaying the pre-
dicted and expected values for every ________ element:
predicted = linear_regression.predict(X_test)
expected = y_test
for p, e in zip(predicted[::5], expected[::5]):
print(f‘predicted: {p:.2f}, expected: {e:.2f}‘)
a. second
b. fifth
c. pth
d. eth
15.4 Q5: Which of the following statements is false?
a. When creating a model, a key goal is to ensure that it is capable of making ac-
curate predictions for data it has not yet seen. Two common problems that pre-
vent accurate predictions are overfitting and underfitting.
b. Underfitting occurs when a model is too simple to make accurate predictions,
based on its training data. An example of underfitting is using a linear model, such
as simple linear regression, when in fact, the problem really requires a more so-
phisticated non-linear model.
c. Overfitting occurs when your model is too complex. In the most extreme case
of overfitting, a model memorizes its training data.
d. When you make predictions with an overfit model, the model won’t know what
to do with new data that matches the training data, but the model will make ex-
cellent predictions with data it has never seen.
15.5 Case Study: Multiple Linear Regression with
the California Housing Dataset
15.5 Q1: Which of the following statements a), b) or c) is false?
a. The California Housing dataset (bundled with scikit-learn) has 20,640 samples,
each with eight numerical features.
b. The LinearRegression estimator performs multiple linear regression by de-
fault using all of a dataset’s numeric features.
c. You should expect more meaningful results from simple linear regression than
from multiple linear regression on the dataset.
18 Chapter 15, Machine Learning: Classification, Regression and Clustering
d. All of the above statements are true.
15.5.1 Loading the Dataset
15.5 Q2: Which of the following statements is false?
a. You load the California Housing dataset using the the sklearn.datasets
module’s fetch_california_housing function, which returns a Bunch object.
b. The Bunch object’s data and target attributes are NumPy arrays containing
the 20,640 samples and their target values respectively.
c. To confirm the number of samples (rows) and features (columns), look at the
data array’s shape attribute, which shows that there are 20,640 rows and 8 col-
umns, as in:
In [4]: california.data.shape
Out[4]: (20640, 8)
Similarly, you can see that the number of target values—the median house val-
ues—matches the number of samples by looking at the target array’s shape, as
in:
In [5]: california.target.shape
Out[5]: (20640,)
d. The Bunch’s features attribute contains the names that correspond to each
column in the data array.
15.5.2 Exploring the Data with Pandas
15.5 Q3: Consider the following code that imports pandas and sets some options:
import pandas as pd
pd.set_option(‘precision‘, 4)
pd.set_option(‘max_columns’, 9)
pd.set_option(‘display.width’, None)
Which of the following statements a), b) or c)about the set_option calls is false?
a. ‘precision’ is the maximum number of digits to display to the right of each
decimal point.
b. ‘max_columns’ is the maximum number of columns to display when you out-
put the DataFrame’s string representation. In IPython interactive mode, by de-
fault, pandas displays all of the columns left-to-right. The ‘max_columns’ setting
enables pandas to show all the columns using multiple rows of output.
Chapter 15, Machine Learning: Classification, Regression and Clustering 19
c. ‘display.width’ specifies the width in characters of your Command Prompt
(Windows), Terminal (macOS/Linux) or shell (Linux). The value None tells pan-
das to auto-detect the display width when formatting string representations of
Series and DataFrames.
d. All of the above statements are true.
15.5.3 Visualizing the Features
15.5 Q4: Which of the following statements a), b) or c) is false?
a. It’s helpful to visualize your data by plotting the target value against each fea-
ture—in the case of the California Housing Prices dataset, to see how the median
home value relates to each feature.
b. DataFrame method sample can randomly select a percentage of a Data-
Frame’s data (specified keyword argument frac), as in:
sample_df = california_df.sample(frac=0.1, random_state=17)
c. The keyword argument random_state in Part (b)’s snippet enables you to
seed the random number generator. Each time you use the same seed value,
method sample selects a similar random subset of the DataFrame’s rows.
d. All of the above statements are true.
15.5.4 Training the Model
15.5 Q5: Which of the following statements a), b) or c) is false?
a. By default, a LinearRegression estimator uses all the features in the dataset’s
data array to perform a multiple linear regression.
b. An error occurs if any of the features passed to a LinearRegression estimator
for training are categorical rather than numeric. If a dataset contains categorical
data, you must exclude the categorical features from the training process.
c. A benefit of working with scikit-learn’s bundled datasets is that they’re already
in the correct format for machine learning using scikit-learn’s models.
d. All of the above statements are true.
Chapter 15, Machine Learning: Classification, Regression and Clustering 21
In [33]: expected[:5]
Out[33]: array([0.762, 1.732, 1.125, 1.37 , 1.856])
c. With classification, we saw that the predictions were distinct classes that
matched existing classes in the dataset. With regression, it’s tough to get exact
predictions, because you have continuous outputs. Every possible value of x1, x2
… xn in the calculation
y = m1x1 + m2x2 + … mnxn + b
predicts a different value.
d. All of the above statements are true.
15.5.6 Visualizing the Expected vs. Predicted Prices
No questions.
15.5.7 Regression Model Metrics
15.5 Q8: Which of the following statements a), b) or c) is false?
a. Scikit-learn provides many metrics functions for evaluating how well estima-
tors predict results and for comparing estimators to choose the best one(s) for
your particular study.
b. Scikit-learn’s metrics vary by estimator type.
c. Functions confusion_matrix and classification_report (from the mod-
ule sklearn.metrics) are two of many metrics functions specifically for evalu-
ating regression estimators.
d. All of the above statements are true.
15.5 Q9: Which of the following statements a), b) or c) is false?
a. Among the many metrics for regression estimators is the model’s coefficient of
determination, which is also called the R2 score.
b. To calculate an estimator’s R2 score, use the sklearn.metrics module’s
r2_score function with the arrays representing the expected and predicted re-
sults, as in:
In [44]: from sklearn import metrics
In [45]: metrics.r2_score(expected, predicted)
Out[45]: 0.6008983115964333
22 Chapter 15, Machine Learning: Classification, Regression and Clustering
c. R2 scores range from 0.0 to 1.0 with 1.0 being the best. An R2 score of 1.0 indi-
cates that the estimator perfectly predicts the independent variable’s value, given
the dependent variable(s) value(s). An R2 score of 0.0 indicates the model cannot
make predictions with any accuracy, based on the independent variables’ values.
d. All of the above statements are true.
15.5 Q10: Which of the following statements a), b) or c) is false?
a. Another common metric for regression models is the mean squared error,
which
• calculates the difference between each expected and predicted value—
this is called the error,
• squares each difference and
• calculates the average of the squared values.
b. To calculate a regression estimator’s mean squared error, call function
mean_squared_error (from module sklearn.metrics) with the arrays rep-
resenting the expected and predicted results, as in:
In [46]: metrics.mean_squared_error(expected, predicted)
Out[46]: 0.5350149774449119
c. When comparing estimators with the mean squared error metric, the one with
the value closest to 1 best fits your data.
d. All of the above statements are true.
15.5.8 Choosing the Best Model
15.6 Case Study: Unsupervised Machine Learning,
Part 1—Dimensionality Reduction
15.6 Q1: Which of the following statements a), b) or c) is false?
a. Unsupervised machine learning and visualization can help you get to know
your data by finding patterns and relationships among unlabeled samples.
b. Using Matplotlib, Seaborn and other visualization libraries, you can plot da-
tasets with two or three variables using 2D and 3D visualizations, respectively.
Chapter 15, Machine Learning: Classification, Regression and Clustering 23
c. In the Digits dataset, every sample has 64 features (and a target value), so there
is no way to visualize the dataset.
d. All of the above statements are true.
15.6 Q2: Which of the following statements a), b) or c) is false?
a. In big data, samples can have hundreds, thousands or even millions of features.
b. To visualize a dataset with many features (that is, many dimensions), you must
first reduce the data to two or three dimensions. This requires a supervised ma-
chine learning technique called dimensionality reduction.
c. When you graph the resulting data after dimensionality reduction, you might
see patterns in the data that will help you choose the most appropriate machine
learning algorithms to use. For example, if the visualization contains clusters of
points, it might indicate that there are distinct classes of information within the
dataset.
d. All of the above statements are true.
15.6 Q3: Which of the following statements a), b) or c) is false?
a. It’s difficult for humans to think about data with large numbers of dimensions.
This is called the curse of dimensionality.
b. If data has closely correlated features, some could be eliminated via dimension-
ality reduction to improve the training performance.
c. Eliminating features with dimensionality reduction, improves the accuracy of
the model.
d. All of the above statements are true.
15.6 Q4: Which of the following statements a), b) or c) is false?
a. We can use a TSNE estimator (from the sklearn.manifold module) to per-
form dimensionality reduction. This estimator analyzes a dataset’s features and
reduces them to the specified number of dimensions.
b. The following code creates a TSNE object for reducing a dataset’s features to
two dimensions, as specified by the keyword argument n_components:
24 Chapter 15, Machine Learning: Classification, Regression and Clustering
In [3]: from sklearn.manifold import TSNE
In [4]: tsne = TSNE(n_components=2, random_state=11)
c. When using TSNE on the Digits dataset bundled with scikit-learn, the TSNE es-
timator’s random_state keyword argument in Part (b) ensures the reproduci-
bility of the “render sequence” when we display the digit clusters, for example.
d. All of the above statements are true.
15.6 Q5: Which of the following statements a), b) or c) is false?
a. Dimensionality reduction in scikit-learn typically involves two steps—training
the estimator with the dataset, then using the estimator to transform the data into
the specified number of dimensions.
b. The steps mentioned in Part (a) can be performed separately with the TSNE
methods fit and transform, or they can be performed in one statement using
the fit_transform method, as in:
In [5]: reduced_data = tsne.fit_transform(digits.data)
c. TSNE’s fit_transform method takes some time to train the estimator then
perform the reduction. When the method completes its task, it returns an array
with the same number of rows as digits.data, but only the number of columns
specified by the n_components argument when you created the estimator object.
You can confirm this by checking reduced_data’s shape.
d. All of the above statements are true.
15.7 Case Study: Unsupervised Machine Learning,
Part 2—k-Means Clustering
15.7 Q1: Which of the following statements is false?
a. k-means clustering is perhaps the simplest unsupervised machine learning al-
gorithm.
b. The k-means clustering algorithm analyzes unlabeled samples and attempts to
place them in clusters that appear to be related.
c. The k in “k–means” represents the number of clusters to impose on the data.
d. The k-means clustering algorithm organizes samples into the number of clus-
ters you specify in advance, using distance calculations similar to the k-nearest
neighbors clustering algorithm.
Chapter 15, Machine Learning: Classification, Regression and Clustering 27
a. Because the Iris dataset is labeled, we can look at its target array values to get
a sense of how well the k-means algorithm clustered the samples for the three
Iris species.
b. In the Iris dataset, the first 50 samples are Iris setosa, the next 50 are Iris ver-
sicolor, and the last 50 are Iris virginica.
c. If the KMeans estimator chose the Iris dataset clusters perfectly, then each
group of 50 elements in the estimator’s labels_ array should have mostly the
same label.
d. All of the above statements are true.
15.7.5 Dimensionality Reduction with Principal Component
Analysis
15.7 Q8: Which of the following statements a), b) or c) is false?
a. The PCA estimator (from the sklearn.decomposition module), like TSNE,
performs dimensionality reduction. The PCA estimator uses an algorithm called
principal component analysis to analyze a dataset’s features and reduce them to
the specified number of dimensions.
b. Like TSNE, a PCA estimator uses the keyword argument n_components to spec-
ify the number of dimensions, as in:
from sklearn.decomposition import PCA
pca = PCA(n_components=2, random_state=11)
c. The following snippet trains the PCA estimator and produces the reduced data
by calling the PCA estimator’s fit and transform methods:
pca.fit(iris.data)
iris_pca = pca.transform(iris.data)
d. All of the above statements are true.
15.7 Q9: Which of the following statements is false?
a. Each centroid in the KMeans object’s cluster_centers_ array has the same
number of features as the original dataset (four in the case of the Iris dataset).
b To plot the centroids in two-dimensions, you must reduce their dimensions.
c. You can think of a centroid as the “median” sample in its cluster.
d. Each centroid should be transformed using the same PCA estimator used to re-
duce the other samples in that cluster
28 Chapter 15, Machine Learning: Classification, Regression and Clustering
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: c. Actually, you can think of a centroid as the “average” (or “mean”)
sample in its cluster.
15.7.6 Choosing the Best Clustering Estimator
No questions.