Esempio n. 1
0
def nmf_run():
    """
    非负矩阵分解 run
    :return:
    """
    # 1、导入用户商品矩阵
    print("----------- 1、load data -----------")
    data_matrix = FTool.LoadData(file_name="data.txt").load_data_with_none()
    # 2、利用梯度下降法对矩阵进行分解
    print("----------- 2、training -----------")
    w_matrix, h_matrix = train(data_matrix, 5, 10000, 1e-5)
    # 3、保存分解后的结果
    print("----------- 3、save decompose -----------")
    with FTool.SaveModel(file_name="w_matrix") as save_file:
        save_file.save_model_mul(w_matrix)
    with FTool.SaveModel(file_name="h_matrix") as save_file:
        save_file.save_model_mul(h_matrix)
    # 4、预测
    print("----------- 4、prediction -----------")
    predict = prediction(data_matrix, w_matrix, h_matrix, 0)
    # 进行Top-K推荐
    print("----------- 5、top_k recommendation ------------")
    top_recommend = top_k(predict, 2)
    print(top_recommend)
    print(w_matrix * h_matrix)
Esempio n. 2
0
def mf_run():
    """
    Matrix Factorization-based Recommend run
    :return:
    """
    # 1、导入用户商品矩阵
    print("----------- 1、load data -----------")
    data_matrix = FTool.LoadData(file_name="data.txt").load_data_with_none()
    # 2、利用梯度下降法对矩阵进行分解
    print("----------- 2、training -----------")
    p, q = grad_ascent(data_matrix, 3, 0.0002, 0.02,
                       5000)  # 由于样本过少, 迭代次数增加min loss也不会继续收敛
    # 3、保存分解后的结果
    print("----------- 3、save decompose -----------")
    with FTool.SaveModel(file_name="p_result") as save_file:
        save_file.save_model_mul(p)
    with FTool.SaveModel(file_name="q_result") as save_file:
        save_file.save_model_mul(q)
    # 4、预测
    print("----------- 4、prediction -----------")
    predict = prediction(data_matrix, p, q, 0)
    # 进行Top-K推荐
    print("----------- 5、top_k recommendation ------------")
    top_recommend = top_k(predict, 2)
    print(top_recommend)
    print(p * q)
Esempio n. 3
0
def mean_shift():
    """
    Mean Shift 执行函数
    :return:
    """
    # 导入数据集
    print("----------1.load data ------------")
    data = load_data("data", 2)
    x_data = [_data[0] for _data in data]
    y_data = [_data[1] for _data in data]
    with FTool.PaintingWithList(name="Mean Shift") as paint:
        paint.painting_simple_list(x_data, y_data)
    # 训练,h=2
    print("----------2.training ------------")
    points, shift_points, cluster = train_mean_shift(data, 2)
    # 保存所属的类别文件
    print("----------3.1.save sub ------------")
    with FTool.SaveModel(file_name="sub_1") as save_model:
        save_model.save_model_mul(np.mat(cluster))
    print("----------3.2.save center ------------")
    # 保存聚类中心
    with FTool.SaveModel(file_name="center_1") as save_model:
        save_model.save_model_mul(shift_points)
    after_x = list()
    after_y = list()
    for _position in range(len(shift_points)):
        after_x.append(shift_points[_position, 0])
        after_y.append(shift_points[_position, 1])
    with FTool.PaintingWithList(name="Mean Shift Result") as paint:
        paint.painting_list_with_label(after_x, after_y, cluster)
Esempio n. 4
0
def k_means_pp():
    """
    K-Means ++ 实现
    :return:
    """
    k = 4  # 聚类中心的个数
    file_path = "data.txt"
    # 1、导入数据
    print("---------- 1.load data ------------")
    data, _, _ = FTool.LoadData(file_name=file_path).load_data(
        feature_end=0, need_label_length=True, need_list=True)
    x_data = [_data[0] for _data in data]
    y_data = [_data[1] for _data in data]
    with FTool.PaintingWithList(name="K-Means") as paint:
        paint.painting_simple_list(x_data, y_data)
    # 2、KMeans++的聚类中心初始化方法
    print("---------- 2.K-Means++ generate centers ------------")
    data = np.mat(data)
    centroids = get_centroids(data, k)
    # 3、聚类计算
    print("---------- 3.k-Means ------------")
    sub_center = k_means_function(data, k, centroids)
    # 4、保存所属的类别文件
    print("---------- 4.save subCenter ------------")
    with FTool.SaveModel(file_name="sub_pp") as save_model:
        save_model.save_model_mul(sub_center)
    # 5、保存聚类中心
    print("---------- 5.save centroids ------------")
    with FTool.SaveModel(file_name="center_pp") as save_model:
        save_model.save_model_mul(centroids)
Esempio n. 5
0
def k_means(k_class: int):
    """
    The K-Means Function
    :param k_class: 聚类中心的个数
    :return:
    """
    k = k_class  # 聚类中心的个数
    file_path = "data.txt"
    # 1、导入数据
    print("---------- 1.load data ------------")
    data, _, _ = FTool.LoadData(file_name=file_path).load_data(feature_end=0, need_label_length=True, need_list=True)
    x_data = [_data[0] for _data in data]
    y_data = [_data[1] for _data in data]
    with FTool.PaintingWithList(name="K-Means") as paint:
        paint.painting_simple_list(x_data, y_data)
    # 2、随机初始化k个聚类中心
    print("---------- 2.random center ------------")
    data = np.mat(data)
    centroids = rand_cent(data, k)
    # 3、聚类计算
    print("---------- 3.kmeans ------------")
    sub_center = k_means_function(data, k, centroids)
    # 4、保存所属的类别文件
    print("---------- 4.save subCenter ------------")
    with FTool.SaveModel(file_name="sub") as save_model:
        save_model.save_model_mul(sub_center)
    # 5、保存聚类中心
    print("---------- 5.save centroids ------------")
    with FTool.SaveModel(file_name="center") as save_model:
        save_model.save_model_mul(centroids)