数据挖掘模块

2019-07-16  本文已影响0人  过气海豹

一、相关模块简介

1.numpy 可以高的处理数据、提供数组支持、很多模块的都依赖它,比如pandas、sipy、matplotlib都依赖它,所以这个模块是基础
2.pandas 主要用于数据探索和数据分析
3.matplotlib 作图,可视化
4.scipy 主要用于数值计算,同时支持矩阵运算,并提供了很多高等数据处理功能,比如积分、傅里叶变换、微分方程求解等
5.statsmodels 这个模块主要用于统计分析
6.Gensim 主要用于文本挖掘
7.sklearn、keras 前者机器学习,后者深度学习

二、相关模块的基本使用

1.numpy

import numpy

1.1创建数组格式:
x = numpy.array([元素1,元素二,...元素n])
注:这里的元素也可以是数组
1.2排序sort()
x.sort()升序排序,如果是多维数组则每个数组内升序排序
1.3取最大值max()和最小值min()
x1 = x.max()取该数组内最大值,如果是多维数组则取所有数组内最大的一个数
1.4切片
这里是python的基本语法,这里点出之时说明numpy数组也可以使用切片

2.pandas

import pandas as pd

2.1基本使用

a = pd.Series([1,6,5,9,2])

输出:

0    1
1    6
2    5
3    9
4    2
dtype: int64

2.2设置索引值

b = pd.Series([8,9,8,7],index=["one","two","three","four"])

输出:

one      8
two      9
three    8
four     7
dtype: int64

2.3DateFrame
DataFrame有点类似于表格

c = pd.DataFrame([[5,6,8,9],[1,5,4,8],[2,5,7,99]])

输出:

   0  1  2   3
0  5  6  8   9
1  1  5  4   8
2  2  5  7  99

设置DataFrame列名

d = pd.DataFrame([[5,6,8,9],[1,5,4,8],[2,5,7,99]],columns = ["one","two","three","four"])

输出:

   one  two  three  four
0    5    6      8     9
1    1    5      4     8
2    2    5      7    99

2.4用字典给DataFrame赋值

e = pd.DataFrame(
    {
        "one":4,
        "two":[6,4,3],
        "three":list(str(654)),
        })
   one  two three
0    4    6     6
1    4    4     5
2    4    3     4

2.5显示头部数据和尾部数据

d,head()#头部数据,默认前五行
#DataFrame.head(行数)
d.tail()#尾部数据,默认后五行

这两句命令最后执行得出的都是下面的结果,因为行数少于5行,就全部显示了。

   one  two  three  four
0    5    6      8     9
1    1    5      4     8
2    2    5      7    99

2.6详细信息

d.describe()#按列统计情况

输出:

            one       two     three       four
count  3.000000  3.000000  3.000000   3.000000          #数据个数
mean   2.666667  5.333333  6.333333  38.666667          #平均数
std    2.081666  0.577350  2.081666  52.252592          #标准差
min    1.000000  5.000000  4.000000   8.000000          #最小值
25%    1.500000  5.000000  5.500000   8.500000          #第一四分位数
50%    2.000000  5.000000  7.000000   9.000000          #中位数
75%    3.500000  5.500000  7.500000  54.000000          #第三四分位数
max    5.000000  6.000000  8.000000  99.000000          #最大值

注:
1)第一四分位数(Q1),又称“较小四分位数”,等于该样本中所有数值由小到大排列后第25%的数字;

2)第二四分位数(Q2),又称“中位数”,等于该样本中所有数值由小到大排列后第50%的数字;

3)第三四分位数(Q3),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。

2.7转置(a.T)

>>> d
   one  two  three  four
0    5    6      8     9
1    1    5      4     8
2    2    5      7    99
>>> d.T
       0  1   2
one    5  1   2
two    6  5   5
three  8  4   7
four   9  8  99
上一篇 下一篇

猜你喜欢

热点阅读