<jupyter_start><jupyter_text>Exercises and Appendix AThe idea of this notebook is to gather all the exercises (and answers) that do not rquire coding. Those exercises that do require code are planned to be covered in the notebook of the according chapter. All answers shown here can also be found in Appendix A (page 475) of the book. Exercises in Chapter 1page 31 1. How would you define Machine Learning?A machine is said to learn a taks T with increasing experience E if its performance on this task T, as measured by some performance measure P, increases with E.Book solution: Machine Learning is about building systems that can learn from data. Learning means getting better at some task, given some performance measure. 2. Can you name four types of problems where it shines?- Problems that require a lot of hand-tuning or long lists of rules: these tasks can be automated by using a Machine Learning algorithm.- Complex problems for which there seem to be no traditional solutions. Machine Learning might find a good solution.- Significantly fluctuating input data: a Machine Learning algorithm can adapt to new input data.- Getting insights about complex problems and large amounts of data.Book solution: Machine Learning is great for complex problems for which we have no algorithmic solution, to replace long lists of hand-tuned rules, to build systems that adopt to fluctuating environments, and finally to help humans learn (e.g., data mining). 3. What is a labeled training set?A training set where all instances also have the target feature. Labeled training sets are used for supervised Machine Learning.Book solution: A labeled training set is a training set that contains the desired solution (a.k.a. a label) for each instance. 4. What are the two most common supervised tasks?Classification and regression.Book solution: The two most common supervised tasks are regression and classification. 5. Can you name four common unsupervised tasks?Clustering (e.g., clustering customers into groups), visualization (of networks or graphs), dimensionality reduction (get rid of redundant features), and anomaly detection (e.g., credit card fraud).Book solution: Common unsupervised tasks inlude clustering, visualization, dimensionality reduction, and association rule learning. 6. What type of Machine Learning algorithm would you use to allow a robot to walk in various unknown terrains?An algorithm that uses "reinforcement learning", i.e., an algorithm that accumulates "rewards" for desired behavior and "penalties" for undesired behavior.Book solution: Reinforcement Learning is likely to perform best if we want a robot to learn to walk in various unknown terrains since this is typically the type of problem that Reinforcement Learning tackles. It might be possible to expres the problem as a supervised or semisupervised learning problem, but it would be less natural. 7. What type of algorithm would you use to segment your customers into multiple groups?Unsupervised Machine Learning.Book solution: If you don't know how to define the groups, then you can use a clustering algorithm (unsupervised learning) to segment your customers into clusters of similar customers. However, if you know what groups you would like to have, then you can feed many examples of each group to a classifcation algorithm (supervised learning), and it will classify all your customers into these groups. 8. Would you frame the problem of spam detection as a supervised learning problem or an unsupervised learning problem?As a supervised learning problem.Book solution: Spam detection is a typical supervised learning problem: the algorithm is fed many emails along with their label (spam or not spam). 9. What is an online learning system?One that (i) automatically includes newly available data in its learning process or (ii) employs out-of-core learning.Book solution: An online learning system can learn incrementally, as opposed to a batch learning system. This makes it capable of adapting rapidly to both changing data and autonomous systems, and of training on very large quantities of data. 10. What is out-of-core learning?Cutting huge amounts of data (so huge that it cannot be fed to the system at once) in pieces ("mini-batches"), training the system on the first piece, and then feeding the system the remaining data piece-by-piece as if these pieces were new data in an online system. The difference to actual online learning is that the "new" pieces are not new and the "online" learning happens actually offline.Book solution: Out-of-core algorithms can handle vast quantities of data that cannot fit in a computer's main memory. An out-of-core learning algorithm chops the data into mini-batches and uses online learning techniques to learn from these mini-batches. 11. What type of learning algorithm relies on a similarity measure to make predictions?Instance-based learning.Book solution: An instance-based learning system learns the training data by heart; then, when given a new instance, it uses a similarity measure to find the most similar learnd instances and uses them to make predictions. 12. What is the difference between a model parameter and a learning algorithm's hyperparameter?A model parameter is optimized in the process of training a model-based learning algorithm. This is not possible in an instance-based learning algorithm simply because there are no model parameters. But both model-based and instance-based algorithms use hyperparameters that determine the overall structure of the algorithm. Examples:- Model-based algorithmThe order of a polynomial for polynomial regression is a hyperparameter while the prefactor to a certain power is a model parameter.- Instance-based algorithmThe kind of norm used for a similarity measure is also a hyperparameter.Book solution: A model has one or more model parameters that determine what it will predict given a new instance (e.g., the slope of a linear model). A learning algorithm tries to find optimal values for these parameters such that the model generalizes well to new instances. A hyperaparameter is a parameter of the learning algorithm  itslef, not of the model (e.g.,  the amount of regularization to apply). 13. What do model-based learning algorithms search for? What is the most common strategy they use to succeed? How do they make predictions?Model-based algorithms aim to optimize the model parameters versus some performance measure. The most common strategy is regression, aiming to minimize a cost function. These algorithms make predictions by applying the model to a new instance, the output being usually a number (or several numbers).Book solution: Model-based learning algorithms search for an optimal value for the model parameters such that the model will generalize well to new instances. We usually train such systems by minimizing a cost function that measures how bad the system is at making predictions on the training data, plus a penalty for model complexity if the model is regularized. To make predicitons, we feed the new instance's features into the model's  prediction function, using the parameter values found by the learning algorithm. 14. Can you name four of the main challenges in Machine Learning?Insufficient quantity of training data (results in insufficient learning), erroneous data (results in wrong predictions), irrelevant features in data (slows down the training process), overfitting (good on training data but bad on new data), and underfitting (model does not capture the complexity and makes naïve i.e. poor predictions).Book solution: Some of the main challenges in Machine Learning are the lack of data, poor data quality, nonrepresentative data, uninformative features, excessively simple models that underfit the training data, and excessively complex models that overfit the data. 15. If your model performs great on the training data but generalizes poorly to new instances, what is happening? Can you name three possible solutions?Probably, overfitting is happening. Get more data, higher quality, or regularize the model via hyperparameters.Book solution: If a model performs great on the training data but generalizes poorly to new instances, the model is likely overfitting the training data (or we got extremely lucky on the training data). Possible solutions to overfitting are getting more data, simplifying the model (selecting a simpler algorithm, reducing the number of parameters or features used, or regularizing the model), or reducing the noise in the training data. 16. What is a test set and why would you want to use it?To simulate new instances and test the established model on these "new" instances.Book solution: A test set is used to estimate the generalization error that a model will make on new instances, before the model is launched in production. 17. What is the purpose of a validation set?To test several version of several algorithm. First, shortlist a few promising algorithms, then optimize these, and finally choose the best. All this is done using a validation set (or via cross-validation). The test set remains untouched in this entire process.Book solution: A validation set is used to compare models. It makes it possible to select the best model and tune the hyperparameters. 18. What can go wrong if you tune hyperparameters using the test set?The final model might end up as a fit to the test set. Being some kind of fit to this data, the model might not generalize well to all-new data.Book solution: If you tune hyperparameters using the test set, you risk overfitting the test set, and the generalization error you measure will be optimistic (you may launch a model that performs worse than you expect). 19. What is cross-validation and why would you prefer it to a validation set?Cross validation does not split the training set (the counterpart of the test set) further into sets for training and validation but uses the entire set for training. Validation is still possible as follows.- The training set is cut in k (usually k=5 or k=10) folds, all of which should preserve representative ratios of features.- The model is trained on k-1 folds and validated on the remaining fold.- This previous step is performed k times so every fold has been used for validation once.The advantages of cross-validation are that, first, the model is trained on all the training data, i.e., on more data. Secondly, performing the validation k times allows for a measure on the insecurity of the model, e.g., by calculating the standard deviation of the performance on the k validation folds.Book solution: Cross-validation is a technique that makes it possible to compare models (for model selection and hyperparamter tuning) without the need for a separate validation set. This saves precious training data. Exercises in Chapter 2See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 3See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 4page 142 1. What Linear Regression training algorithm can you use if you have a training set with millions of features?If you have a training set with millions of features you can use Stochastic Gradient Descent  or Mini-batch Gradient Descent, and perhaps Batch Gradient Descent if the training set fits in memory. But you cannot use the Normal Equation because the computational complexity grows quickly (more than quadratically) with the number of features. 2. Suppose the features in your training set have very different scales. What algorithms might suffer from this, and how? What can you do about it?If the features in your training set have very different scales, the cost function will have the shape of an elongated bowl, so the Gradient Descent algorithms will take a long time to converge. To solve this you should scale the data before training the model. Note that the Normal Equation will work just fine without scaling. Moreover, regularized models may converge to a suboptimal solution if the features are not scaled: indeed, since regularzation penalizes large weights, features with smaller values will tend to be ignored compared to features with larger values. 3. Can Gradient Descent get stuck in a local minimum when training a Logistic Regression model?Gradient Descent cannot get stuck in a local minimum when training a Logistic Regression model because the cost function is convex. 4. Do all Gradient Descent algorithms lead to the same model provided you let them run long enough?If the optimization problem is convex (such as Linear Regression or Logistic Regression), and assuming the learning rate is not too high, then all Gradient Descent algorithms will apporach the global optimum and end up producing fairly similar models. However, unless you gradually reduce the learning rate, Stochastic GD and mini-batch GD will never truly converge; instead, they will keep jumping back and forth around the global optimum. This means that even if you let them run for a very long time, these Gradient Descent algorithms will porudce slightly different models. 5. Suppose you use Batch Gradient Descent and you plot the validation error at every epoch. If you notice that the validation error consistently goes up, what is likely going on? How can you fix this?If the validation error consistently goes up after every eopoch, then one possibility is that the learning rate is too high and the algorithm is diverging. If the training error also goes up, then this is clearly the problem and you should reduce the learning rate. However, if the training error is not going up, then your model is overfitting the training set and you should stop training. 6. Is it a good idea to stop Mini-batch Gradient Descent immediately when the validation error goes up?Due to their random nature, neither Stochastic Gradient Descent nor Mini-batch Gradient Descent is guaranteed to make progress at every single training iteration. So if you immediately stop training when the validation error goes up, you may stop much too early, before the optimum is reached. A better option is to save the model at regular intervals, and when it has not improved for a long time (meaning it will porbably never beat the record), you can revert to the best saved model. 7. Which Gadient Descent algorithm (among those we discussed) will reach the vicinity of the optimal solution the fastest? Which will actually converge? How can you make the others converge as well?Stochastic Gradient Descent has the fastest training iteration since it considers only one training instance at a time, so it is generally the first to reach the vicinity of the global optimum (or Mini-batch GD with a very small min-batch size). However, only Batch Gradient Descent will actually converge, given enough training time. As mentioned, Stocastic GD and Mini-batch GD will bounce around the optimum, unless you gradually reduce the learning rate. 8. Suppose you are using Polynomial Regression. You plot the learning curves and you notice that there is a large gap between the training error and the validation eror. What is happening? What are three ways to solve this?If the validation error is much higher than the training error, this is likely because your model is overfitting the training set. One way to try to fix this is to reduce the polynomial degree: a model with fewer degrees of freedom is less likely to overfit. Another thing you can try is to reularize the model -- for example, by adding an $L_2$ penalty (Ridge) or an $L_1$ penalty (Lasso) to the cost function. This will also reduce the degrees of freedom of the model. Lastly, you can try to increase the size of the training set. 9. Suppose you are using Ridge Regression and you notice that the training error and the validation error are almost equal and fairly high. Would you say that the model suffers from high bias or high variance? Should you increase the regularization hyperparameeter $\alpha$ or reduce it?If both the training error and the validation error are almost equal and fairly high, the model is likely underfitting the training set, which means it has a high bias. You should try reducing the regularization hyperparameter $\alpha$. 10. Why would you want to use:- **Ridge Regression  instead of plain Linear Regression (i.e., without any regularization)?**- **Lasso instead of Ridge Regression?**- **Elastic Net instead of Lasso?**Let's see:- A model with some regulaization typically performs better than a model without any regularization, so you should generally prefer Ridge Regression over plain Linear Regression.- Lasso Regression uses an $L_1$ penalty, which tends to push the weights down to exactly zero. This leads to sparse models, where all weights are zero except for the most important weights. This is a way to perfrom feature selection automatically, which is good if you suspect that only a few features actually matter. When you are not sure, you should prefer Ridge Regression.- Elastic Net is generally preferred over Lasso since Lasso may behave erratically in some cases (when several features are strongly correlated or when there are more features than training intances). However, it does add an extra hpyerparameter to tune. If you just want Lasso without the erratic behavior, you can just use Elasic Net with an $L_1$_ratio close to 1. 11. Suppose you want to classify pictures as outdoor/indoor and daytime/nighttime. Should you implement two Logistic Regression classifiers or one Softmax Regression classifier?If you want to classify pictures as outdoor/indoor and daytime/nighttime, since these are not exclusive classes (i.e., all four combinations are possible) you should train two Logistic Regression classifiers. 12.See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 5page 165 1. What is the fundamental idea behind Support Vector Machines?The fundamental idea behind Support Vector Machines is to fit the widest possible "street" between the classes. In other words, the goal is to have the largest possible margin between the decision boundary that separates the two classes and the training instances. When performing soft margin classification, the SVM searches for a compromise between perfectly separating the two classes and having the widest possible street (i.e., a few instances may end up on the street). Another key idea is to use kernels when training on nonlinear datasets. 2. What is a support vector?After training an SVM, a *support vector* is any instance located on the "street" (see the previous answer), including its border. The decision boundary is entirely determined by the support vectors. Any instance that is *not* a support vector (i.e., off the street) has no influence whatsoever; you could remove them, add more instances, or move them around, and as long as they stay off the street they won't affect the decision boundary. Computing the predictions only involves the support vectors, not the whole training set. 3. Why is it important to scale the inputs when using SVMs?SVMs try to fit the largest posible "street" between the classes (see the first answer), so if the training set is not scaled, the SVM will tend to neglect small features (see Figure 5-2).Own note: The possible neglection of small features comes about from the fact that machine algorithms may implicitly (machine limit) or explicitly (*tolerance* etc.) fixed boundaries on their accuracy. 4. Can an SVM classifier output a confidence score when it classifies an instance? What about a probability?An SVM classifier can output the distance between the test instance and the decision boundary, and you can use this as a confidence score. However, this score cannot be directly converted into an estimation of the class probability. If you set  *probability=True* when creating an SVM in Scikit-Learn, then after training it will calibrate the probabilities using Logisitc Regression on the SVM's scores (trained by an addtitional five-fold cross-validation on the training data). This will add the *predict_proba()* and *predict_log_proba()* methods to the SVM. 5. Should you use the primal or the dual form of the SVM problem to train a model on a training set with millions of instances and hundreds of features?This question applies only to linear SVMs since kernelized can only use the dual form. The computational coplexity of the primal form of th SVM problem is proportional to the number of training instances $m$, while the computational complexity of the dual form is proportional to a number between $m^2$ and $m^3$. So if there are millions of instances, you should definitely use the primal form, because the dual form will be much too slow. 6. Say you trained an SVM classifier with an RBF kernel. It seems to underfit the traninig set: should you increase or decrease $\gamma$ (gamma)? What about $C$?If an SVM classifier trained with an RBF kernel underfits the training set, there might be too much regularization. To decrease it, you need to increase $\gamma$ or $C$ (or both). 7. How should you set the QP parameters ($H$, $f$, $A$, and $b$) to solve the soft margin linear SVM classifier problem using an off-the-shelf QP solver?Let's call the QP parameters for the hard-margin problem $H'$, $f'$, $A'$ and $b'$ (see "Quadratic Programming" on page 159). The QP parameters for the soft margin problem have $m$ additional parameters ($n_p=n+1+m$) and $m$ additional constraints ($n_c=2m$). They can be defined like so:- $H$ is equaal to $H'$, plus $m$ columns of 0s on the right and $m$ rows of 0s at the bottom:$H=\begin{pmatrix}H'&0&\cdots\\0&0&\\\vdots&&\ddots\end{pmatrix}$.- $f$ is equal to $f'$ with $m$ additional elements, all equal to the value of the hyperparameter $C$.- $b$ is equal to $b'$ with $m$ additional elements, all equal to 0.- $A$ is equal to $A'$, with an extra $m\times m$ identitiy matrix $I_m$ appended to the right, $-I_m$ just below it, and the rest filled with zeros: $A=\begin{pmatrix}A'&I_m\\0&-I_m\end{pmatrix}$.A confirmation of the above relations is shown in the file "Gradients.pdf". 8.-10.See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 6page 178 1. What is the approximate depth of a Decision Tree trained (without restrictions) on a training set with 1 million  instances?If every leaf node will contain only one instance and all above nodes split the set in a balanced way, $m=m_{\rm leaves}=2^{n_{\rm nodes}}$ is the number of instances. So $n_{\rm nodes}=log_2m$ is the approximate depth.Book solution: The depth of a well-balanced binary tree containing *m* leaves is equal to $log_2m^3$, rounded up. A binary Decision Tree (one that makes only binary decisions, as is the case of all trees in Scikit-Learn) will end up more or less well balanced a the end of training, with one leaf per training instance if it is trained without restrictions. Thus, if the training set contains one million instances, the Decision Tree will have a depth of $log_210^6\approx20$ (actually a bit more since the tree will generally not be perfectly well balanced). 2. Is a node's Gini impurity generally lower or greater than its parent's? Is it *generally* lower/greater, or *always* lower/greater?A node's Gini impurity is generally lower than its parent's. This is due to the CART training algorithm's cost function, which splits each node in a way that minimizes the weighted sum of its children's Gini impurities. However, it is possible for a node to have a higher Gini impurity than its parent, as long as this increase is more than compensated for by a decrease of the other child's impurity. For example, consider a node containing four instances of class A and 1 of class B. Its Gini impurity is $1-\frac{2^2}{5}-\frac{3^2}{5}=0.32$. Now suppose the dataset is one-dimensinal and the instances are lined up in the following order: A,B, A, A, A. You can verify that the algorithm will split this node after the second instance, producing one child note with instances A, B, and the other child node with instances A, A, A. The first child node's Gini impurity is $1-\frac{1^2}{2}-\frac{1^2}{2}=0.5$, which is higher than its parent. This is compensated for by the fact that the other node is pure, so the overall weighted Gini impurity is $\frac{2}{5}\times 0.5+\frac{3}{5}\times0=0.2$, which is lower than the parent's Gini impurity. 3. If a Decision Tree is overfitting the training set, is it a good idea to try decreasing *max_depth*?If a Decision Tree is overfitting the training set, it may be a good idea to decrease *max_depth*, since this will constrain the model, regularizing it. 4. If a Decision Tree is underfitting the training set, is it a good idea to try scaling the input features?Scaling the input features is irrelevant for Decision Trees as the treat all features independently.Book solution: Decision Trees don't care whether or not the training data is scaled or centered; that's one of the nice things about them. So if a Decision Tree underfits the training set, scaling the input features will just be a waste of time. 5. If it takes one hour to train a Decisoin Tree on a training set containing 1 million instances, roughly how much time will it take to train another Decision Tree on a training set containing 10 million instances?The computational complexity of training a Decision Tree is $\mathcal{O}(n\times m\log(m))$. So if you multiply the training set size by 10, the training time will be multiplied by $K=(n\times10m\times\log(10m))/(n\times m\times\log(m))=10\times\log(10m)/\log(m)$. If $m=10^6$, then $K\approx11.7$, so you can expect the training time to be roughly 11.7 hours.Note that the basis of the logarithm is 10, here: $\log(10)=1$. 6. If your training set contains 100000 instances, will setting *presort=True* speed up training?No, presorting with Scikit-Learn is only effective up to about a thousand instances.Book solution: Presorting the training set speeds up training only if the dataset is smaller than a few thousand instances. If it contains 100000 instances, setting *presort=True* will considerably slow down training. 7.-8.See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 7page 202 1. If you have trained five different models on the exact same training data, and they all achieve 95% precision, is there any chance that you can combine these models to get better results? If so, how? If not, why?If you have trained five different models and they all achieve 95% precision, you can try combining them into a voting ensemble, which will often give you even better results. It works better if the models are very different (e.g., an SVM classifier, A Decision Tree classifier, a Logistic Regression classifier, and so on). It is even better if they are trained on different training instances (that's the whole point of bagging and pasting ensembles), but if not it will still work as long as the models are very different. 2. What is the difference between hard and soft voting classifiers?A hard voting classifier just counts the votes of each classifier in the ensemble and picks the class that gets the most votes. A soft voting classifier computes the average estimated class probability of each class and picks the class with the highest probability. This gives high-confidence votes more weight and often perfroms better, but it works only if every classifier is able to estimate class probabilities (e.g., for the sVM classifiers in Scikit-Learn you must set "probability=True"). 3. Is it possible to speed up training of a bagging ensemble by distributing it across multiple servers? What about pasting ensembles, boosting ensembles, random forests, or stacking ensembles?It is quite possible to speed up training of a bagging ensemble by distributing it across multiple servers, since each predictor in the ensemble is independent of the others. The same goes for pasting ensembles and Random Forests, for the same reason. However, each predictor in a boosting ensemble is built based on the previous predcitor, so training is necessarily sequential, and you will not gain anything by distributing training across multiple servers. Regarding stacking ensembles, all the predictors in a given layer are independent of each other, so they can be trained in parallel on multiple servers. However, the predictors in one layer can only be trained after the predictors in the previous layer have all been trained. 4. What is the benefit of out-of-bag evaluation?With out-of-bag evaluation, each predictor in a bagging ensemble is evaluated using instances that it was not trained on (they were held out). This makes it possible to have a fairly unbiased evaluation of the ensemble without the need for an additional validation set. Thus, you have more instances available for training, and your ensemble can perform slightly better. 5. What makes Extra-Trees more random then regular Random Forests? How can this extra randomness help? Are Extra-Trees slower or faster than regular Random Forests?When you are growing a tree in a Random Forest, only a random subset of the features is considered for splitting at each node. This is true as well for Extra-Trees, but they go one step further: rather than searching for the best possible thresholds, like regular Decision Trees do, they use random thresholds for each feature. This extra randomness acts like a form of regularization: if a Random Forest overfits the training data, Extra-Trees might perfrom better. Moreover, since Extra-Trees don't search for the best possible thresholds, they are much faster to train than Random Forests. However, they are neither faster nor slower than Random Forests when making predictions. 6. If your AdaBoost ensemble underfits the training data, what hyperparameters should you tweak and how?If your AdaBoost ensemble underfits the training data, you can try increasing the number of estimators or reducing the regularization hyperparameters of the base estimator. You may also try slightly increasing the learning rate. 7. If your Gradient Boosting ensemble overfits the training set, should you increase or decrease the learning rate?If your Gradient Boosting ensemble overfits the training set, you should try decreasing the learning rate. You could also use early stopping to find the right number of predictors (you probably have too many). 8.-9.See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 8page 224 1. What are the main motivations for reducing a dataset's dimensionality? What are the main drawbacks?The main motivations for dimensionality reduction are:- To speed up a subsequent training algorithm (in some cases it may even remove noise and redundant featrures, making the training algorithm perform better).- To visualize the data and gain insights on the most important features.- Simply to save space (compression).The main drawbacks are:- Some information is lost, possibly degrading the performance of subsequent training algorithms.- It can be computationally intensive.- It adds some complexity to your Machine Learning pipelines.- Transformed features are often hard to interpret. 2. What is the curse of dimensionality?The curse of dimensionality refers to the fact that many problems that do not exist in low-dimensional space arise in high-dimensional space. In Machine Learning, one common manifestation is the fact that randomly sampled high-dimensional vectors are generally very sparse, increasing the risk of overfitting and making it very difficult to identify patterns in the data without having plenty of training data. 3. Once a dataset's dimensionality has been reduced, is it possible to reverse the operation? If so, how? If not, why?Once a dataset's dimensionality has been reduced using on of the algorithms we discussed, it is almost always impossible to perfectly reverse the operation, because some information gets lost during dimensionality reduction. Moreover, while some algorithms (such as PCA) have a simple reverse transformation procedure that can reconstruct a dataset relatively similar to the original, other algorithms (such as T-SNE) do not. 4. Can PCA be used to reduce the dimensionality of a highly nonlinear dataset?PCA can be used to significantly reduce the dimensionality of most datasets, even if they are highly nonlinear, because it can at least get rid of useless dimensions. However, if there are no useless dimensions - for example, the Swiss roll - then reducinng dimensionality with PCA will lose too much information. You want to unroll the Swiss roll, not squash it. 5. Suppose you perfrom PCA on a 1000-dimensional dataset, setting the explained variance ratio to 95%. How many dimensions will the resulting dataset have?That's a trick question: it depends on the dataset. Let's look at two extreme examples. First, suppose the dataset is composed of points that are almost perfectly aligned. In this case, PCA can reduce the dataset down to just one dimesnion while still perserving 95% of the variance. Now imagine that the dataset is composed of perfectly random points, scatterd all around the 1000 dimensions. In this case roughly 950 dimensions are required to preserve 95% of the variance. So the answer is, it depends on the dataset, and it could be any number between 1 and 950. Plotting the explained variance as a function of the number of dimensions is one way to get a roguh idea of the dataset's intrinsic dimensionality. 6. In what cases would you use vanilla PCA, Icremental PCA, Randomized PCA, or Kernel PCA?Regular PCA is the default, but it works only if the dataset fits in memory. Incremental PCA is useful for large datasets that don't fit in memory, but it is slower than regular PCA, so if the dataset fits in memory you should prefer regular PCA. Incremental PCA is also useful for online tasks, when you need to apply PCA on the fly, every time a new instance arrives. Randomized PCA is useful when you want to considerably reduce dimensionality and the dataset fits in memory; in this case, it is much faster than regular PCA. Finally, Kernel PCA is useful for nonlinear datsets.Remark: The word "vanilla" refers to the default version of an algorithm. The analogy comes from ice cream, where the vanilla flavour can be considered a "default" choice of flavour because basically everybody likes it. In the end, it might not be the best choice but it is a good choice if you do not know where to start. 7. How can you evaluate the performance of a dimensionality reduction algorithm on your dataset?Intuitively, a dimensionality reduction algorithm performs well if it eliminates a lot of dimensions from the dataset without losing too much information. One way to measure this is to apply the reverse transformation and measure the reconstruction error. However, not all dimensionality reduction algorithms provide a reverse transformation. Alternatively, if you are using dimensionality reduction as a preprocessing step before another Machine Learning algorithm (e.g., a Random Forest classifier), then you can simply measure the performance of that second algorithm; if dimensionality reduction did not lose too much information, then the algorithm should perform just as well as when using the orignal dataset. 8. Does it make any sense to chain two different dimensionality reduction algorithms?It can absolutely make sense to chain two different dimensionality reduction algorithms. A common example is using PCA to quickly get rid of a large number of useless dimensions, then applying another much slower dimensionality reduction algirthm, such as LLE. This two-step approach will likely yield the same performance as using LLE only, but in a fraction of the time. 9.-10.See the Jupyter notebooks available at https://github.com/ageron/handson-ml. Exercises in Chapter 9page 251 1. What are the main benefits of creating a computation graph rather than directly executing the computations? What are the main drawbacks?Main benefits and drawbacks of creating a computation graph rather than directly executing the computations:*Main benefits:*- TensorFlow can automatically compute the gradients for you (using reverse-mode autodiff).- TensorFlow can take care of running the operations in parallel in different threads.- It makes it easier to run the same model across different devices.- I simplifies introspection -- for example, to view the model in TensorBoard.*Main drawbacks:*- It makes the learning curve steeper.- It makes step-by-step debugging harder. 2. Is the statement "a_val = a.eval(session=sess)" equivalent to "a_val = sess.run(a)"?Yes, the statement "a_val = a.eval(session=sess)" is indeed equivalent to "a_val = sess.run(a)". 3. Is the statement "a_val, b_val = a.eval(session=sess), b.eval(session=sess)" equivalent to "a_val, b_val = sess.run([a, b])"?No, the statement "a_val, b_val = a.eval(session=sess), b.eval(session=sess)" is not equivalent to "a_val, b_val = sess.run([a, b])". Indeed, the first statement runs the graph twice (once to compute "a", once to compute "b"), while the second statement runs the graph only once. If any of these operations (or the ops they depend on) have side effects (e.g., a variable is modified, an item is inserted in a queue, or a reader reads a file), then the effects will be different. If they don't have side effects, both statements will return the same result, but the second statement will be faster than the first. 4. Can you run two graphs in the same session?No, you cannot run two graphs in the same session. You would have to merge the graphs into a single graph first. 5. If you create a graph "g" containing a variable "w", then start two threads and open a session in each thread, both using the same graph "g", will each session have its own copy of the variable "w" or will it be shared?In local TensorFlow, sessions manage variable values, so if you create a graph "g" containing a variable "w", then start two threads and open a local session in each thread, both using the samge graph "g", then each session will have its own copy of the variable "w". However, in distributed TensorFlow, variable values are stored in containers mangaged by the cluster, so if both sessions connect to the same cluster and use the same container, then they will share the same variable values for "w". 6. When is a variable initialized? When is it destroyed?A variable is initialized when you call its initializer, and it is destroyed when the session ends. In distributed TensorFlow, variables live in containers on the cluster, so closing a session will not destroy the variable. To destroy a variable, you need to clear its container. 7. What is the difference between a placeholder and a variable?Variables and placeholders are extremely different, but beginners often confuse them:- A variable is an operation that holds a value. If you run the variable, it returns that value. Before you can run it, you need to initialize it. You can change the variable's value (for example, by using an assignement operation). It is stateful: the variable keeps the same value upon successive runs of the graph. It is typically used to hold model parameters but also for other purposes (e.g., to count the global training step).- Placeholders technically don't do much: they just hold information about the type and shape of the tensor they represent, but they have no value. In fact, if you try to evaluate an operation that depends on a placeholder, you must feed TensorFlow the value of the placeholder (using the "feed_dict" argument) or else you will get an exception. Placeholders are typically used to feed training or test data to TensorFlow during the execution phase. They are also useful to pass a value to an assignment node, to change the value of a variable (e.g., model weights). 8. What happens when you run the graph to evaluate an operation that depends on a placeholder but you don't feed its value? What happens if the operation does not depend on the placeholder?If you run the graph to evaluate an operation that depends on a placeholder but you don't feed its value, you get an exception. If the operation does not depend on the placeholder, then no exception is raised. 9. When you run a graph, can you feed the output value of any operation, or just the value of placeholders?When you run a graph, you can feed the output value of any operation, not just the value of placeholders. In practice, however, this is rather rare (it can be useful, for example, when you are caching the output of frozen layers; see Chapter 11). 10. How can you set a variable to any value you want (during the execution phase)?You can specify a variable's initial value when constructing the graph, and it will be initialized later when you run the variable's initializer during the execution phase. If you want to change that variable's value to anything you want during the execution phase, then the simplest option is to create an assignment node (during the graph construction phase) using the "tf.assign()" function, passing the variable and a placeholder as parameters. During the execution phase, you can run the assignment operation and feed the variable's new value using the placeholder.<jupyter_code>import tensorflow as tf                                             # import tensorflow
x = tf.Variable(tf.random_uniform(shape=(), minval=0.0,maxval=1.0)) # variable x
x_new_val = tf.placeholder(shape=(), dtype=tf.float32)              # placeholder x_new_val
x_assign = tf.assign(x, x_new_val)                                  # assignment node x_assign assigns x_new_val to x
with tf.Session():                                                  # start a tensorflow session
    x.initializer.run()                                             # initialize the variable x
    print(x.eval())                                                 # evaluate x (random number in [0,5]) and print it
    x_assign.eval(feed_dict={x_new_val: 5.0})                       # run x_assign while feeding a value to the ...
                                                                    # ... placeholder => updated x
    print(x.eval())                                                 # evaluate x again and print it<jupyter_output>/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:493: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:494: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:495: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:496: FutureWarning: Passing (type, 1) or [...]
{'fraction_non_alphanumeric': 0.044544405997693194, 'fraction_numerical': 0.007335640138408305, 'mean_word_length': 4.529464285714286, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '476644', 'license': 'Apache-2.0', 'n_tokens_mistral': 10186, 'n_tokens_neox': 9518, 'n_words': 6711, 'path': 'ExercisesWithoutCode.ipynb', 'repo_name': 'MRD-Git/Hands-On-Machine-Learning', 'size': '43350'}