pandas scatter_matrix使用

2023-05-16

示例来自《Phython机器学习基础教程》
(Introduction to Machine Learning with Python)
[德] Andreas C.Müller [美] Sarah Guido 著 张亮(hysic)译

书上示例代码

import pandas as pd
import mglearn 

在这里插入图片描述

然而在copy下来在pycharm里运行时发现了各种错误

01

首先是在进行import mglearn时出现的future warning

\anaconda\lib\site-packages\sklearn\externals\six.py:31: FutureWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
  "(https://pypi.org/project/six/).", FutureWarning)
\anaconda\lib\site-packages\sklearn\externals\joblib\__init__.py:15: FutureWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.
  warnings.warn(msg, category=FutureWarning)

反正大意是说“版本将会有变动你这个代码可能在新版本python下搞不好啦”之类的,忽视就行。

02

grr = pd.plotting.scatter_matrix(iris_dataFrame, c=y_train, figsize=(15, 15), marker=‘o’,
hist_kwds={‘bins’: 20}, s=60, alpha=.8, cmap=mglearn.cm3)

貌似2019年以后,pandas中的pd.scatter_matrix()调用不可行,变成了pd.plotting.scatter_matrix()来调用

03

如何显示??
为啥我什么都搞好了就是没有图呢?

最后发现需要一个plt.show()
plt又是什么?

需要导入一个包import matplotlib as plt

完整代码

# 文件名 test.py
# 导入包
import pandas as pd
import matplotlib.pyplot as plt
import mglearn
# 随机分割数据集、分为训练集和测试集的函数
from sklearn.model_selection import train_test_split
# sklearn自带的数据集
from sklearn.datasets import load_iris

# 载入数据集
iris_dataset = load_iris()

# 随机分割数据集【因为数据集原本是按照target顺序排列的】
'''
Target:
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
'''

X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'],iris_dataset['target'], random_state=0
)

# 将numpy数组转换成pandas dataFrame类型
iris_dataFrame = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
display(iris_dataFrame)

# 此处可以打印查看一下,记得要 `from IPython.display import display`
# display(iris_dataFrame)

# 调用函数 scatter_matrix,绘制散点图矩阵
grr = pd.plotting.scatter_matrix(iris_dataFrame, c=y_train, figsize=(15, 15), marker='o',
                        hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
plt.show()


# KNN算法对未知分类的花分类
from sklearn.neighbors import KNeighborsClassifier
# 只考虑一位邻居 ——如果多位邻居,把参数n_neighors改掉就行
knn = KNeighborsClassifier(n_neighbors=1)

# 训练模型
knn.fit(X_train,y_train)

# 尝试预测新的种类

import numpy as np
X_new = np.array([[5, 2.9, 1, 0.2]])
print("X_new.shape: {}".format(X_new.shape))  
'''X_new.shape: (1, 4) '''	
# shape必须符合X_test
# 例如
# shape of data: (150, 4)
# 因此一个元组的shape为(1, 4)


# 尝试调用 knn 对象的 predict 方法来进行预测
prediction = knn.predict(X_new)
print("Prediction: \n{}".format(prediction))
print("Prediction target name :\n {}".format(iris_dataset['target_names'][prediction]))
'''
Prediction: 
[0]
Prediction target name :
 ['setosa']
 
 # 预测值为0 ,对应种类为setosa
 '''


# 评估模型
y_pre = knn.predict(X_test)
corr_rate = np.mean(y_pre == y_test)
print("Test set score : {:.2f}".format(corr_rate))

'''
Test set score : 0.97
'''
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pandas scatter_matrix使用 的相关文章

随机推荐

  • 漫话线性代数:线性变换的几何解释

    网购了一本书 xff0c 说的是线性代数的几何解释 一口气读完 xff0c 感觉这部书有些贪多了 xff0c 什么细节都要弄个几何解释 xff0c 不免让琐碎的细节把关键性的主题给遮掩了 所以萌生一个念头 xff0c 把线性代数的核心概念和
  • ChatGPT 逆天测试,结局出乎预料

    目录 一 数学解题能力二 编程能力三 日常生活咨询四 问一些离谱的问题 xff0c 它有啥反应 xff1f 五 逆天大测试 一 数学解题能力 据说 ChatGPT 会做数学题 xff0c 给他几个条件不充分的问题 xff0c 看看他是否真的
  • 我发现 chatGPT 在智能客服方面一个逆天的应用呀

    chatGPT 有助于快速构建知识库 xff0c 想了一个有趣的例子 xff0c 感觉 chatGPT真是强大呀 xff01 废话不多讲 xff0c 直接看效果吧 xff1a
  • 在 WIndows 下安装 Apache Tinkerpop (Gremlin)

    一 安装 JDK 首先安装 Java JDK xff0c 这个去官网下载即可 xff0c 我下载安装的 JDK19 xff08 jdk 19 windows x64 bin msi xff09 xff0c 细节不赘述 二 去 Tinkerp
  • 阅读笔记:TF - IDF 原理

    今天查阅 TF IDF 资料 xff0c 发现百度百科里面提供了一个例子 xff0c 解释的很清楚 xff0c 记下来备用 原文链接 xff1a https baike baidu com item tf idf 8816134 fr 61
  • 词向量语义匹配:欧氏距离和余弦相似度,选择哪一个?

    最近做自然语言处理算法 xff0c 需要根据词向量判断两个词汇的相似度 面临两个选择 xff1a 欧氏距离和余弦相似度 选择哪一个好呢 xff1f 一 概念图解 为便于理解这个问题 xff0c 假设词向量是二维的 我们分析一下这两种方法计算
  • 一分钟理解 AP(Affinity Propagation) 亲和⼒传播算法

    这篇博客发出来后 xff0c 我用 Rust 复现代码出现问题 为此 xff0c 我对对照了 sklearn 的相关代码 xff0c 反复比较了两天 xff0c 发现一处 bug xff0c 把 43 61 误写成了 61 xff0c 导致
  • 机器学习:准确率(Precision)、召回率(Recall)、F值(F-Measure)、ROC曲线、PR曲线

    增注 xff1a 虽然当时看这篇文章的时候感觉很不错 xff0c 但是还是写在前面 xff0c 想要了解关于机器学习度量的几个尺度 xff0c 建议大家直接看周志华老师的西瓜书的第2章 xff1a 模型评估与选择 xff0c 写的是真的很好
  • (5)细菌实验分组

    描述 有一种细菌分为A B两个亚种 xff0c 它们的外在特征几乎完全相同 xff0c 仅仅在繁殖能力上有显著差别 xff0c A亚种繁殖能力非常强 xff0c B亚种的繁殖能力很弱 在一次为时一个小时的细菌繁殖实验中 xff0c 实验员由
  • Python you-get 库 + FFmpeg 工具下载B站视频

    Python you get 库 43 FFmpeg 工具下载B站视频 电脑系统 xff1a Windows 10 准备阶段 xff08 安装you get和FFmpeg xff09 安装其实非常简单 xff0c 出现问题可以在网上参考其他
  • 虚拟机的创建、Linux相关基本命令等

    作业内容 xff1a 1 重新创建一个虚拟机 xff0c 熟悉下步骤 创建一个新的虚拟机 xff1a 1 点击 xff1a 创建新的虚拟机 2 下一步 3 提前下载一个镜像文件 xff0c 选择该文件即可 xff08 下载地址 xff1a
  • 解决mysql8.0主从配置,从库连接报错:Authentication plugin ‘caching_sha2_password‘ reported error

    其他配置都是按照网上分享的流程 xff0c 但是show slave status 查看从库状态时 xff0c Slave IO Running 61 connecting xff0c 这个状态是不对的 xff08 正常的Slave IO
  • windows10下visual studio 2019安装以及cuda11配置

    安装visual studio 2019 进入官方的下载页面 xff0c 可能需要登录 xff0c 登录后选取社区版下载 注意 xff0c 这里只是下载安装器 xff0c 真正的安装会在后续执行文件 xff0c 配置安装目录后 xff0c
  • Go语言基础语法_1_2021-10-28

    序言 xff1a 本身 xff0c 我是一个Java开发者 xff0c 但是最近在学习Java虚拟机 xff0c 但是看来看去总是不明白Java虚拟机是怎样实现的 xff0c 而有一本书是 自己动力手写Java虚拟机 xff0c 是用go语
  • debian(Linux)系统下安装jdk1.8

    第一步 xff1a 下载安装包 下载Linux环境下的jdk8 xff0c 请去 xff08 Java Downloads Oracle xff09 中下载jdk的安装文件 xff1b 由于我的Linux是64位的 xff0c 因此我下载j
  • Codeblocks自动代码格式化

    在代码框里点右键 xff0c 按Format use Astyle就会自动代码格式化了 但是它默认的风格是大括号另起一行 xff0c 很不习惯 xff0c 实际上是可以改的 1 Setting gt Editor gt Source For
  • 无法找到输出设备?

    希望我的方法能帮到你 我的电脑是联想 系统是win11 上次更新之后 扬声器就不能用了 右下角的喇叭是一个叉叉 一直没有放在心上 今天解决了一下 右键显示叉叉的喇叭 gt 希望我的方法 能帮到你
  • java程序设计-第一章

    第一章 教材知识点 概念1 2 什么是计算机1 3 编程语言1 4操作系统 Operating System1 5 操作系统1 6 1 7 1 8 1 10关键术语 教材quiz1 如何换行要点12 打印表格 要点2 域宽3 求半径5 5的
  • 将PHP项目部署到服务器

    CONTENT 流程1 租服务器2 配置环境3 测试4 WINScp上传文件 项目语言php xff0c 连接mysql 流程 租服务器 gt 配置环境 gt 测试php和数据库环境 gt 上传文件 gt 运行 1 租服务器 步骤 xff1
  • pandas scatter_matrix使用

    示例来自 Phython机器学习基础教程 Introduction to Machine Learning with Python 德 Andreas C M ller 美 Sarah Guido 著 张亮 xff08 hysic xff0