<jupyter_start><jupyter_text>Normalization with Z Score<jupyter_code>from scipy import stats
z_scores = stats.zscore(sparse_matrix_drop)

z_scores_prep = pd.DataFrame(z_scores)
z_scores_matrix = z_scores_prep.dropna(axis='columns')
z_scores_matrix.columns = sparse_matrix_drop.columns
z_scores_matrix.index = sparse_matrix_drop.index
z_scores_matrix.head()<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>from sklearn.neighbors import NearestNeighbors

n_matrix= z_scores_matrix
nbrs = NearestNeighbors(n_neighbors=4, algorithm='auto').fit(n_matrix)
distances, indexes = nbrs.kneighbors(n_matrix)
neighbours=indexes
neighbours
NUun_trans = df.groupby('visitorid')['transactionid'].nunique()
NUun_trans
NUun_trans.mean()
itemdf = pd.DataFrame(df['itemid'].unique())
itemdf
itemdf.shape

#itemdf.count()
visitordf = pd.DataFrame(df['visitorid'].unique())
visitordf.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11538461538461539, 'fraction_numerical': 0.001098901098901099, 'mean_word_length': 10.831168831168831, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '567143', 'license': 'Apache-2.0', 'n_tokens_mistral': 366, 'n_tokens_neox': 344, 'n_words': 43, 'path': 'KNN RecSys .ipynb', 'repo_name': 'datasciencesociety/E-Commerce-Case-Recommender-Systems-', 'size': '910'}