numpy模块

numpy模块:用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学运算

在使用的时候,使用方法与其他的模块有一点不一样

  1. import numpy as np

具体的使用方法

1.创建numpy数组---》可变

  1. # 一组数据相乘
  2. import numpy as np
  3. arr1 = np.array([1,2,3])
  4. arr2 = np.array([4,5,6])
  5. print(arr1*arr2)
  6. # 输出为 [ 4 10 18]

2.数组的维数

  1. # 一维数组(不在讨论范围内)
  2. arr1 = np.array([1, 2, 4])
  3. print(type(arr), arr1)
  4. #输出结果为 :<class 'numpy.ndarray'> [1 2 4]
  5. # 二维数组(******)
  6. arr2 = np.array([
  7. [1, 2, 3],
  8. [4, 5, 6]
  9. ])
  10. print(arr2)
  11. #输出结果为 :
  12. [[1 2 3]
  13. [4 5 6]]
  14. # 三维数组(不在讨论范围内)--》tensorflow
  15. arr3 = np.array([
  16. [[1, 2, 3],
  17. [4, 5, 6]],
  18. [[1, 2, 3],
  19. [4, 5, 6]],
  20. ])
  21. print(arr3)
  22. #输出结果为 :
  23. [[[1 2 3]
  24. [4 5 6]]
  25. [[1 2 3]
  26. [4 5 6]]]
  1. #T 数组的转置(对高维数组而言) --> 行列互换,转置
  2. print(arr, '\n', arr.T)
  3. # size 数组元素的个数
  4. print(arr.size)
  5. # ndim 数组的维数
  6. print(arr.ndim)
  7. print(arr3.ndim)
  8. # shape 数组的维度大小(以元组形式)
  9. print(arr.shape[0])
  10. print(arr.shape[1])
  11. # astype 类型转换
  12. arr = arr.astype(np.float64)
  13. print(arr)
  14. # 切片numpy数组
  15. lt = [1, 2, 3]
  16. print(lt[:])
  17. arr = np.array([
  18. [1, 2, 3],
  19. [4, 5, 6]
  20. ])
  21. print(arr[:, :]) # 行,列
  22. print(arr[0, 0])
  23. print(arr[0, :])
  24. print(arr[:, -2:])
  25. # 逻辑取值
  26. print(arr[arr > 4])
  27. # 赋值
  28. lt = [1, 2, 3]
  29. lt[:] = [0, 0, 0]
  30. print(lt)
  31. arr = np.array([
  32. [1, 2, 3],
  33. [4, 5, 6]
  34. ])
  35. arr[0, 0] = 0
  36. print(arr)
  37. arr[0, :] = 0
  38. print(arr)
  39. arr[:, :] = 0
  40. print(arr)
  41. # 数组的合并
  42. arr1 = np.array([
  43. [1, 2, 3],
  44. [4, 5, 6]
  45. ])
  46. arr2 = np.array([
  47. [7, 8, 9],
  48. ['a', 'b', 'c']
  49. ])
  50. print(np.hstack((arr1, arr2))) # 只能放元组
  51. print(np.vstack((arr1, arr2)))
  52. print(np.concatenate((arr1, arr2), axis=1)) # 默认以列合并 # 0表示列,1表示行
  53. # 通过函数创建numpy数组
  54. print(np.ones((2, 3)))
  55. print(np.zeros((2, 3)))
  56. print(np.eye(3, 3))
  57. print(np.linspace(1, 100, 10))
  58. print(np.arange(2, 10))
  59. arr1 = np.zeros((1, 12))
  60. print(arr1.reshape((3, 4))) # 重构形状
  61. # numpy数组运算
  62. # +-*'
  63. arr1 = np.ones((3, 4)) * 4
  64. print(arr1)
  65. # numpy数组运算函数
  66. print(np.sin(arr1))
  67. # 矩阵运算--点乘
  68. arr1 = np.array([
  69. [1, 2, 3],
  70. [4, 5, 6]
  71. ])
  72. arr2 = np.array([
  73. [1, 2],
  74. [4, 5],
  75. [6, 7]
  76. ])
  77. # 2* 3 3*2
  78. print(np.dot(arr1, arr2))
  79. # 求逆
  80. arr = np.array([[1, 2, 3], [4, 5, 6], [9, 8, 9]])
  81. print(np.linalg.inv(arr))
  82. # numpy数组数学和统计方法
  83. print(np.sum(arr[0, :]))
  84. # numpy.random生成随机数(******)
  85. print(np.random.rand(3, 4))
  86. print(np.random.random((3, 4)))
  87. # np.random.seed(1)
  88. print(np.random.random((3, 4)))
  89. s = np.random.RandomState(1)
  90. print(s.random((3, 4)))
  91. arr = np.array([[1, 2, 3], [4, 5, 6], [9, 8, 9]])
  92. np.random.shuffle(arr)
  93. print(arr)
  1. #仅作了解
  2. # dtype 数组元素的数据类型,numpy数组是属于python解释器的;int32/float64属于numpy的
  3. print(arr.dtype)
  4. # 针对一维
  5. print(np.random.choice([1, 2, 3], 1))
  6. # 针对某一个范围
  7. print(np.random.randint(1, 100, (3, 4)))ython

matplotlib模块

matplotlib模块:画图

1.条形图

  1. from matplotlib import pyplot as plt # 约定俗成
  2. from matplotlib.font_manager import FontProperties # 修改字体
  3. font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
  4. plt.style.use('ggplot') # 设置背景
  5. clas = ['3班', '4班', '5班', '6班']
  6. students = [50, 55, 45, 60]
  7. clas_index = range(len(clas))
  8. # [0,1,2,3] [50,55,45,60]
  9. plt.bar(clas_index,students,color='darkblue')
  10. plt.xlabel('学生',fontproperties=font)
  11. plt.ylabel('学生人数',fontproperties=font)
  12. plt.title('班级-学生人数',fontproperties=font,fontsize=20,fontweight=25)
  13. plt.xticks(clas_index,clas,fontproperties=font)
  14. plt.show()

2.直方图

  1. import numpy as np
  2. from matplotlib import pyplot as plt # 约定俗成
  3. from matplotlib.font_manager import FontProperties # 修改字体
  4. font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
  5. plt.style.use('ggplot')
  6. x1 = np.random.randn(10000)
  7. x2 = np.random.randn(10000)
  8. fig = plt.figure() # 生成一张画布
  9. ax1 = fig.add_subplot(1, 2, 1) # 1行2列取第一个
  10. ax2 = fig.add_subplot(1, 2, 2)
  11. ax1.hist(x1, bins=50,color='darkblue')
  12. ax2.hist(x2, bins=50,color='y')
  13. fig.suptitle('两个正太分布',fontproperties=font,fontsize=20)
  14. ax1.set_title('x1的正太分布',fontproperties=font) # 加子标题
  15. ax2.set_title('x2的正太分布',fontproperties=font)
  16. plt.show()

3.折线图

  1. import numpy as np
  2. from matplotlib import pyplot as plt # 约定俗成
  3. from matplotlib.font_manager import FontProperties # 修改字体
  4. font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
  5. plt.style.use('ggplot')
  6. x1 = np.random.randn(10000)
  7. x2 = np.random.randn(10000)
  8. fig = plt.figure() # 生成一张画布
  9. ax1 = fig.add_subplot(1, 2, 1) # 1行2列取第一个
  10. ax2 = fig.add_subplot(1, 2, 2)
  11. ax1.hist(x1, bins=50,color='darkblue')
  12. ax2.hist(x2, bins=50,color='y')
  13. fig.suptitle('两个正太分布',fontproperties=font,fontsize=20)
  14. ax1.set_title('x1的正太分布',fontproperties=font) # 加子标题
  15. ax2.set_title('x2的正太分布',fontproperties=font)
  16. plt.show()

4.散点图+直线图

  1. font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
  2. plt.style.use('ggplot')
  3. fig = plt.figure()
  4. ax1 = fig.add_subplot(1, 2, 1)
  5. ax2 = fig.add_subplot(1, 2, 2)
  6. x = np.arange(20)
  7. y = x ** 2
  8. x2 = np.arange(20)
  9. y2 = x2
  10. ax1.scatter(x, y, c='r', label='红')
  11. ax1.scatter(x2, y2, c='b', label='蓝')
  12. ax2.plot(x, y)
  13. ax2.plot(x2, y2)
  14. fig.suptitle('两张图', fontproperties=font, fontsize=15)
  15. ax1.set_title('散点图', fontproperties=font)
  16. ax2.set_title('折线图', fontproperties=font)
  17. ax1.legend(prop=font)
  18. plt.show()

pandas模块

功能介绍

pandas是python数据分析的核心模块。它主要提供了五大功能:

  1. 支持文件存取操作,支持数据库(sql)、html、json、pickle、csv(txt、excel)、sas、stata、hdf等。
  2. 支持增删改查、切片、高阶函数、分组聚合等单表操作,以及和dict、list的互相转换。
  3. 支持多表拼接合并操作。
  4. 支持简单的绘图操作。
  5. 支持简单的统计分析操作。

具体用法

  1. # pd从excel中读取 DataFrame数据类型
  2. np.random.seed(10)
  3. index = pd.date_range('2019-01-01', periods=6, freq='M')
  4. print(index)
  5. columns = ['c1', 'c2', 'c3', 'c4']
  6. print(columns)
  7. val = np.random.randn(6, 4)
  8. print(val)
  9. df = pd.DataFrame(index=index, columns=columns, data=val)
  10. print(df)
  11. # 保存文件,读出成文件
  12. df.to_excel('date_c.xlsx')
  13. # 读出文件
  14. df = pd.read_excel('date_c.xlsx', index_col=[0])
  15. print(df)
  16. print(df.index)
  17. print(df.columns)
  18. print(df.values)
  19. print(df[['c1', 'c2']])
  20. # 按照index取值
  21. # print(df['2019-01-31'])
  22. print(df.loc['2019-01-31'])
  23. print(df.loc['2019-01-31':'2019-05-31'])
  24. # 按照values取值
  25. print(df)
  26. print(df.iloc[0, 0])
  27. df.iloc[0, :] = 0
  28. print(df)

模块讲解---numpymo模块,matplotlib模块,pandas模块的更多相关文章

  1. 使用Python的pandas模块、mplfinance模块、matplotlib模块绘制K线图

    目录 pandas模块.mplfinance模块和matplotlib模块介绍 pandas模块 mplfinance模块和matplotlib模块 安装mplfinance模块.pandas模块和m ...

  2. 开发技术--pandas模块

    开发|pandas模块 整了一篇关于pandas模块的使用文章,方便检查自己的学习质量.自从使用了pandas之后,真的是被它的功能所震撼~~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...

  3. numpy模块、matplotlib模块、pandas模块

    目录 1. numpy模块 2. matplotlib模块 3. pandas模块 1. numpy模块 numpy模块的作用 用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学计算 实 ...

  4. pandas、matplotlib、Numpy模块的简单学习

    目录 一.pandas模块 二.matplotlib模块 1.条形图 2. 直方图 3.折线图 4.散点图+直线图 三.numpy 一.pandas模块 pandas是BSD许可的开源库,为Pytho ...

  5. Pandas模块

    前言: 最近公司有数据分析的任务,如果使用Python做数据分析,那么对Pandas模块的学习是必不可少的: 本篇文章基于Pandas 0.20.0版本 话不多说社会你根哥!开干! pip insta ...

  6. 4 pandas模块,Series类

      对gtx图像进行操作,使用numpy知识 如果让gtx这张图片在竖直方向上进行颠倒.   如果让gtx这张图片左右颠倒呢?   如果水平和竖直方向都要颠倒呢?   如果需要将gtx的颜色改变一下呢 ...

  7. pandas模块补充

    数据分析模块pandas和matplotlib补充 面向百度式编程 面向百度式工作 遇到没有见过的知识点或者是相关知识点一定不要慌,结合百度和已知的知识点去学习 pandas模块补充 基于numpy构 ...

  8. python之pandas模块

    一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...

  9. Python 数据处理扩展包: numpy 和 pandas 模块介绍

    一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...

随机推荐

  1. sql中能使用charindex 不要用 in 。charindex比in快很多

    写SQL语句我们经常需要判断一个字符串中是否包含另一个字符串,但是SQL SERVER中并没有像C#提供了Contains函数,不过SQL SERVER中提供了一个叫CHAEINDX的函数,顾名思义就 ...

  2. 解决maven项目无法读取src/main/java目录下面的配置文件问题

    我们在用Mybatis去操作底层数据库的时候,需要用到xml配置文件,一般我们是把配置文件和dao放置在同一层目录. 但是在用idea操作maven项目的时候,我们可能会遇到无法读取到dao对应的ma ...

  3. jquery绑定input的change事件

    ### jquery绑定input的change事件 背景:在做一个登录页时,如果用户未输入验证码则无法点击登录按钮,所以想到了用input的change事件,但是在写完后发现无法监听input值的改 ...

  4. 汉字在unicode中的位置

    在www.unicode.org中查找汉字.china找不到,后来查资料才明白,应该查CJK,为什么内? unicode这个组织吧中国日本韩国的字合并了   中日韩统一表意文字(CJK Unified ...

  5. C 循环统计输入的单词个数和字符长度

    C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...

  6. 关于Python的导入覆盖解决办法

    这种问题一般来说还是不会引起的,可能会出现在datetime和time这样类型的模块中. 例如: import datetime from datetime import datetime 如果写在一 ...

  7. 《CAP定理》

    分布式系统的最大难点,就是各个节点的状态如何同步.CAP 定理是这方面的基本定理,也是理解分布式系统的起点. 分布式系统的三个指标 这三个指标不可能同时做到——这个结论就叫做 CAP 定理. Part ...

  8. 深度剖析Kubernetes API Server三部曲 - part 3

    在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...

  9. SQL Server系统函数:类型转换函数

    原文:SQL Server系统函数:类型转换函数 1.基本的转化 SELECT CAST(2008 as varchar(4)) + ' year!' SELECT CONVERT(varchar(4 ...

  10. cli create ssl certkey

    cli create ssl certkey ############################### # 创建CA密钥 create ssl rsakey bwsrv-root.key -ex ...