数据挖掘——月亮数据

2023-11-11 19:10
文章标签 数据 数据挖掘 月亮

本文主要是介绍数据挖掘——月亮数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、问题描述

月亮数据是sklearn工具库提供的一个数据集。它上用于分类和聚类算法的实践实验。图中每一个点是一条数据。其中(x1,x2)是特征组,颜色是标签值。    

二、实验目的

学习决策树和随机森林

三、实验内容

1.数据导入:采用自动生成的数据

2.数据预处理:使用库函数进行数据处理

四、实验结果及分析

原始数据:

Max_depth=2:

Max_depth = 5:

五、遇到的问题和解决方法

图像处理的时候不太懂,参考别人的做的。

六、完整代码

decisionTreeBase.py

import numpy as np
from machine_learning.homework.week10.TreeNode import Nodeclass DecisionTreeBase:def __init__(self, max_depth, feature_sample_rate, get_score):self.max_depth = max_depthself.feature_sample_rate = feature_sample_rateself.get_score = get_scoredef split_data(self, j, theta, X, idx):idx1, idx2 = list(), list()for i in idx:value = X[i][j]if value <= theta:idx1.append(i)else:idx2.append(i)return idx1, idx2def get_random_features(self, n):shuffled = np.random.permutation(n)size = int(self.feature_sample_rate * n)selected = shuffled[:size]return selecteddef find_best_split(self, X, y, idx):m, n = X.shapebest_score = float("inf")best_j = -1best_theta = float("inf")best_idx1, best_idx2 = list(), list()selected_j = self.get_random_features(n)for j in selected_j:thetas = set([x[j] for x in X])for theta in thetas:idx1, idx2 = self.split_data(j, theta, X, idx)if min(len(idx1), len(idx2)) == 0:continuescore1, score2 = self.get_score(y, idx1), self.get_score(y, idx2)w = 1.0 * len(idx1) / len(idx)score = w * score1 + (1 - w) * score2if score < best_score:best_score = scorebest_j = jbest_theta = thetabest_idx1 = idx1best_idx2 = idx2return best_j, best_theta, best_idx1, best_idx2, best_scoredef generate_tree(self, X, y, idx, d):r = Node()r.p = np.average(y[idx], axis=0)if d == 0 or len(idx) < 2:return rcurrent_score = self.get_score(y, idx)j, theta, idx1, idx2, score = self.find_best_split(X, y, idx)if score >= current_score:return rr.j = jr.theta = thetar.left = self.generate_tree(X, y, idx1, d - 1)r.right = self.generate_tree(X, y, idx2, d - 1)return rdef fit(self, X, y):self.root = self.generate_tree(X, y, range(len(X)), self.max_depth)def get_prediction(self, r, x):if r.left == None and r.right == None:return r.pvalue = x[r.j]if value <= r.theta:return self.get_prediction(r.left, x)else:return self.get_prediction(r.right, x)def predict(self, X):y = list()for i in range(len(X)):y.append(self.get_prediction(self.root, X[i]))return np.array(y)

decisionTreeClassifier.py

import numpy as np
from machine_learning.homework.week10.decisionTreeBase import DecisionTreeBasedef get_impurity(y, idx):p = np.average(y[idx], axis=0)return 1 - p.dot(p.T)def get_entropy(y, idx):_, k = y.shapep = np.average(y[idx], axis=0)return - np.log(p + 0.001 * np.random.rand(k)).dot(p.T)class DecisionTreeClassifier(DecisionTreeBase):def __init__(self, max_depth=0, feature_sample_rate=1.0):super().__init__(max_depth=max_depth,feature_sample_rate=feature_sample_rate,get_score=get_entropy)def predict_proba(self, X):return super().predict(X)def predict(self, X):proba = self.predict_proba(X)return np.argmax(proba, axis=1)

moon.py

from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from machine_learning.homework.week10.decisionTreeClassifier import DecisionTreeClassifier
from machine_learning.homework.week10.randomForestClassifier import RandomForestClassifier
from sklearn.metrics import accuracy_score
import numpy as npdef convert_to_vector(y):m = len(y)k = np.max(y) + 1v = np.zeros(m * k).reshape(m,k)for i in range(m):v[i][y[i]] = 1return vX, y = make_moons(n_samples=1000, noise=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
plt.figure(0)
plt.axis([-1.5, 2.5, -0.75, 1.25 ])
plt.scatter(X_train[:, 0][y_train==0], X_train[:, 1][y_train==0], c='b', marker='o', s=10)
plt.scatter(X_train[:, 0][y_train==1], X_train[:, 1][y_train==1], c='r', marker='o', s=10)plt.show()tree = DecisionTreeClassifier(max_depth=5)
tree.fit(X_train, convert_to_vector(y_train))
y_pred = tree.predict(X_test)
print("decision tree accuracy= {}".format(accuracy_score(y_test, y_pred)))plt.figure(1)
x0s = np.linspace(-3, 4, 100)
x1s = np.linspace(-1, 6, 100)
x0, x1 = np.meshgrid(x0s, x1s)
W = np.c_[x0.ravel(), x1.ravel()]
u= tree.predict(W).reshape(x0.shape)
plt.axis([-1.5, 2.5, -0.75, 1.25 ])
plt.scatter(X_train[:, 0][y_train==0], X_train[:, 1][y_train==0], c='b', marker='o', s=30)
plt.scatter(X_train[:, 0][y_train==1], X_train[:, 1][y_train==1], c='g', marker='^', s=30)
plt.scatter(X_train[:, 0][y_train==2], X_train[:, 1][y_train==2], c='y', marker='s', s=30)
plt.contourf(x0, x1, u, c=u, alpha=0.2)
plt.show()
forest = RandomForestClassifier(max_depth=5, num_trees=100, feature_sample_rate=0.5, data_sample_rate=0.15)
forest.fit(X_train, convert_to_vector(y_train))
y_pred = forest.predict(X_test)
print("random forest accuracy= {}".format(accuracy_score(y_test, y_pred)))plt.figure(2)
u= forest.predict(W).reshape(x0.shape)
plt.axis([-1.5, 2.5, -0.75, 1.25 ])
plt.scatter(X_train[:, 0][y_train==0], X_train[:, 1][y_train==0], c='b', marker='o', s=30)
plt.scatter(X_train[:, 0][y_train==1], X_train[:, 1][y_train==1], c='g', marker='^', s=30)
plt.scatter(X_train[:, 0][y_train==2], X_train[:, 1][y_train==2], c='y', marker='s', s=30)
plt.contourf(x0, x1, u, c=u, alpha=0.2)plt.show()

randomForestClassifier.py

import numpy as np
from machine_learning.homework.week10.decisionTreeClassifier import DecisionTreeClassifierclass RandomForestClassifier:def __init__(self, num_trees, max_depth, feature_sample_rate,data_sample_rate, random_state=0):self.max_depth, self.num_trees = max_depth, num_treesself.feature_sample_rate = feature_sample_rateself.data_sample_rate = data_sample_rateself.trees = []np.random.seed(random_state)def get_data_samples(self, X, y):shuffled_indices = np.random.permutation(len(X))size = int(self.data_sample_rate * len(X))selected_indices = shuffled_indices[:size]return X[selected_indices], y[selected_indices]def fit(self, X, y):for t in range(self.num_trees):X_t, y_t = self.get_data_samples(X, y)model = DecisionTreeClassifier(max_depth=self.max_depth,feature_sample_rate=self.feature_sample_rate)model.fit(X_t, y_t)self.trees.append(model)def predict_proba(self, X):probas = np.array([tree.predict_proba(X) for tree in self.trees])return np.average(probas, axis=0)def predict(self, X):proba = self.predict_proba(X)return np.argmax(proba, axis=1)

TreeNode.py

# 树节点
class Node:j = Nonetheta = Nonep = Noneleft = Noneright = None

这篇关于数据挖掘——月亮数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/392228

相关文章

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据