微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

虹膜数据集的散点图

如何解决虹膜数据集的散点图

我是数据科学的新手。我编写了这个脚本来绘制所有不同种类的虹膜数据集散点图。尽量不与自己密谋。如何优化我的代码

'''蟒蛇

from sklearn.datasets import load_iris

import numpy as np

import pandas as pd

iris=load_iris()

list1=[]

fig,ax =plt.subplots(nrows=3,ncols=2,figsize=(10,10))

for ii in range(4):
  for jj in range(1,4):
    if ii==jj:
      break
    if ii*jj not in list1[1::2]:
      list1.extend((ii+jj,ii*jj))
    elif ii+jj in list1[::2]:
      break
    a=ii
    b=jj
    x_index=ii
    y_index=jj
    colors=['blue','red','green']
    if ii==0:
      b=b-1
    elif jj==1:
      a=a-2
      b,a=a,b
    elif ii==3:
      a=a-1
      b=b-1
      a,b=b,a
    for label,color in zip(range(len(iris.target_names)),colors):
      ax[b,a].scatter(iris.data[iris.target==label,x_index],iris.data[iris.target==label,y_index],label=iris.target_names[label],color=color)

    ax[b,a].set_xlabel(iris.feature_names[x_index])
    ax[b,a].set_ylabel(iris.feature_names[y_index])
    ax[b,a].legend(loc="upper right")
    fig.tight_layout()
    fig.show()

''' enter image description here 这是输出

如果是你,你会怎么写?

感谢您的帮助。

解决方法

我会使用 pandas 的可视化或 seaborn 的可视化。

以下内容将在更少的空间内完成工作,但请记住,称其为高效的,您就犯了一个错误。因为在绘制数据集时,效率并不是一个重要的问题,尤其是在 Python 中(如果我错了,请纠正我)。

import seaborn as sns
import matplotlib.pyplot as plt
from pandas.plotting import parallel_coordinates
import pandas as pd
# Parallel Coordinates
# Load the data set
iris = sns.load_dataset("iris")
parallel_coordinates(iris,'species',color=('#556270','#4ECDC4','#C7F464'))
plt.show()

结果如下:

enter image description here

from pandas.plotting import andrews_curves
# Andrew Curves
a_c = andrews_curves(iris,'species')
a_c.plot()
plt.show()

其图如下所示: enter image description here

from seaborn import pairplot
# Pair Plot
pairplot(iris,hue='species')
plt.show()

将绘制以下图: enter image description here

还有另一个我认为最少使用和最重要的情节如下:

from plotly.express import scatter_3d
# Plotting in 3D by plotly.express that would show the plot with capability of zooming,# changing the orientation,and rotating
scatter_3d(iris,x='sepal_length',y='sepal_width',z='petal_length',size="petal_width",color="species",color_discrete_map={"Joly": "blue","Bergeron": "violet","Coderre": "pink"})\
            .show()

这个会绘制到您的浏览器中并需要 HTML5,您可以随心所欲地查看它。下一个图是那个。请记住,这是一个散点图,每个球的大小都显示了 petal_width 的数据,因此所有四个特征都在一个图中。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。