python打印星图

Diamonds are a data scientist’s best friend. More specifically, the diamond dataset found on Kaggle. In this article, I will walk through a simple workflow to create a Star Chart (aka Spider Chart or Radar Charts). This tutorial was adapted from the wonderful workflow of Alex at Python Charts. All of the code in this article and the needed dataset are available on GitHub.

钻石是数据科学家的最好朋友。 更具体地说, 在Kaggle上找到的菱形数据集。 在本文中,我将通过一个简单的工作流程来创建星图(又名蜘蛛图或雷达图)。 本教程改编自Python Charts的Alex出色的工作流程。 GitHub上提供了本文中的所有代码和所需的数据集。

钻石是数据科学家的最好朋友 (Diamonds are a Data Scientist’s Best Friend)

To begin you will need a few libraries. I am running Python 3. I created this workflow using a Jupyter notebook, pandas, matplotlib, and numpy. These packages can be installed via pip or conda if they are not already on your system.

首先,您将需要一些库。 我正在运行Python3。我使用Jupyter笔记本,pandas,matplotlib和numpy创建了此工作流程。 如果这些软件包尚未安装在系统上,则可以通过pip或conda进行安装。

pip install jupyterlabpip install pandaspip install matplotlib pip install numpy

The dataset can be downloaded from Kaggle and should be around 3.2 MB. I have included a copy of the dataset on the Github. I have the dataset in the data folder. Load the dataset with pandas, drop the extra index column, and we are off!

数据集可以从Kaggle下载,并且大约为3.2 MB。 我已经在Github上包含了数据集的副本。 我在数据文件夹中有数据集。 用大熊猫加载数据集,删除多余的索引列,我们关闭了!

df = pd.read_csv("data/diamonds.csv")df.drop("Unnamed: 0", axis=1, inplace=True)

3 C的电平 (Levels of the 3 C’s)

The 4 C’s of diamonds are Cut, Color, Clarity, and Carat. Cut, Color, and Clarity are defined as categorical variables used in the diamond industry. Carat is a numeric representing the weight of a stone.

钻石的4 C分别是切工,颜色,净度和克拉。 切工,颜色和净度被定义为钻石行业中使用的分类变量。 克拉是代表宝石重量的数字。

Photo by Hao Zhang on Unsplash
张皓在《 Unsplash》上的照片

To create the Star chart, we need to represent the diamond industry terms as numerics. To do this we need to gather information about the levels that are in our dataset. Cut is composed five levels with Ideal being the highest [4] and Fair being the lowest level [0]. In the seven levels of Color, D is the highest [6] and J is the lowest level [0]. Finally, Clarity is composed of eight levels with IF, meaning internally flawless as the highest level [7] and I1, inclusions level 1, as the lowest level [0].

要创建星图,我们需要将钻石行业术语表示为数字。 为此,我们需要收集有关数据集中级别的信息。 Cut分为五个级别,其中“ 理想 ”级别最高[4],而“ 公平 ”级别最低[0]。 在七个颜色级别中, D是最高级别[6], J是最低级别[0]。 最后,清晰度由IF的八个级别组成这意味着内部无瑕疵为最高级别[7],而I1为内含物,级别1为最低级别[0]。

显示的切割和抛光数据 (Cutting and Polishing Data for Display)

In our dataset, we cut 3 outliers in carat size that skew the downstream column scaling.

在我们的数据集中,我们切下了3个离群值,这些值偏离了下游列的缩放比例。

## Cut diamonds that skew carat rangeindicies_to_remove = [27415, 27630, 27130]df = df.drop(indicies_to_remove)

Next, we create new columns in our dataframe to house the rankings created by mapping a dictionary against our C’s column. An example of the mapping is below.

接下来,我们在数据框中创建新列,以容纳通过将字典映射到C列所创建的排名。 映射的示例如下。

cut={'Ideal':4,'Premium':3,'Very Good':2,'Good': 1,'Fair':0}df['Cut'] = df['cut'].map(cut) #Note: 'Cut' is a different column

Finally, we need to scale the columns that we will use in our Star Chart to represent the data fairly.

最后,我们需要缩放将在星图中使用的列以公平地表示数据。

## Convert all rankings and contiguous data to scale between 0-100factors = ['Cut', 'Color', "Clarity", "carat", "price"]new_max = 100new_min = 0new_range = new_max - new_min## Create Scaled Columnsfor factor in factors:    max_val = df[factor].max()    min_val = df[factor].min()    val_range = max_val - min_val    df[factor + '_Adj'] = df[factor].apply(lambda x: (((x - min_val) * new_range) / val_range) + new_min)

We then subset the scaled columns for downstream plotting. Notice how we are creating a new dataframe (df2) with only the columns we intend to use in the Star Chart.

然后,我们将缩放列的子集用于下游绘图。 请注意,我们如何仅使用打算在星形图表中使用的列来创建新的数据框(df2)。

## Subset scaled columns df2 = df[['Cut_Adj', "Color_Adj", "Clarity_Adj", "carat_Adj", "price_Adj"]]df2.columns = ['Cut', "Color", "Clarity", "Carat", "Price"]

表演之星 (The Star of the Show)

To create the Star Chart, we must specify which columns to use and create the circular plot object using numpy.

要创建星形图,我们必须指定要使用的列,并使用numpy创建圆形图对象。

labels = ['Cut', "Color", "Clarity", "Carat", "Price"]points = len(labels)angles = np.linspace(0, 2 * np.pi, points, endpoint=False).tolist()angles += angles[:1]

We then create a helper function to plot a diamond solely by the index number.

然后,我们创建一个辅助函数,以仅通过索引号绘制菱形。

def add_to_star(diamond, color, label=None):    values = df2.loc[diamond].tolist()    values += values[:1]    if label != None:        ax.plot(angles, values, color=color, linewidth=1, label=label)    else:        ax.plot(angles, values, color=color, linewidth=1, label=diamond)    ax.fill(angles, values, color=color, alpha=0.25)

Now the magic begins! We can begin populating our Star Chart with any diamonds we want. How about the most expensive and the two least expensive:

现在魔术开始了! 我们可以开始用我们想要的任何钻石填充星图。 最贵的和最便宜的两个分别如何:

## Create plot object   fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))## Plot a new diamond with the add_to_star functionadd_to_star(27749, '#1aaf6c', "Most Expensive Diamond")add_to_star(0, '#429bf4', "Least Expensive A")add_to_star(1, '#d42cea', "Least Expensive B")

This amount is enough to create a Star Chart, however, there are no x labels, no orientation, and no custom flair. Let’s change that!

此数量足以创建星图,但是,没有x标签,没有方向,也没有自定义样式。 让我们改变它!

## Fix axis to star from topax.set_theta_offset(np.pi / 2)ax.set_theta_direction(-1)## Edit x axis labelsfor label, angle in zip(ax.get_xticklabels(), angles):    if angle in (0, np.pi):        label.set_horizontalalignment('center')    elif 0 < angle < np.pi:        label.set_horizontalalignment('left')    else:        label.set_horizontalalignment('right')## Customize your graphic# Change the location of the gridlines or remove themax.set_rgrids([20, 40, 60 ,80])#ax.set_rgrids([]) # This removes grid lines# Change the color of the ticksax.tick_params(colors='#222222')# Make the y-axis labels larger, smaller, or remove by setting fontsizeax.tick_params(axis='y', labelsize=0)# Make the x-axis labels larger or smaller.ax.tick_params(axis='x', labelsize=13)# Change the color of the circular gridlines.ax.grid(color='#AAAAAA')# Change the color of the outer circleax.spines['polar'].set_color('#222222')# Change the circle background colorax.set_facecolor('#FAFAFA')# Add title and legendax.set_title('Comparing Diamonds Across Dimensions', y=1.08)ax.legend(loc='upper right', bbox_to_anchor=(1.3, 1.1))# Draw axis lines for each angle and label.ax.set_thetagrids(np.degrees(angles), labels)

So what is the output?

那么输出是什么?

A Star Chart of the most expensive and the 2 least expensive diamonds. Carets seem to be a large driver of price.
最贵和最便宜的2颗钻石的星图。 Carets似乎是价格的主要驱动力。

最好的金光闪闪 (Best Bling for Your Buck)

What is the diamond with the highest rating across the 4 C’s with the lowest price? To find out we must get the total value across the 4 C’s and divide by the raw (unscaled) price. This section operates over the original dataframe with all the raw columns. To find the total, we sum the four scaled columns.

在价格最低的4 C钻石中评级最高的钻石是什么? 为了找出答案,我们必须获得4 C的总价值,然后除以原始(未定标)价格。 本节将对具有所有原始列的原始数据框进行操作。 为了找到总数,我们对四个比例列进行求和。

df['Total'] = df['Cut_Adj'] + df['Color_Adj'] + df['Clarity_Adj'] + df['carat_Adj']## Divide Value total by Pricedf['4C_by_Price'] = df['Total']/df['price']df = df.sort_values(by="4C_by_Price", ascending=False)

The diamond with the most bling for our buck is #31597 and the diamond with the least bling for our buck is #26320. How do these diamonds compare on a Star Chart? Let’s explore below:

对我们的降压效果最高的钻石是#31597,而对我们的降压效果最少的钻石是#26320。 这些钻石与星图相比如何? 让我们探索以下内容:

One is diamond of a deal, the other is a chunk of carbon.
一个是交易的钻石,另一个是大块的碳。

结论: (Conclusions:)

Thank you for exploring a few diamond characteristics in a Star Chart format using matplotlib. If you have any questions post them below or to the location of the full code, the GitHub repository. My name is Cody Glickman and I can be found on LinkedIn. Be sure to check out some other articles about fun data science projects!

感谢您使用matplotlib探索星图格式的一些钻石特征。 如果您有任何疑问,请在下面或完整代码的位置发布GitHub存储库 。 我叫Cody Glickman ,可以在LinkedIn上找到 请务必查看其他有关有趣的数据科学项目的文章!

翻译自: https://towardsdatascience.com/stars-charts-in-python-9c20d02fb6c0

python打印星图


http://www.taodudu.cc/news/show-5289399.html

相关文章:

  • 【教程】如何使用星图地球数据云地图API调用服务
  • 星图达人数据采集接口
  • 巨量星图逆向分析
  • 星图识别
  • 360星图-Web日志分析 使用记录
  • 【申报通知】2022年江苏省双软评估申报工作
  • 两化融合贯标申报条件-制造
  • 证监会网站查询公募基金申报信息
  • 高新企业申报咨询工作网
  • 高新技术企业认定申报工作网,高新认定申报系统激活
  • 关于组织申报常州市2022年度高新技术产品的通知
  • 【项目申报】关于组织申报江苏省2022年度高新技术企业的通知
  • ITSS认证三级申报基本条件
  • 计算机网络管理员报名官网,申报计算机网络管理员
  • 科技创新申报要求
  • 租赁补贴,2022年武汉市工业厂房租赁费补贴申报奖励标准以及申报条件流程
  • 安徽省16市什么企业适合做知识产权贯标申报好处和什么材料汇编整理
  • 计算机应用与维修课程,计算机应用与维修专业(中技)教学计划
  • 计算机应用在我们生活中的哪些方面,计算机应用领域论文
  • 对计算机应用基础这门课的认识,对计算机应用基础课程教学的探讨
  • 爆笑搞笑图片,又短又精典的冷笑话
  • HTML期末大作业~ 大话西游之大圣娶亲电影4页面 ~学生网页设计作业源码(HTML+CSS+JS)...
  • 搞笑文章
  • 需要学习的,学习记录,需要做的事情
  • 突然想写写自己的伞。
  • 多校集训记
  • 现实版西游记:孙悟空英雄,唐僧才是赢家
  • 搞笑经典语录
  • 恶搞西游
  • 09年搞笑签名

python打印星图_Python中的星图相关推荐

  1. python queue 多进程_python中的Queue与多进程(multiprocessing)

    最近接触一个项目,要在多个虚拟机中运行任务,参考别人之前项目的代码,采用了多进程来处理,于是上网查了查python中的多进程 一.先说说Queue(队列对象) Queue是python中的标准库,可以 ...

  2. python print换行_Python中九九乘法表与古诗对话机器人及sep-end值

    # while实现对话机器人:你和机器人讲什么,机器人都输出一句诗赞美你 # 实现方案:while循环 + input输入 + random模块 import random a = ["清水 ...

  3. python模块实例化_python中zipfile模块实例化解析

    文章内容由--"脚本之家"--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压 ...

  4. python 打印文件名_Python | 打印文件内容以及文件名

    python 打印文件名 打印文件名 (Printing file name ) To print the filename, we use "file_object.name". ...

  5. python打印异常_python异常输出

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 开发准备cas 的 python sdk 包含了用于访问和操作 cas 的所有 ...

  6. python打印语句_Python 打印语句

    Python 打印语句 首先申明下,本文为笔者学习<Python学习手册>的笔记,并加入笔者自己的理解和归纳总结. 1.print语句用来打印,并在行的末尾添加一个换行.>>& ...

  7. python wraps模块_python中 wraps 的作用

    这里使用两段代码比较加入wraps装饰器后,函数打印的结果对比: 新建文件名:Testword 代码1:不加wraps装饰器 # coding=utf-8 from functools import ...

  8. python map用法_Python中ChainMap的一种实用用法

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简而言之ChainMap:将多个字典视为一个,解锁Python超能力. Python标准库中的集合模块包含 ...

  9. python基本统计量_Python中简单统计量的计算

    本篇文章给大家带来的内容是关于Python中简单统计量的计算,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.这些操作都要确保已经在电脑中安装好了Anaconda集成库,如果安装好 ...

最新文章

  1. electron调用python_在Electron app中运行python脚本
  2. 一手指天,一手指地,开!
  3. python的实验报告怎么写_学号:20191221,《python实验设计》实验报告三
  4. Zookeeper的api的简单使用(转载)
  5. 罗振宇2021跨年演讲2:除了规模中国还有什么优势?
  6. 安卓机用久了仍会卡?来看看两年前的小米6
  7. python类型转换astype-python中numpy数据类型转换的方法
  8. github 使用方法总结 还有一部分不太懂
  9. Python规范:用用assert
  10. 关于域名抢注:过期高PR域名抢注价值高吗?
  11. ios 扫描本地音乐_iOS如何获取本地的音乐歌曲mp3的信息数据
  12. ios和android下数字没有垂直居中,手机端设置小号字体的上下居中问题
  13. java double输出 lf_为什么double类型输入(scanf)用%lf,输出(printf)用%f?
  14. 在OCC7.6中,gp_vector和gp_dir和gp_axis有什么区别
  15. 庖丁解牛-图解MySQL 8.0优化器查询转换篇
  16. 括号配对检测python123_括_括是什么意思_括字怎么读_括的含义_括字组词-新东方在线字典...
  17. javax.net.ssl.SSLException 两种解决方法 链接https接口
  18. 【学习笔记--FMCW基础知识】
  19. 客制供应商申请审批单和供应商导入api
  20. 《中国制造2025》提出构建绿色制造体系,成为我国制造业新趋势

热门文章

  1. 网安笔记15 入侵检测IDS
  2. spring源码解读-第二部分面想切面编程
  3. 一种解决游戏无声音的方法
  4. IT项目管理中风险控制的4个重要步骤
  5. footer在html中作用,footer在html中是什么意思
  6. TCL X11G参数 TCL X11G Mini LED 评测怎么样
  7. java-php-python-ssm水星家纺网站计算机毕业设计
  8. 恒生金锐软件面试总结
  9. 如何为移动开发选择技术栈?
  10. Cortex-M软件结构