机器学习-初级入门(集群和关联规则)

一、集群和关联规则

  1. K-Means Clustering(K平均聚类算法 , 无监督学习)
    原理
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    组内平方和
    在这里插入图片描述
    选择类的个数
    在这里插入图片描述
    数据:

     CustomerID   Genre  Age  Annual Income (k$)  Spending Score (1-100)
             1    Male   19                  15                      39
             2    Male   21                  15                      81
             3  Female   20                  16                       6
             4  Female   23                  16                      77
             5  Female   31                  17                      40
             ...
    
    import warnings
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        from sklearn.cluster import KMeans
    import matplotlib.pyplot as plt
    import pandas as pd
    
    # Importing the dataset
    dataset = pd.read_csv('Mall_Customers.csv')
    
    X = dataset.iloc[:, 3:5].values
    
    # Using the elbow method to find the optimal number of cluster
    wcss = []
    for item in range(1, 11):
        kmeans = KMeans(n_clusters=item, max_iter=300, n_init=10, init="k-means++", random_state=0)  # n_clusters: 集群数, max_iter: 最大循环数, n_init: 对多少组的中心值进行计算, init:初始中心值的计算,k-means++可以解决初始值选取陷阱问题
        kmeans.fit(X)
        wcss.append(kmeans.inertia_)  # inertia: 每一次分类的组间距离之和
    plt.plot(range(1, 11), wcss)
    plt.title("The Elbow Method")
    plt.xlabel("Number of Clusters")
    plt.ylabel("WCSS")
    plt.show()
    
    # Applying the K-means to the mall dataset(由上面画图可知最佳分类是5)
    kmeans = KMeans(n_clusters=5, max_iter=300, n_init=10, init="k-means++",
                    random_state=0)  # n_clusters: 集群数, max_iter: 最大循环数, n_init: 对多少组的中心值进行计算, init:初始中心值的计算,k-means++可以解决随机初始化陷阱问题
    y_kmeans = kmeans.fit_predict(X)
    
    # Visualizing the clusters
    plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s=100, c="red", label="Careful")
    plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s=100, c="blue", label="Standard")
    plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s=100, c="green", label="Target")
    plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s=100, c="cyan", label="Careless")
    plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s=100, c="magenta", label="Sensible")
    plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=100, c="yellow", label="Cluster Center")
    plt.title("Cluster of clients")
    plt.xlabel("Annual Income (K$)")
    plt.ylabel("Spending Score (1-100)")
    plt.legend()
    plt.show()
    

    结果:
    分类个数和组内平岗和:在这里插入图片描述
    分类结果:
    在这里插入图片描述
    Careful: 关注人群,高收入低消费
    Standard: 标准消费人群,收入消费融洽
    Target: 目标人群,高收入高消费(推荐推广)
    Careless: 月光族,低收入高消费
    Sensible: 正常人群,低收入低消费

    随机初始中心点陷阱
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  2. 先验算法
    原理:
    在这里插入图片描述
    支持度:
    在这里插入图片描述
    信息水准:
    在这里插入图片描述
    提升度:
    在这里插入图片描述
    数据:

    shrimp,almonds,avocado,vegetables mix,green grapes,whole weat flour,yams,cottage cheese,energy drink,tomato juice,low fat yogurt,green tea,honey,salad,mineral water,salmon,antioxydant juice,frozen smoothie,spinach,olive oil
    burgers,meatballs,eggs
    chutney
    turkey,avocado
    mineral water,milk,energy bar,whole wheat rice,green tea
    ...
    
    from apyori import apriori
    import pandas as pd
    dataset = pd.read_csv('Market_Basket_Optimisation.csv', header=None)
    transactions = []
    for i in range(0, len(dataset)):
        transactions.append([str(dataset.values[i, j]) for j in range(0, 20)])
    
    # Training Apriori on the dataset
    rules = apriori(transactions, min_support=(3 * 7) / 7501, min_confidence=0.2, min_lift=3, min_length=2)
    
    # visualising the results
    results = list(rules)
    myResults = [list(x) for x in results]
    print(myResults)
    

    结果:

    [[frozenset({'extra dark chocolate', 'chicken'}),
      0.0027996267164378083,     # 支持度
      [OrderedStatistic(items_base=frozenset({'extra dark chocolate'}),
        items_add=frozenset({'chicken'}), 
        confidence=0.23333333333333334, 
        lift=3.8894074074074076)]],  .....
    ]
    这个结果的意思是说如果这个用户买了extra dark chocolate他就有23.333%的可能买chicken
    

    apyori 包:

    #!/usr/bin/env python
    
    """
    a simple implementation of Apriori algorithm by Python.
    """
    
    import sys
    import csv
    import argparse
    import json
    import os
    from collections import namedtuple
    from itertools import combinations
    from itertools import chain
    
    
    # Meta informations.
    __version__ = '1.1.1'
    __author__ = 'Yu Mochizuki'
    __author_email__ = 'ymoch.dev@gmail.com'
    
    
    ################################################################################
    # Data structures.
    ################################################################################
    class TransactionManager(object):
        """
        Transaction managers.
        """
    
        def __init__(self, transactions):
            """
            Initialize.
    
            Arguments:
                transactions -- A transaction iterable object
                                (eg. [['A', 'B'], ['B', 'C']]).
            """
            self.__num_transaction = 0
            self.__items = []
            self.__transaction_index_map = {}
    
            for transaction in transactions:
                self.add_transaction(transaction)
    
        def add_transaction(self, transaction):
            """
            Add a transaction.
    
            Arguments:
                transaction -- A transaction as an iterable object (eg. ['A', 'B']).
            """
            for item in transaction:
                if item not in self.__transaction_index_map:
                    self.__items.append(item)
                    self.__transaction_index_map[item] = set()
                self.__transaction_index_map[item].add(self.__num_transaction)
            self.__num_transaction += 1
    
        def calc_support(self, items):
            """
            Returns a support for items.
    
            Arguments:
                items -- Items as an iterable object (eg. ['A', 'B']).
            """
            # Empty items is supported by all transactions.
            if not items:
                return 1.0
    
            # Empty transactions supports no items.
            if not self.num_transaction:
                return 0.0
    
            # Create the transaction index intersection.
            sum_indexes = None
            for item in items:
                indexes = self.__transaction_index_map.get(item)
                if indexes is None:
                    # No support for any set that contains a not existing item.
                    return 0.0
    
                if sum_indexes is None:
                    # Assign the indexes on the first time.
                    sum_indexes = indexes
                else:
                    # Calculate the intersection on not the first time.
                    sum_indexes = sum_indexes.intersection(indexes)
    
            # Calculate and return the support.
            return float(len(sum_indexes)) / self.__num_transaction
    
        def initial_candidates(self):
            """
            Returns the initial candidates.
            """
            return [frozenset([item]) for item in self.items]
    
        @property
        def num_transaction(self):
            """
            Returns the number of transactions.
            """
            return self.__num_transaction
    
        @property
        def items(self):
            """
            Returns the item list that the transaction is consisted of.
            """
            return sorted(self.__items)
    
        @staticmethod
        def create(transactions):
            """
            Create the TransactionManager with a transaction instance.
            If the given instance is a TransactionManager, this returns itself.
            """
            if isinstance(transactions, TransactionManager):
                return transactions
            return TransactionManager(transactions)
    
    
    # Ignore name errors because these names are namedtuples.
    SupportRecord = namedtuple( # pylint: disable=C0103
        'SupportRecord', ('items', 'support'))
    RelationRecord = namedtuple( # pylint: disable=C0103
        'RelationRecord', SupportRecord._fields + ('ordered_statistics',))
    OrderedStatistic = namedtuple( # pylint: disable=C0103
        'OrderedStatistic', ('items_base', 'items_add', 'confidence', 'lift',))
    
    
    ################################################################################
    # Inner functions.
    ################################################################################
    def create_next_candidates(prev_candidates, length):
        """
        Returns the apriori candidates as a list.
    
        Arguments:
            prev_candidates -- Previous candidates as a list.
            length -- The lengths of the next candidates.
        """
        # Solve the items.
        item_set = set()
        for candidate in prev_candidates:
            for item in candidate:
                item_set.add(item)
        items = sorted(item_set)
    
        # Create the temporary candidates. These will be filtered below.
        tmp_next_candidates = (frozenset(x) for x in combinations(items, length))
    
        # Return all the candidates if the length of the next candidates is 2
        # because their subsets are the same as items.
        if length < 3:
            return list(tmp_next_candidates)
    
        # Filter candidates that all of their subsets are
        # in the previous candidates.
        next_candidates = [
            candidate for candidate in tmp_next_candidates
            if all(
                True if frozenset(x) in prev_candidates else False
                for x in combinations(candidate, length - 1))
        ]
        return next_candidates
    
    
    def gen_support_records(transaction_manager, min_support, **kwargs):
        """
        Returns a generator of support records with given transactions.
    
        Arguments:
            transaction_manager -- Transactions as a TransactionManager instance.
            min_support -- A minimum support (float).
    
        Keyword arguments:
            max_length -- The maximum length of relations (integer).
        """
        # Parse arguments.
        max_length = kwargs.get('max_length')
    
        # For testing.
        _create_next_candidates = kwargs.get(
            '_create_next_candidates', create_next_candidates)
    
        # Process.
        candidates = transaction_manager.initial_candidates()
        length = 1
        while candidates:
            relations = set()
            for relation_candidate in candidates:
                support = transaction_manager.calc_support(relation_candidate)
                if support < min_support:
                    continue
                candidate_set = frozenset(relation_candidate)
                relations.add(candidate_set)
                yield SupportRecord(candidate_set, support)
            length += 1
            if max_length and length > max_length:
                break
            candidates = _create_next_candidates(relations, length)
    
    
    def gen_ordered_statistics(transaction_manager, record):
        """
        Returns a generator of ordered statistics as OrderedStatistic instances.
    
        Arguments:
            transaction_manager -- Transactions as a TransactionManager instance.
            record -- A support record as a SupportRecord instance.
        """
        items = record.items
        for combination_set in combinations(sorted(items), len(items) - 1):
            items_base = frozenset(combination_set)
            items_add = frozenset(items.difference(items_base))
            confidence = (
                record.support / transaction_manager.calc_support(items_base))
            lift = confidence / transaction_manager.calc_support(items_add)
            yield OrderedStatistic(
                frozenset(items_base), frozenset(items_add), confidence, lift)
    
    
    def filter_ordered_statistics(ordered_statistics, **kwargs):
        """
        Filter OrderedStatistic objects.
    
        Arguments:
            ordered_statistics -- A OrderedStatistic iterable object.
    
        Keyword arguments:
            min_confidence -- The minimum confidence of relations (float).
            min_lift -- The minimum lift of relations (float).
        """
        min_confidence = kwargs.get('min_confidence', 0.0)
        min_lift = kwargs.get('min_lift', 0.0)
    
        for ordered_statistic in ordered_statistics:
            if ordered_statistic.confidence < min_confidence:
                continue
            if ordered_statistic.lift < min_lift:
                continue
            yield ordered_statistic
    
    
    ################################################################################
    # API function.
    ################################################################################
    def apriori(transactions, **kwargs):
        """
        Executes Apriori algorithm and returns a RelationRecord generator.
    
        Arguments:
            transactions -- A transaction iterable object
                            (eg. [['A', 'B'], ['B', 'C']]).
    
        Keyword arguments:
            min_support -- The minimum support of relations (float).
            min_confidence -- The minimum confidence of relations (float).
            min_lift -- The minimum lift of relations (float).
            max_length -- The maximum length of the relation (integer).
        """
        # Parse the arguments.
        min_support = kwargs.get('min_support', 0.1)
        min_confidence = kwargs.get('min_confidence', 0.0)
        min_lift = kwargs.get('min_lift', 0.0)
        max_length = kwargs.get('max_length', None)
    
        # Check arguments.
        if min_support <= 0:
            raise ValueError('minimum support must be > 0')
    
        # For testing.
        _gen_support_records = kwargs.get(
            '_gen_support_records', gen_support_records)
        _gen_ordered_statistics = kwargs.get(
            '_gen_ordered_statistics', gen_ordered_statistics)
        _filter_ordered_statistics = kwargs.get(
            '_filter_ordered_statistics', filter_ordered_statistics)
    
        # Calculate supports.
        transaction_manager = TransactionManager.create(transactions)
        support_records = _gen_support_records(
            transaction_manager, min_support, max_length=max_length)
    
        # Calculate ordered stats.
        for support_record in support_records:
            ordered_statistics = list(
                _filter_ordered_statistics(
                    _gen_ordered_statistics(transaction_manager, support_record),
                    min_confidence=min_confidence,
                    min_lift=min_lift,
                )
            )
            if not ordered_statistics:
                continue
            yield RelationRecord(
                support_record.items, support_record.support, ordered_statistics)
    
    
    ################################################################################
    # Application functions.
    ################################################################################
    def parse_args(argv):
        """
        Parse commandline arguments.
    
        Arguments:
            argv -- An argument list without the program name.
        """
        output_funcs = {
            'json': dump_as_json,
            'tsv': dump_as_two_item_tsv,
        }
        default_output_func_key = 'json'
    
        parser = argparse.ArgumentParser()
        parser.add_argument(
            '-v', '--version', action='version',
            version='%(prog)s {0}'.format(__version__))
        parser.add_argument(
            'input', metavar='inpath', nargs='*',
            help='Input transaction file (default: stdin).',
            type=argparse.FileType('r'), default=[sys.stdin])
        parser.add_argument(
            '-o', '--output', metavar='outpath',
            help='Output file (default: stdout).',
            type=argparse.FileType('w'), default=sys.stdout)
        parser.add_argument(
            '-l', '--max-length', metavar='int',
            help='Max length of relations (default: infinite).',
            type=int, default=None)
        parser.add_argument(
            '-s', '--min-support', metavar='float',
            help='Minimum support ratio (must be > 0, default: 0.1).',
            type=float, default=0.1)
        parser.add_argument(
            '-c', '--min-confidence', metavar='float',
            help='Minimum confidence (default: 0.5).',
            type=float, default=0.5)
        parser.add_argument(
            '-t', '--min-lift', metavar='float',
            help='Minimum lift (default: 0.0).',
            type=float, default=0.0)
        parser.add_argument(
            '-d', '--delimiter', metavar='str',
            help='Delimiter for items of transactions (default: tab).',
            type=str, default='\t')
        parser.add_argument(
            '-f', '--out-format', metavar='str',
            help='Output format ({0}; default: {1}).'.format(
                ', '.join(output_funcs.keys()), default_output_func_key),
            type=str, choices=output_funcs.keys(), default=default_output_func_key)
        args = parser.parse_args(argv)
    
        args.output_func = output_funcs[args.out_format]
        return args
    
    
    def load_transactions(input_file, **kwargs):
        """
        Load transactions and returns a generator for transactions.
    
        Arguments:
            input_file -- An input file.
    
        Keyword arguments:
            delimiter -- The delimiter of the transaction.
        """
        delimiter = kwargs.get('delimiter', '\t')
        for transaction in csv.reader(input_file, delimiter=delimiter):
            yield transaction if transaction else ['']
    
    
    def dump_as_json(record, output_file):
        """
        Dump an relation record as a json value.
    
        Arguments:
            record -- A RelationRecord instance to dump.
            output_file -- A file to output.
        """
        def default_func(value):
            """
            Default conversion for JSON value.
            """
            if isinstance(value, frozenset):
                return sorted(value)
            raise TypeError(repr(value) + " is not JSON serializable")
    
        converted_record = record._replace(
            ordered_statistics=[x._asdict() for x in record.ordered_statistics])
        json.dump(
            converted_record._asdict(), output_file,
            default=default_func, ensure_ascii=False)
        output_file.write(os.linesep)
    
    
    def dump_as_two_item_tsv(record, output_file):
        """
        Dump a relation record as TSV only for 2 item relations.
    
        Arguments:
            record -- A RelationRecord instance to dump.
            output_file -- A file to output.
        """
        for ordered_stats in record.ordered_statistics:
            if len(ordered_stats.items_base) != 1:
                continue
            if len(ordered_stats.items_add) != 1:
                continue
            output_file.write('{0}\t{1}\t{2:.8f}\t{3:.8f}\t{4:.8f}{5}'.format(
                list(ordered_stats.items_base)[0], list(ordered_stats.items_add)[0],
                record.support, ordered_stats.confidence, ordered_stats.lift,
                os.linesep))
    
    
    def main(**kwargs):
        """
        Executes Apriori algorithm and print its result.
        """
        # For tests.
        _parse_args = kwargs.get('_parse_args', parse_args)
        _load_transactions = kwargs.get('_load_transactions', load_transactions)
        _apriori = kwargs.get('_apriori', apriori)
    
        args = _parse_args(sys.argv[1:])
        transactions = _load_transactions(
            chain(*args.input), delimiter=args.delimiter)
        result = _apriori(
            transactions,
            max_length=args.max_length,
            min_support=args.min_support,
            min_confidence=args.min_confidence)
        for record in result:
            args.output_func(record, args.output)
    
    
    if __name__ == '__main__':
        main()
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值