<jupyter_start><jupyter_text>PCA<jupyter_code>X = recipes_df.drop('cuisine', axis=1) 
y = pd.DataFrame(recipes_df.cuisine)
y.set_index(X.index, inplace=True)
from sklearn.decomposition import PCA
# pca = PCA(n_components = 12)
pca = PCA().fit(X)

cum_expl_var = []
for i in range(len(pca.explained_variance_)):
    if i == 0:
        cum_expl_var.append(pca.explained_variance_[i])
    else:
        cum_expl_var.append(pca.explained_variance_[i] + cum_expl_var[i-1])
        
cum_expl_var_ratio_pca = np.cumsum(pca.explained_variance_ratio_)
cum_90 = np.where(cum_expl_var_ratio_pca >= 0.9)[0][0]

plt.figure(figsize=[8,5])
plt.style.use('ggplot')
plt.plot(cum_expl_var_ratio_pca, color="green")
plt.plot(cum_90, 0.9,'go') 
plt.title('Cumulative Explained Variance in PCA')
plt.xlabel('Number of Components')
plt.ylabel('Cumulative Explained Variance Ratio')
plt.xticks(np.arange(0, len(pca.components_), 50))
plt.annotate("CVR= 90%", (cum_90+20, 0.88)).set_fontsize(12)
plt.annotate("Component {}".format(cum_90+1), (cum_90+20, 0.83)).set_fontsize(12)
# plt.savefig('../figures/1-4.png', bbox_inches = 'tight', pad_inches = 0)
X_pca = PCA(n_components=175).fit_transform(X)
palette = sns.color_palette("tab20", n_colors = 12)
sns.set_style("whitegrid")

fig, ax = plt.subplots()
fig.set_size_inches(11.7, 8.27)

plt.title("Recipes by origin (PCA)", size=22, fontweight="bold")
for i, cur_origin in enumerate(np.unique(y)):
    plot = plt.scatter(X_pca[(y == cur_origin)['cuisine'], 1], X_pca[(y == cur_origin)['cuisine'], 2], 
                c = palette[i], label = cur_origin, s = 5, linewidth=3.5, alpha=0.8)
    plot.set_label(cuisines_df.cuisine_label[i])

plt.legend(fontsize=14, loc='best')
plt.savefig("pca.png")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17526942711287577, 'fraction_numerical': 0.03970504821327283, 'mean_word_length': 5.732824427480916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '508961', 'license': 'MIT', 'n_tokens_mistral': 808, 'n_tokens_neox': 735, 'n_words': 110, 'path': 'notebooks/PCA.ipynb', 'repo_name': 'marialuquea/ingredient_recommender_ai', 'size': '1763'}