可视化库-Matplotlib-Pandas与sklearn结合(第四天)
1. 计算每一种的比例的百分比
import pandas as pd
from matplotlib.ticker import FuncFormatter np.random.seed(0)
df = pd.DataFrame({'Condition 1':np.random.rand(20),
'Condition 2':np.random.rand(20)*0.9,
'Condtion 3':np.random.rand(20)*1.1}) print(df.head()) fig, ax = plt.subplots()
# stacked 进行堆叠操作
df.plot.bar(ax=ax, stacked=True)
plt.show() # 设置百分比
df_ratio = df.div(df.sum(axis=1), axis=0)
fig, ax = plt.subplots()
df_ratio.plot.bar(ax=ax, stacked=True)
ax.yaxis.set_major_formatter(FuncFormatter(lambda y,_:'{:.0%}'.format(y)))
plt.show()


2. 通过pd将数据导入,进行缺失值补充,画出特征的PCA图
# 1 下载数据
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00383/risk_factors_cervical_cancer.csv'
df = pd.read_csv(url, na_values='?')
print(df.head())
# 2.对缺失值进行补充
from sklearn.preprocessing import Imputer
# 引入inputer() 使用均值对缺失值进行填充
impute = pd.DataFrame(Imputer().fit_transform(df))
print(impute.head())
impute.columns = df.columns
impute.index = df.index import seaborn as sns
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D # 3.取出样品特征, 取出Dx:Cancer
features = impute.drop('Dx:Cancer', axis=1)
y = impute['Dx:Cancer']
# 4进行PCA操作
pca = PCA(n_components=3)
X_r = pca.fit_transform(features)
# '{:.2%}'表示保留两位小数, pca.explained_variabce_ratio表示所占的比例
print('Explained variance:\nPC1{:.2%}\nPC2{:.2%}\nPC3{:.2%}'
.format(pca.explained_variance_ratio_[0],
pca.explained_variance_ratio_[1],
pca.explained_variance_ratio_[2],))
# 构造三维坐标系
fig = plt.figure()
ax = Axes3D(fig)
# 画散点图
ax.scatter(X_r[:, 0], X_r[:, 1], X_r[:, 2], c='r', cmap=plt.cm.coolwarm)
# 对三个维度的坐标进行标注
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3') plt.show()

可视化库-Matplotlib-Pandas与sklearn结合(第四天)的更多相关文章
- Python数据可视化库-Matplotlib(一)
今天我们来学习一下python的数据可视化库,Matplotlib,是一个Python的2D绘图库 通过这个库,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率图,条形图,错误图,散点图等等 废 ...
- Python可视化库-Matplotlib使用总结
在做完数据分析后,有时候需要将分析结果一目了然地展示出来,此时便离不开Python可视化工具,Matplotlib是Python中的一个2D绘图工具,是另外一个绘图工具seaborn的基础包 先总结下 ...
- 数据分析处理库pandas及可视化库Matplotlib
一.读取文件 1)读取文件内容 import pandas info = pandas.read_csv('1.csv',encoding='gbk') # 获取文件信息 print(info) pr ...
- Python可视化库Matplotlib的使用
一.导入数据 import pandas as pd unrate = pd.read_csv('unrate.csv') unrate['DATE'] = pd.to_datetime(unrate ...
- python的数据可视化库 matplotlib 和 pyecharts
Matplotlib大家都很熟悉 不谈. ---------------------------------------------------------------------------- ...
- python可视化库 Matplotlib 01 figure的详细用法
1.上一章绘制一幅最简单的图像,这一章介绍figure的详细用法,figure用于生成图像窗口的方法,并可以设置一些参数 2.先看此次生成的图像: 3.代码(代码中有详细的注释) # -*- enco ...
- python可视化库 Matplotlib 00 画制简单图像
1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...
- Python数据可视化库-Matplotlib(二)
我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...
- 可视化库-Matplotlib基础设置(第三天)
1.画一个基本的图 import numpy as np import matplotlib.pyplot as plt # 最基本的一个图,"r--" 线条加颜色, 也可以使用l ...
- Pycon 2017: Python可视化库大全
本文首发于微信公众号“Python数据之道” 前言 本文主要摘录自 pycon 2017大会的一个演讲,同时结合自己的一些理解. pycon 2017的相关演讲主题是“The Python Visua ...
随机推荐
- hdu1527威佐夫博弈
参考博客 https://hrbust-acm-team.gitbooks.io/acm-book/content/game_theory/wei_zuo_fu_bo_yi.html 满足 ,后手必胜 ...
- APP推广运营经验总结
这片文章来自于我在公司的分享会,主题是关于APP在渠道方面的推广,主要包括3个方面,下载量,留存率,日活跃用户. 首先,在应用市场中,一个APP有四个方面,简介,截图,下载量,评论.用户看这四个方面, ...
- css3动画的原理 及 各种效果制作
1. 制作小球弹动效果 在这篇文章中,我们将会去探究一下浏览器是如何去处理CSS Animations和CSS Transitions的, c 以便使你在写一些动画效果之前就可以对该动画在浏览器中 ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- CenOS7.4内核升级修复系统漏洞
先查看当前内核版本# uname -a 一. 导入key# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 二. 安装elrepo ...
- 10055 - Hashmat the Brave Warrior & 各数据类型所占字节数 (C语言)
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...
- sourcetree回退到历史节点
1. 原理 原理,我们都知道Git是基于Git树进行管理的,要想要回滚必须做到如下2点: 本地头节点与远端头节点一样(Git提交代码的前提条件):于本地头节点获取某次历史节点的更改.说的有点抽象,以图 ...
- nexus helm proxy 集成&&问题解决
对于使用kubernetes 进行开发的人员来说helm是很方便的 构建nexus helm plugin git clone https://github.com/sonatype-nexus- ...
- Spring boot的@Configuration
就在我惊艳于spring 4的AbstractAnnotationConfigDispatcherServletInitializer小巧简洁(如下)的时候却发现spring boot下面竟然无效. ...
- IntelliJ IDEA 基础设置
原文地址:IntelliJ IDEA 基础设置 博客地址:http://www.extlight.com 一.前言 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,Intel ...