<jupyter_start><jupyter_text>Kernel Density Estimation<jupyter_code>from sklearn.datasets import make_blobs

X, y = make_blobs(n_features=1, n_samples=30, random_state=1)
plt.hist(X);
plt.scatter(X.ravel(), -np.ones(len(X)))
plt.yticks(())
from sklearn.neighbors import KernelDensity

kde = KernelDensity(bandwidth=1).fit(X)
line = np.linspace(X.min() - 2, X.max() + 2, 100)
line_scores = np.exp(kde.score_samples(line[:, np.newaxis]))
plt.plot(line, line_scores)
plt.twinx().scatter(X.ravel(), np.ones(len(X)))
kde.score(X)
kde = KernelDensity(bandwidth=0.2).fit(X)
line = np.linspace(X.min() - 2, X.max() + 2, 1000)
line_scores = np.exp(kde.score_samples(line[:, np.newaxis]))
plt.plot(line, line_scores)
plt.twinx().scatter(X.ravel(), np.ones(len(X)))
 
kde.score(X)
from sklearn.grid_search import GridSearchCV
param_grid = {'bandwidth': np.logspace(-1, 1, 20)}
grid = GridSearchCV(KernelDensity(), param_grid, cv=10)
grid.fit(X)
grid.best_params_
line_scores = np.exp(grid.best_estimator_.score_samples(line[:, np.newaxis]))
plt.plot(line, line_scores)
plt.twinx().scatter(X.ravel(), -np.ones(len(X)))<jupyter_output><empty_output><jupyter_text>PCA as probabilistic model<jupyter_code>from sklearn.datasets import make_low_rank_matrix
from sklearn.decomposition import PCA

X = make_low_rank_matrix(n_features=100, effective_rank=10, random_state=0)
pca = PCA(n_components=20).fit(X)
pca.score(X)
pca = PCA(n_components=50).fit(X)
pca.score(X)
from sklearn.learning_curve import validation_curve
param_range = range(2, 40, 2)
training_scores, validation_scores = validation_curve(PCA(), X, None, param_name="n_components",
                                                      param_range=param_range, cv=10)
from figures import plot_validation_curve
plot_validation_curve(param_range, training_scores, validation_scores)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14870689655172414, 'fraction_numerical': 0.021551724137931036, 'mean_word_length': 6.339920948616601, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '11466', 'license': 'Apache-2.0', 'n_tokens_mistral': 758, 'n_tokens_neox': 700, 'n_words': 116, 'path': 'scikit/Chapter 5/Model Selection for Density Models.ipynb', 'repo_name': 'KarthikKothareddy/Data-Science-Practice', 'size': '1856'}