Chapter 9: Hotspot Analysis
Activity 21: Estimating Density in One Dimension
Solution:
- Open a new notebook and install all the necessary libraries.
get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt import numpy import pandas import seaborn import sklearn.datasets import sklearn.model_selection import sklearn.neighbors seaborn.set()
- Sample 1,000 data points from the standard normal distribution. Add 3.5 to each of the last 625 values of the sample (that is, the indices between 375 and 1,000). To do this, set a random state of 100 using
numpy.random.RandomState
to guarantee the same sampled values, and then randomly generate the data points using therandn(1000)
call:rand = numpy.random.RandomState(100) vals = rand.randn(1000) # standard normal vals[375:] += 3.5
- Plot the 1,000-point sample data as a histogram and add a scatterplot below it:
fig, ax = plt.subplots(figsize=(14, 10)) ax.hist(vals, bins=50, density...