K近邻算法-数据集处理-属性描述-数据集可视化-数据集划分

一、概述:

1、获取数据集

  • 小数据集:sklearn.datasets.load_*
  • 大数据集:sklearn.datasets.fetch_*

2、数据集返回值介绍

  • 返回值类型是bunch-是一个字典类型
  • 返回值的属性:
    (1) data:特征数据数组
    (2) target:标签(目标)数组
    (3) DESCR:数据描述
    (4)feature_names:特征名
    (5)target_names:标签(目标值)名

3、数据集划分

  • from sklearn.model_selection import train_test_split
  • 参数:
    (1) x -- 特征值
    (2) y -- 目标值
    (3) test_size -- 测试值大小
    (4) random_state -- 随机数种子
  • 返回值:注意顺序
    (1) x_train,x_test,y_train,y_test

二、分述:

1、获取数据:

1.1 sklearn本地直接获取小数据集收集鸢尾花数据:

# coding:utf-8

from sklearn.datasets import load_iris

iris = load_iris()
print(iris)

  • 1.1 运行结果1+2部分合起来:
运行结果部分1.png

运行结果部分2.png

1.2 获取大数据集,例如新闻:

# coding:utf-8

from sklearn.datasets import load_iris, fetch_20newsgroups
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

# 获取大数据集,例如新闻
news = fetch_20newsgroups()
print(news)
  • 1.2 运行结果拿到JSON在线解析器查看更加方便:


    运行结果查看.png

2、sklearn数据集返回值介绍:

  • 2.1、load和fetch返回的数据类型datasets.base.Bunch(字典格式)

    data:特征数据数组,是[n_samples*n_features]的二维numpy.ndarray数组
    target:标签数组,是n_samples的一维numpy.ndarray数组
    DESCR:数据描述
    feature_names:特征名,新闻数据,手写数字、回归数据等
    target_names:标签名

  • 2.2、sklearn数据集返回值介绍代码示范🌰:

# coding:utf-8

from sklearn.datasets import load_iris, fetch_20newsgroups
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

# 1.数据集获取
# 1.1 本地直接获取小数据集,例如鸢尾花:
iris = load_iris()
print(iris)

# 1.2 大数据集获取,例如新闻
# news = fetch_20newsgroups()
# print(news)


# 2.数据集属性描述
print("数据集特征值是:\n", iris.data)
print("数据集目标值是:\n", iris["target"])
print("数据集的特征值名字是:\n", iris.feature_names)
print("数据集的目标值名字是:\n", iris.target_names)
print("数据集的描述:\n", iris.DESCR)

  • 2.3、sklearn数据集返回值介绍运行结果:

    sklearn数据集返回值介绍部分1.png
sklearn数据集返回值介绍部分2.png

3、数据可视化:

  • 3.1、代码示范🌰:
# coding:utf-8

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_iris, fetch_20newsgroups
import ssl
from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei']
ssl._create_default_https_context = ssl._create_unverified_context

# 1.数据集获取
# 1.1 本地直接获取小数据集,例如鸢尾花:
iris = load_iris()
# print(iris)

# 1.2 大数据集获取,例如新闻
# news = fetch_20newsgroups()
# print(news)


# 2.数据集属性描述
# print("数据集特征值是:\n", iris.data)
# print("数据集目标值是:\n", iris["target"])
# print("数据集的特征值名字是:\n", iris.feature_names)
# print("数据集的目标值名字是:\n", iris.target_names)
# print("数据集的描述:\n", iris.DESCR)

# 3.数据集的可视化
iris_d = pd.DataFrame(data=iris.data, columns=['Sepal_Length', 'Speal_Width', 'Petal_Length', 'Petal_Width'])
iris_d["target"] = iris.target
print(iris_d)


def iris_plot(data, col1, col2):
    sns.lmplot(x=col1, y=col2, data=data, hue="target", fit_reg=False)
    plt.title("鸢尾花数据显示")
    plt.show()


iris_plot(iris_d, 'Speal_Width', 'Petal_Length')
iris_plot(iris_d, 'Sepal_Length', 'Petal_Width')

  • 3.2、鸢尾花可视化效果图:
鸢尾花可视化效果图1.png
鸢尾花可视化效果图2.png

4、数据集的划分

  • 4.1、 机器学习一般的数据集会划分为两部分:
    (1) 训练集数据:用于训练,构建模型
    (2) 测试数据:在模型检验时使用,用于评估模型是否有效

  • 4.2、划分比例:
    (1)训练集:70%、80%、75%
    (2)测试集:30%、20%、25%

  • 4.3、数据及划分api:
    sklearn.model_selection.train_test_split(arrays,*options)

  • 参数:
    (1) x数据集的特征值
    (2) y数据集的特征值
    (3) test_size测试集的大小,一般为float
    (4)random_state随机数种子,不同的种子会造成不同的随机采样结果,相同的种子采样结果相同

  • return: x_train, x_test, y_train, y_test

  • 4.5、代码示范🌰:
# coding:utf-8

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.model_selection import train_test_split

import ssl
from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei']
ssl._create_default_https_context = ssl._create_unverified_context

# 1.数据集获取
# 1.1 本地直接获取小数据集,例如鸢尾花:
iris = load_iris()
# print(iris)

# 1.2 大数据集获取,例如新闻
# news = fetch_20newsgroups()
# print(news)


# 2.数据集属性描述
# print("数据集特征值是:\n", iris.data)
# print("数据集目标值是:\n", iris["target"])
# print("数据集的特征值名字是:\n", iris.feature_names)
# print("数据集的目标值名字是:\n", iris.target_names)
# print("数据集的描述:\n", iris.DESCR)

# 3.数据集的可视化
iris_d = pd.DataFrame(data=iris.data, columns=['Sepal_Length', 'Speal_Width', 'Petal_Length', 'Petal_Width'])
iris_d["target"] = iris.target
print(iris_d)


def iris_plot(data, col1, col2):
    sns.lmplot(x=col1, y=col2, data=data, hue="target", fit_reg=False)
    plt.title("鸢尾花数据显示")
    plt.show()


# iris_plot(iris_d, 'Speal_Width', 'Petal_Length')
# iris_plot(iris_d, 'Sepal_Length', 'Petal_Width')

# 4.数据集的划分
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=22)
print("训练集的特征值是:\n", x_train)
print("训练集的目标值是:\n", y_train)
print("测试集的特征值是:\n", x_test)
print("测试集的目标值是:\n", y_test)

print("训练集的目标值的形状是:\n", y_train.shape)
print("测试集的目标值的形状是:\n", y_test.shape)

# 不一样的random_state,结果不一样
x_train1, x_test1, y_train1, y_test1 = train_test_split(iris.data, iris.target, test_size=0.2, random_state=2)
x_train2, x_test2, y_train2, y_test2 = train_test_split(iris.data, iris.target, test_size=0.2, random_state=2)

print("测试集的目标值是:\n", y_test)
print("测试集的目标值1是:\n", y_test1)
print("测试集的目标值2是:\n", y_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 2 2]
训练集的目标值的形状是:
(120,)
测试集的目标值的形状是:
(30,)
测试集的目标值是:
[0 2 1 2 1 1 1 2 1 0 2 1 2 2 0 2 1 1 2 1 0 2 0 1 2 0 2 2 2 2]
测试集的目标值1是:
[0 0 2 0 0 2 0 2 2 0 0 0 0 0 1 1 0 1 2 1 1 1 2 1 1 0 0 2 0 2]
测试集的目标值2是:
[0 0 2 0 0 2 0 2 2 0 0 0 0 0 1 1 0 1 2 1 1 1 2 1 1 0 0 2 0 2]
Process finished with exit code 0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容