<jupyter_start><jupyter_text>scikit-learnStrona biblioteki: [https://scikit-learn.org](https://scikit-learn.org)  Dokumentacja/User Guide: [https://scikit-learn.org/stable/user_guide.html](https://scikit-learn.org/stable/user_guide.html)Podstawowa biblioteka do uczenia maszynowego w języku Python.Aby zainstalować bibliotekę scikit-learn, użyj polecenia poniżej:```!pip install scikit-learn```Aby zaktualizować do najnowszej wersji bibliotekę scikit-learn, użyj polecenia poniżej:```!pip install --upgrade scikit-learn```Kurs stworzony w oparciu o wersję `0.22.1` Spis treści:1. [Import bibliotek](0)2. [Wygenerowanie danych](1)3. [Przygotowanie danych](2)4. [Algorytm Apriori](3)   Import bibliotek<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Wygenerowanie danych<jupyter_code>data = {'produkty': ['chleb jajka mleko', 'mleko ser', 'chleb masło ser', 'chleb jajka']}

transactions = pd.DataFrame(data=data, index=[1, 2, 3, 4])
transactions<jupyter_output><empty_output><jupyter_text>Przygotowanie danych<jupyter_code># rozwinięcie kolumny do obiektu DataFrame
expand = transactions['produkty'].str.split(expand=True)
expand
# wydobycie nazw wszystkich produktów
products = []
for col in expand.columns:
    for product in expand[col].unique():
        if product is not None and product not in products:
            products.append(product)

products.sort()
print(products)
transactions_encoded = np.zeros((len(transactions), len(products)), dtype='int8')
transactions_encoded
# kodowanie 0-1
for row in zip(range(len(transactions)), transactions_encoded, expand.values):
    for idx, product in enumerate(products):
        if product in row[2]:
            transactions_encoded[row[0], idx] = 1

transactions_encoded
transactions_encoded_df = pd.DataFrame(transactions_encoded, columns=products)
transactions_encoded_df<jupyter_output><empty_output><jupyter_text>Algorytm Apriori<jupyter_code>from mlxtend.frequent_patterns import apriori, association_rules

supports = apriori(transactions_encoded_df, min_support=0.0, use_colnames=True)
supports
supports = apriori(transactions_encoded_df, min_support=0.3, use_colnames=True)
supports
rules = association_rules(supports, metric='confidence', min_threshold=0.65)
rules = rules.iloc[:, [0, 1, 4, 5, 6]]
rules
rules.sort_values(by='lift', ascending=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11678526580159063, 'fraction_numerical': 0.014231896190874843, 'mean_word_length': 7.129251700680272, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '31359', 'license': 'MIT', 'n_tokens_mistral': 891, 'n_tokens_neox': 872, 'n_words': 171, 'path': 'unsupervised/03_association_rules/01_apriori.ipynb', 'repo_name': 'krakowiakpawel9/machine-learning-bootcamp', 'size': '2389'}