分类数据是一种可以划分为不同类别或组的数据类型。例如,数据集可能有一列的类别为“红色”、“绿色”和“蓝色”。处理分类数据可能具有挑战性,因为它不能以与数字数据相同的方式处理。
可视化和分析分类数据的一种方法是使用Bokeh,这是一个强大的Python库,用于创建交互式可视化。在本文中,我们将探讨如何使用Bokeh处理分类数据,并提供一些示例来说明这些概念。
概念
在深入研究使用Bokeh处理分类数据的细节之前,重要的是要了解一些相关概念。
- 分类数据:如上所述,分类数据是一种可以划分为不同类别或组的数据类型。它可以是名义的(没有固有的顺序)或顺序的(有固有的顺序)。
- 分类轴:在Bokeh中,分类轴用于表示图上的分类数据。类别沿轴绘制,每个类别由刻度线表示。
- 分类颜色映射:分类颜色映射是一种将不同颜色分配给图上不同类别的方法。这对于在视觉上区分不同类别很有用。
现在我们对概念有了基本的理解,让我们来看看使用Bokeh处理分类数据的步骤。
具体步骤
1.导入必要的库(Bokeh 和您可能需要的任何其他库)
2.使用分类数据创建数据集
3.创建一个图形对象,并将x_range或y_range设置为要绘制的类别
4.使用vbar()或hbar()方法绘制数据,将类别指定为x或y坐标,将值指定为顶部或右侧坐标
5.可选:通过设置宽度、添加网格线和设置范围起始值来自定义图的外观
6.使用show()函数显示绘图
现在我们已经完成了使用Bokeh 处理分类数据的步骤,让我们看一些例子来进一步说明这些概念。
示例1 - 简单条形图
#Import the necessary libraries
from bokeh.io import output_file, show
from bokeh.plotting import figure
from sklearn.datasets import load_breast_cancer
# Load the datasets
data = load_breast_cancer(as_frame=True)
# file to save the model
output_file("Breast Cancer.html")
# color of the wedges
color = ["orange","green"]
p = figure(x_range=data.target_names, height=350, title="breast cancer",toolbar_location=None, tools="")
p.vbar(x=data.target_names, top=data['target'].value_counts(), color=color, width=0.9)
show(p)
输出
代码说明:
output_file函数指定保存图表的HTML文件的名称和位置。
这里我们使用了scikit-learn库中的乳腺癌数据集
figure函数使用指定的选项创建新的图表图形:
x_range:x轴的标签,在本例中,乳腺癌为“恶性”或“良性”。
height:图表的高度(以像素为单位)
title:图表的标题
p.vbar函数向图表中添加竖条,x参数指定竖条的标签(乳腺癌状态),top参数指定竖条的高度(value_count)。width参数设置条形的宽度。
最后,show函数显示图表。当脚本运行时,将创建一个名为“bar_sorted.html”的HTML文件,其中包含图表。
示例2 - 嵌套条形图
# Import the necessary module
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
from sklearn.datasets import load_iris
# Load the iris dataset
data = load_iris(as_frame=True)
data= data.frame
# Replace 0, 1, 2 with respective iris flower name
data.target.replace({0: load_iris().target_names[0],
1:load_iris().target_names[1],
2:load_iris().target_names[2]},
inplace = True)
# CGroup the iris flower by the average length value
df = data.groupby('target').agg('mean')
# This will create a list with iris flower name and their respective sepal & petal length and width name
x = [ (cls, col) for cls in data.target.unique() for col in data.columns[:-1] ]
# This will contain the respective numerical for each x
mean = sum(zip(df['sepal length (cm)'],
df['sepal width (cm)'],
df['petal length (cm)'],
df['petal width (cm)']), ())
# create the dicti
source= ColumnDataSource(data=dict(x=x, Average = mean))
# file to save the model
output_file("Bokeh Nested Bar chart.html")
# FIgure
p = figure(x_range=FactorRange(*x),
height=350,
title="Iris Flower Average length",
)
#color
color = ['orange','#FF0000','green','#00FF00',]
# Vertical bar chaRT
p.vbar(x='x',
top='Average',
width=0.9,
source=source,
line_color="white",
fill_color=factor_cmap('x', palette=color, factors=data.columns[:-1], start=1, end=2)
)
# Orintation of text
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)
输出
代码说明:
在这里,我们使用了scikit-learn库中的iris flower数据集。
然后根据鸢尾花的名称对数据进行分组,计算萼片和花瓣的平均长度和宽度。
然后我们创建一个列表来存储每个变量及其各自的值,名称为’x’和’mean’。
figure函数创建一个新的图表图形。
x_range:x轴的标签,在本例中是鸢尾花的名称。
height:图表的高度(以像素为单位)
title:图表的标题
p.vbar函数的作用是:向图表中添加竖条,x参数指定竖条的标签,top参数指定竖条的高度(平均值)。width参数设置条形的宽度。
最后,show函数显示图表。当脚本运行时,将创建一个名为“bar_sorted.html”的HTML文件,其中包含图表。
结论
本文中,我们探讨了如何使用Bokeh处理分类数据,并提供了一些示例来说明这些概念。通过使用Bokeh,您可以轻松地可视化和分析分类数据,使其成为数据科学的宝贵工具。