介绍
R中有一个名为ggplot2的可视化包,这个Python版本是情节九是。
当谈到 Python 中的可视化时,matplotlib,海运是有代表性的。除此以外散景或者情节等等。有多种选择,但 ggplot2 (=情节九),这也很好。
它看起来很漂亮,而且因为它很漂亮,所以它的可读性也很高。
在本文中,关于 sklearn wine 数据集情节九绘制独特的直方图、箱线图和散点图。
执行条件等
- 在谷歌 colab 上运行
- 可以读取任意数据集和sklearn等数据集。执行
葡萄酒数据集是对来自意大利同一地区的三种不同葡萄酒进行化学分析的结果。
- 酒精:酒精含量
- malic_acid:苹果酸
- 灰:灰
- alcalinity_of_ash:灰的碱度
- 镁:镁
- total_phenols:总酚含量
- 类黄酮:类黄酮
- nonflavanoid_phenols:非黄酮酚
- 原花青素:原花青素
- color_intensity:颜色强度
- 色相:色相
- od280/od315_of_diluted_wines:稀释葡萄酒在 280nm 和 315nm 处的吸光度比
- 脯氨酸:脯氨酸
- 目标:class_0、class_1、class_2(酒类)
* 从 alchol 到 proline 共有 13 个解释变量,目标变量是酒的类型(3 种)。
首先,我想知道是否有任何解释变量可以表达葡萄酒类型之间的差异。关于它。
Pairplot 在这种情况下非常有用。
读取数据集后,seaborn 分析仪我试着用
seaborn-analyzer 的 pairplot 具有高度可读性并且非常好。
似乎很难仅用一个变量来捕捉葡萄酒类型的差异,但首先,让我们看一下酒精与 plotnine 的直方图。
由于这个直方图没有捕捉到任何特征,我绘制了一个按葡萄酒类型分层的直方图。
有很多重叠之处,但您可以清楚地看到每种葡萄酒的酒精含量似乎都不同。
接下来,让我们并排绘制每种葡萄酒的直方图。
plotnine 的特点之一是可以很容易地完成这样的表达式。
以同样的方式绘制箱线图。
您可以更直观地看到不同葡萄酒类型的酒精中位数差异。
接下来,以与上面直方图相同的方式为每种类型的葡萄酒绘制水平箱线图。
我不需要独立绘制这个,但是 plotnine 可以很容易地绘制这样的。接下来是散点图。
在我先画的pairplot中,酒精和od280/od315_of_dliluted_wines的关系似乎可以区分酒的种类,所以就用这个画了。
很容易理解,因为它是按层显示的。接下来,让我们并排绘制每种葡萄酒的散点图。
这很容易理解。plotnine 也可以显示线性回归。
以下供您参考seaborn 分析仪它是用 class_separator_plot in 绘制的。
它通过 SVC 可视化分类。在最后,情节九我将介绍分面设置。
下面是使用 facet 设置的 Titanic 数据集的可视化。
X轴:年龄(乘客年龄),Y轴:票价+活着(生死)颜色分类(分层)+每个性别(性别)和等级(舱位等级)的矩阵的散点图。
在男性和女性方面,女性的生存率压倒男性。
至于男人,二三等几乎全军覆没,但只有一等存活率高。
此外,蹒跚学步的孩子似乎在每节课上都得救了。
对于女性来说,存活率似乎很高,但三等舱的死亡人数却是可观的。
“妇女儿童得到保护”,“富人优先”。通过以这种方式使用构面设置,可以排列具有高可读性的图形。
执行代码
下边是seaborn 分析仪是安装。
安装 seaborn-analyzer!pip install seaborn-analyzer下面通过Google Colab的form函数设置数据集,这样就可以在下拉列表中选择数据集了。
数据集选择#@title Select_Dataset { run: "auto" } #@markdown **<font color= "Crimson">注意</font>:かならず 実行する前に 設定してください。**</font> dataset = 'wine : classification' #@param ['Boston_housing :regression', 'Diabetes :regression', 'Breast_cancer :binary','Titanic :binary', 'Iris :classification', 'Loan_prediction :binary',"wine : classification", 'Upload']下面是读取上面下拉列表中数据集的代码。读取后将数据存储在数据框中。
读取数据集⇒存储数据帧#@title Load dataset #ライブラリインポート import numpy as np import pandas as pd import seaborn as sns import warnings warnings.simplefilter('ignore') if dataset =='Upload': from google.colab import files uploaded = files.upload()#Upload target = list(uploaded.keys())[0] df = pd.read_csv(target) elif dataset == "Diabetes :regression": from sklearn.datasets import load_diabetes dataset = load_diabetes() df = pd.DataFrame(dataset.data, columns=dataset.feature_names) df["target"] = dataset.target elif dataset == "Breast_cancer :binary": from sklearn.datasets import load_breast_cancer dataset = load_breast_cancer() df = pd.DataFrame(dataset.data, columns=dataset.feature_names) df["target"] = dataset.target elif dataset == "Titanic :binary": data_url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv" df = pd.read_csv(data_url) X = df.drop(['Survived'], axis=1) # 目的変数を除いたデータ y = df['Survived'] # 目的変数 df = pd.concat([X, y], axis=1) elif dataset == "Titanic(seaborn) :binary": df = sns.load_dataset('titanic') X = df.drop(['survived','pclass','embarked','who','adult_male','alive'], axis=1) # 重複しているカラムを除いたデータ y = df['alive'] # 目的変数 df = pd.concat([X, y], axis=1) elif dataset == "Iris :classification": from sklearn.datasets import load_iris iris = load_iris() df = pd.DataFrame(iris.data, columns = iris.feature_names) df['target'] = iris.target_names[iris.target] elif dataset == "wine : classification": from sklearn.datasets import load_wine wine = load_wine() df = pd.DataFrame(wine.data, columns = wine.feature_names) df['target'] = wine.target_names[wine.target] elif dataset == "Loan_prediction :binary": data_url = "https://github.com/shrikant-temburwar/Loan-Prediction-Dataset/raw/master/train.csv" df = pd.read_csv(data_url) else: from sklearn.datasets import load_boston dataset = load_boston() df = pd.DataFrame(dataset.data, columns=dataset.feature_names) df["target"] = dataset.target #X = pd.DataFrame(dataset.data, columns=dataset.feature_names) #y = pd.Series(dataset.target, name='target') source = df.copy() FEATURES = df.columns[:-1] TARGET = df.columns[-1] X = df.loc[:, FEATURES] y = df.loc[:, TARGET] df.info(verbose=True) df.head()下面,这些列按数据类型分类并显示。
*笔记:category 和 bool 也分为 Numerical_columns:。按数据类型分隔列#@title Datasetの数字・文字列区分 numerical_col = [] Object_col = [] for col_name, item in df.iteritems(): if item.dtype == object: Object_col.append(col_name) else: numerical_col.append(col_name) print('-----------------------------------------------------------------------------------------') print('Numerical_columns:', numerical_col) print('-----------------------------------------------------------------------------------------') print('Object_columns:', Object_col) print('-----------------------------------------------------------------------------------------')下边是seaborn 分析仪这是一个由运行的pairplot
配对图显示#@title Pairplot from seaborn_analyzer import CustomPairPlot cp = CustomPairPlot() cp.pairanalyzer(df, hue=TARGET)直方图
直方图#@title Histgram from plotnine import * Column_name = 'alcohol'#@param {type:"raw"} bins_number_slider = 13 #@param {type:"slider", min:5, max:20, step:1} (ggplot(df, aes(Column_name))+ geom_histogram(bins = bins_number_slider))叠加直方图#@title Stratified histogram by target Column_name = 'alcohol'#@param {type:"raw"} bins_number_slider = 13 #@param {type:"slider", min:5, max:20, step:1} from plotnine import * (ggplot(df, aes(Column_name, fill = TARGET)) + geom_histogram(bins = bins_number_slider, position = "identity", alpha = 0.7) )按类别的直方图#@title Histogram for each target variable Column_name = 'alcohol'#@param {type:"raw"} from plotnine import * (ggplot(df, aes(x = Column_name, fill = TARGET)) + geom_histogram() + facet_wrap(TARGET))箱形图
箱形图#@title Stratified box-plot by target Column_name = 'alcohol'#@param {type:"raw"} from plotnine import * (ggplot(df, aes(TARGET, Column_name, fill = TARGET)) + geom_boxplot())箱线图按类别#@title box-plot for each target variable Column_name = 'alcohol'#@param {type:"raw"} from plotnine import * (ggplot(df, aes(TARGET, Column_name, fill = TARGET)) + geom_boxplot() + facet_wrap(TARGET))散点图
散点图#@title Stratified scatter-plot by target X_column_name = 'alcohol'#@param {type:"raw"} y_column_name = 'od280/od315_of_diluted_wines'#@param {type:"raw"} from plotnine import * (ggplot(df, aes(x=X_column_name, y=y_column_name, color= TARGET)) +geom_point())按类别的散点图#@title Scatter-plot for each target variable X_column_name = 'alcohol'#@param {type:"raw"} y_column_name = 'od280/od315_of_diluted_wines'#@param {type:"raw"} from plotnine import * (ggplot(df,aes(x=X_column_name, y=y_column_name, color=TARGET)) + geom_point() + facet_wrap(TARGET))每个类别的线性回归散点图#@title Scatter-plot for each target variable with linear regression X_column_name = 'alcohol'#@param {type:"raw"} y_column_name = 'od280/od315_of_diluted_wines'#@param {type:"raw"} from plotnine import * (ggplot(df, aes(x=X_column_name, y=y_column_name, color = TARGET)) + geom_point() + stat_smooth(method='lm') + facet_wrap(TARGET))class_separator_plot
seaborn-analyzer 的 class_separator_plot#@title class_separator_plot X_column_name = 'alcohol'#@param {type:"raw"} y_column_name = 'od280/od315_of_diluted_wines'#@param {type: "raw"} import seaborn as sns from sklearn.svm import SVC from seaborn_analyzer import classplot clf = SVC() classplot.class_separator_plot(clf, [X_column_name, y_column_name], TARGET, df)带有分面函数的散点图
带有分面函数的散点图#@title scatter plot with facet X_column_name = 'age'#@param {type:"raw"} y_column_name = 'fare'#@param {type: "raw"} facet = 'sex~class'#@param {type: "raw"} from plotnine import * ( ggplot(df) + facet_grid(facets = facet) #facetは 'sex~class'のように記述 + aes(x= X_column_name, y= y_column_name, color=TARGET) + labs( x= X_column_name, y= y_column_name, title="Plot Subsets of Data Into Panels in the Same Plot", ) + geom_point() )在最后
情节九是一个漂亮且可读性强的可视化,可以通过简单的代码来实现。
我seaborn 分析仪我喜欢用这个集合。
原创声明:本文系作者授权爱码网发表,未经许可,不得转载;
原文地址:https://www.likecs.com/show-308629451.html