numpy模块、matplotlib模块、pandas模块
1. numpy模块
numpy模块的作用
用来做数据分析,对numpy数组(既有行又有列)——矩阵进行科学计算
实例
lt1 = [1, 2, 3] # n个元素
lt2 = [4, 5, 6] lt = []
for i in range(len(lt1)): # O(n)
lt.append(lt1[i] * lt2[i]) print(lt) import numpy as np # 约定俗成的 arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 * arr2) # gpu --> 图形显卡 # 创建numpy数组 --> 可变 # 一维数组(不在讨论范围内)
arr = np.array([1, 2, 4])
print(type(arr), arr) # 二维数组(******)
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr) # 三维数组(不在讨论范围内)--》tensorflow
arr3 = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]],
])
print(arr) # numpy数组的属性 arr = np.array([
[1, 2, 3],
[4, 5, 6]
]) # T 数组的转置(对高维数组而言) --> 行列互换,转置
print(arr, '\n', arr.T) # dtype 数组元素的数据类型,numpy数组是属于python解释器的;int32/float64属于numpy的
print(arr.dtype)
'''
# 定制化的科学计算机
11111111111111111111111111111111111111111
'''
# size 数组元素的个数
print(arr.size)
# ndim 数组的维数
print(arr.ndim)
print(arr3.ndim)
# shape 数组的维度大小(以元组形式)
print(arr.shape[0])
print(arr.shape[1])
# astype 类型转换
arr = arr.astype(np.float64)
print(arr) # 切片numpy数组
lt = [1, 2, 3] print(lt[:]) arr = np.array([
[1, 2, 3],
[4, 5, 6]
]) print(arr[:, :]) # 行,列 print(arr[0, 0]) print(arr[0, :]) print(arr[:, -2:]) # 逻辑取值
print(arr[arr > 4]) # 赋值
lt = [1, 2, 3] lt[:] = [0, 0, 0]
print(lt) arr = np.array([
[1, 2, 3],
[4, 5, 6]
]) arr[0, 0] = 0
print(arr) arr[0, :] = 0
print(arr) arr[:, :] = 0
print(arr) # 数组的合并 arr1 = np.array([
[1, 2, 3],
[4, 5, 6]
]) arr2 = np.array([
[7, 8, 9],
['a', 'b', 'c']
]) print(np.hstack((arr1, arr2))) # 只能放元组 print(np.vstack((arr1, arr2))) print(np.concatenate((arr1, arr2), axis=1)) # 默认以列合并 # 0表示列,1表示行 # 通过函数创建numpy数组 print(np.ones((2, 3))) print(np.zeros((2, 3))) print(np.eye(3, 3)) print(np.linspace(1, 100, 10)) print(np.arange(2, 10)) arr1 = np.zeros((1, 12))
print(arr1.reshape((3, 4))) # 重构形状 # numpy数组运算 # +-*'
arr1 = np.ones((3, 4)) * 4
print(arr1) # numpy数组运算函数 print(np.sin(arr1)) # 矩阵运算--点乘 arr1 = np.array([
[1, 2, 3],
[4, 5, 6]
]) arr2 = np.array([
[1, 2],
[4, 5],
[6, 7]
])
# 2* 3 3*2
print(np.dot(arr1, arr2)) # 求逆
arr = np.array([[1, 2, 3], [4, 5, 6], [9, 8, 9]])
print(np.linalg.inv(arr)) # numpy数组数学和统计方法
print(np.sum(arr[0, :])) # numpy.random生成随机数(******)
print(np.random.rand(3, 4)) print(np.random.random((3, 4))) # np.random.seed(1)
print(np.random.random((3, 4))) s = np.random.RandomState(1)
print(s.random((3, 4))) arr = np.array([[1, 2, 3], [4, 5, 6], [9, 8, 9]])
np.random.shuffle(arr)
print(arr) # 针对一维
print(np.random.choice([1, 2, 3], 1)) # 针对某一个范围
print(np.random.randint(1, 100, (3, 4)))
2. matplotlib模块
matplotlib模块的作用
画图(画各种与数据相关的图)
实例
# 条形图
# from matplotlib import pyplot as plt # 约定俗成
# from matplotlib.font_manager import FontProperties # 修改字体
#
# font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
#
# plt.style.use('ggplot') # 设置背景
#
# clas = ['3班', '4班', '5班', '6班']
# students = [50, 55, 45, 60]
# clas_index = range(len(clas))
#
# # [0,1,2,3] [50,55,45,60]
# plt.bar(clas_index,students,color='darkblue')
#
# plt.xlabel('学生',fontproperties=font)
# plt.ylabel('学生人数',fontproperties=font)
# plt.title('班级-学生人数',fontproperties=font,fontsize=20,fontweight=25)
# plt.xticks(clas_index,clas,fontproperties=font)
#
# plt.show() # # 直方图
# import numpy as np
# from matplotlib import pyplot as plt # 约定俗成
# from matplotlib.font_manager import FontProperties # 修改字体
#
# font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
#
# plt.style.use('ggplot')
#
# x1 = np.random.randn(10000)
#
# x2 = np.random.randn(10000)
#
# fig = plt.figure() # 生成一张画布
# ax1 = fig.add_subplot(1, 2, 1) # 1行2列取第一个
# ax2 = fig.add_subplot(1, 2, 2)
#
# ax1.hist(x1, bins=50,color='darkblue')
# ax2.hist(x2, bins=50,color='y')
#
# fig.suptitle('两个正太分布',fontproperties=font,fontsize=20)
# ax1.set_title('x1的正太分布',fontproperties=font) # 加子标题
# ax2.set_title('x2的正太分布',fontproperties=font)
# plt.show() # 折线图
#
# import numpy as np
# from matplotlib import pyplot as plt # 约定俗成
# from matplotlib.font_manager import FontProperties # 修改字体
#
# font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
#
# plt.style.use('ggplot')
#
# np.random.seed(10)
# x1 = np.random.randn(40).cumsum()
# x2 = np.random.randn(40).cumsum()
# x3 = np.random.randn(40).cumsum()
# x4 = np.random.randn(40).cumsum()
#
# plt.plot(x1, c='r', linestyle='-', marker='o', label='红圆线')
# plt.plot(x2, color='y', linestyle='--', marker='*', label='黄虚线')
# plt.plot(x3, color='b', linestyle='-.', marker='s', label='蓝方线')
# plt.plot(x4, color='black', linestyle=':', marker='s', label='黑方线')
# plt.legend(loc='best', prop=font) # 显示label
# plt.show() # 散点图+直线图
import numpy as np
from matplotlib import pyplot as plt # 约定俗成
from matplotlib.font_manager import FontProperties # 修改字体 font = FontProperties(fname='C:\Windows\Fonts\simsun.ttc') plt.style.use('ggplot') fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2) x = np.arange(20)
y = x ** 2 x2 = np.arange(20)
y2 = x2 ax1.scatter(x, y, c='r', label='红')
ax1.scatter(x2, y2, c='b', label='蓝') ax2.plot(x, y)
ax2.plot(x2, y2) fig.suptitle('两张图', fontproperties=font, fontsize=15)
ax1.set_title('散点图', fontproperties=font)
ax2.set_title('折线图', fontproperties=font)
ax1.legend(prop=font)
plt.show()
3. pandas模块
pandas模块的作用
操作各种文本文件(如 excel / json / sql / ini / csv 等)
实例
# import pandas as pd
#
# df = pd.read_csv('test.csv',header=None)
# df.to_excel('test.xls') # pd从excel中读取 DataFrame数据类型
import numpy as np
import pandas as pd np.random.seed(10) index = pd.date_range('2019-01-01', periods=6, freq='M')
print(index)
columns = ['c1', 'c2', 'c3', 'c4']
print(columns)
val = np.random.randn(6, 4)
print(val) df = pd.DataFrame(index=index, columns=columns, data=val)
print(df) # 保存文件,读出成文件
df.to_excel('date_c.xlsx') # 读出文件
df = pd.read_excel('date_c.xlsx', index_col=[0])
print(df) print(df.index)
print(df.columns)
print(df.values) print(df[['c1', 'c2']]) # 按照index取值
# print(df['2019-01-31'])
print(df.loc['2019-01-31'])
print(df.loc['2019-01-31':'2019-05-31']) # 按照values取值
print(df)
print(df.iloc[0, 0]) df.iloc[0, :] = 0
numpy模块、matplotlib模块、pandas模块的更多相关文章
- numpy,matplotlib,pandas
目录 numpy模块 numpy简介 numpy使用 matplotlib模块 条形图 直方图 折线图 散点图+直线图 pandas模块 numpy模块 numpy简介 numpy官方文档:https ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---3
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 0.pandas基于Numpy实现的,前者注重应用,后者注重结构 1.Series类型 ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---2
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 1.python自带的图像库PIL 1.1常用API Image.open() ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---1
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 1.ndarray对象的属性 .ndim..shape..size(元素个数,不是占用 ...
- numpy最后一部分及pandas初识
今日内容概要 numpy剩余的知识点 pandas模块 今日内容详细 二元函数 加 add 减 sub 乘 mul 除 div 平方 power 数学统计方法 sum 求和 cumsum 累计求和 m ...
- 模块讲解---numpymo模块,matplotlib模块,pandas模块
目录 numpy模块 matplotlib模块 pandas模块 numpy模块 numpy模块:用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学运算 在使用的时候,使用方法与其他的模 ...
- Python 数据处理扩展包: numpy 和 pandas 模块介绍
一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...
- numpy模块&pandas模块
目录 numpy模块 pandas模块 numpy模块 import pandas as pd import numpy as np df=pd.Series(np.array(['a','b'])) ...
- 使用Python的pandas模块、mplfinance模块、matplotlib模块绘制K线图
目录 pandas模块.mplfinance模块和matplotlib模块介绍 pandas模块 mplfinance模块和matplotlib模块 安装mplfinance模块.pandas模块和m ...
随机推荐
- 浏览器从输入URL到渲染出页面发生了什么
总体来说分为以下几个过程: 1. DNS解析 2. TCP连接 3. 发送HTTP请求 4. 服务器处理请求并返回HTTP报文 5. 浏览器解析渲染页面 6. 连接结束 参考资料:[https:// ...
- VIM常用操作手册
VIM常用操作手册 1.多行操作,多行注释,多行取消注释 https://jingyan.baidu.com/article/9c69d48f43ed6d13c8024e7b.html 2.常用操作 ...
- Unity3D中的SendMessage使用(消息传递的三种方法)
概述 Unity提供的消息推送机制可以非常方便我们的脚本开发,它实现的是一种伪监听者模式,利用的是反射机制. 常用函数 关于消息推送,常用的函数有三个:”SendMessage“.”SendMessa ...
- Java 8中处理集合的优雅姿势——Stream
在Java中,集合和数组是我们经常会用到的数据结构,需要经常对他们做增.删.改.查.聚合.统计.过滤等操作.相比之下,关系型数据库中也同样有这些操作,但是在Java 8之前,集合和数组的处理并不是很便 ...
- 【神经网络与深度学习】【VS开发】【CUDA开发】VS2013 配置CUDNN V4 DEMO
VS2013 配置CUDNN V4 DEMO 众所周知,当前主流深度学习的实现中调用的底层API都是cudnn,自己做项目需要开发深度学习模块时,也需要调用cudnn库,因此熟悉cudnn库是很有必要 ...
- 【VS开发】使用MFC创建并调用ActiveX控件
使用MFC创建并调用ActiveX控件 今天做了一下ActiveX的使用测试,总结一下: 首先使用MFC创建一个activeX的控件譬如ActiveXTest,编译成ocx并注册,然后另外编写一个测试 ...
- Django框架中使用Echart进行统计的SQL语句
最近想用Echart做数据统计的图形显示,数据来源是MySQL数据库,自然需要根据不同的搜索条件筛选出表中的数据,用比较多的就是时间的参数吧! 常用的mysql时间的条件进行检索的SQL语句: 数据表 ...
- Python新手练手项目
1.新手练手项目集中推荐 https://zhuanlan.zhihu.com/p/22164270 2.Python学习网站 https://www.shiyanlou.com 3.数据结构可视化学 ...
- [百度百科]PCI-E的速度
在早期开发中,PCIe最初被称为HSI(用于高速互连),并在最终确定其PCI-SIG名称PCI Express之前,将其名称更改为3GIO(第三代I / O). 名为阿拉帕霍工作组(AWG)的技术工作 ...
- Codeforces 1156F Card Bag(概率DP)
设dp[i][j]表示选到了第i张牌,牌号在j之前包括j的概率,cnt[i]表示有i张牌,inv[i]表示i在mod下的逆元,那我们可以考虑转移,dp[i][j]=dp[i-1][j-1]*cnt[j ...