模块讲解---numpymo模块,matplotlib模块,pandas模块
numpy模块
numpy模块:用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学运算
在使用的时候,使用方法与其他的模块有一点不一样
import numpy as np
具体的使用方法
1.创建numpy数组---》可变
# 一组数据相乘
import numpy as np
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
print(arr1*arr2)
# 输出为 [ 4 10 18]
2.数组的维数
# 一维数组(不在讨论范围内)
arr1 = np.array([1, 2, 4])
print(type(arr), arr1)
#输出结果为 :<class 'numpy.ndarray'> [1 2 4]
# 二维数组(******)
arr2 = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr2)
#输出结果为 :
[[1 2 3]
[4 5 6]]
# 三维数组(不在讨论范围内)--》tensorflow
arr3 = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]],
])
print(arr3)
#输出结果为 :
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
#T 数组的转置(对高维数组而言) --> 行列互换,转置
print(arr, '\n', arr.T)
# 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)
#仅作了解
# dtype 数组元素的数据类型,numpy数组是属于python解释器的;int32/float64属于numpy的
print(arr.dtype)
# 针对一维
print(np.random.choice([1, 2, 3], 1))
# 针对某一个范围
print(np.random.randint(1, 100, (3, 4)))ython
matplotlib模块
matplotlib模块:画图
1.条形图
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()
2.直方图
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()
3.折线图
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()
4.散点图+直线图
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()
pandas模块
功能介绍
pandas是python数据分析的核心模块。它主要提供了五大功能:
- 支持文件存取操作,支持数据库(sql)、html、json、pickle、csv(txt、excel)、sas、stata、hdf等。
- 支持增删改查、切片、高阶函数、分组聚合等单表操作,以及和dict、list的互相转换。
- 支持多表拼接合并操作。
- 支持简单的绘图操作。
- 支持简单的统计分析操作。
具体用法
# pd从excel中读取 DataFrame数据类型
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
print(df)
模块讲解---numpymo模块,matplotlib模块,pandas模块的更多相关文章
- 使用Python的pandas模块、mplfinance模块、matplotlib模块绘制K线图
目录 pandas模块.mplfinance模块和matplotlib模块介绍 pandas模块 mplfinance模块和matplotlib模块 安装mplfinance模块.pandas模块和m ...
- 开发技术--pandas模块
开发|pandas模块 整了一篇关于pandas模块的使用文章,方便检查自己的学习质量.自从使用了pandas之后,真的是被它的功能所震撼~~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...
- numpy模块、matplotlib模块、pandas模块
目录 1. numpy模块 2. matplotlib模块 3. pandas模块 1. numpy模块 numpy模块的作用 用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学计算 实 ...
- pandas、matplotlib、Numpy模块的简单学习
目录 一.pandas模块 二.matplotlib模块 1.条形图 2. 直方图 3.折线图 4.散点图+直线图 三.numpy 一.pandas模块 pandas是BSD许可的开源库,为Pytho ...
- Pandas模块
前言: 最近公司有数据分析的任务,如果使用Python做数据分析,那么对Pandas模块的学习是必不可少的: 本篇文章基于Pandas 0.20.0版本 话不多说社会你根哥!开干! pip insta ...
- 4 pandas模块,Series类
对gtx图像进行操作,使用numpy知识 如果让gtx这张图片在竖直方向上进行颠倒. 如果让gtx这张图片左右颠倒呢? 如果水平和竖直方向都要颠倒呢? 如果需要将gtx的颜色改变一下呢 ...
- pandas模块补充
数据分析模块pandas和matplotlib补充 面向百度式编程 面向百度式工作 遇到没有见过的知识点或者是相关知识点一定不要慌,结合百度和已知的知识点去学习 pandas模块补充 基于numpy构 ...
- python之pandas模块
一.pandas模块是基于Numpy模块的,pandas的主要数据结构是Series和DadaFrame,下面引入这样的约定: from pandas import Series,DataFrame ...
- Python 数据处理扩展包: numpy 和 pandas 模块介绍
一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...
随机推荐
- ARTS第九周打卡
Algorithm : 做一个 leetcode 的算法题 /* 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. ...
- wordpress 图片上传时发生了错误,请稍后再试 的解决办法
前一天网站还是好好的,仅一天过后就显示图片无法上传,百思不得其解 上下百度,大多数帖子提供的解决办法对我都不适用,继续搜,最后发现一篇帖子中提到是wp-config的编码格式问题 想到昨天刚好修改了下 ...
- Django打印出在数据库中执行的语句
有时我们需要看models操作时对应的SQL语句, 可以用如下方法查看--- 在django project中的settings文件尾部添加如下代码 LOGGING = { 'version': 1, ...
- 硬件实现IIC协议读取EEPROM
我TMD也是服了,反正我板子搞了半天也不成功我也不知道为什么,野火STM32-MINI,一直卡EV5,不管了 先代码沾上 工程目录(板子为野火STM32 MINI) 串口相关代码: bsp_usart ...
- go 实现每次生成不同随机值
直接使用rand.Intn(10) 多次运行发现每次的随机值都是一样的 查看 Intn方法的源码说明 // Intn returns, as an int, a non-negative pseudo ...
- 【计算几何】The Queen’s Super-circular Patio
The Queen’s Super-circular Patio 题目描述 The queen wishes to build a patio paved with of a circular cen ...
- 2019牛客多校四 E. triples II (容斥)
大意: 给定$n,a$, 求$n$个$3$的倍数, $or$和为$a$的方案数. 简单容斥题 可以求出$f_{x,y}$表示所有$3$的倍数中, 奇数位不超过$x$个$1$, 偶数位不超过$y$个$1 ...
- 判断pdf文件是否正常可用
之前在下载港交所的文件的时候会碰到有些文件异常的情况,文件下载下来,但是不能打开.查到其他的方法不是判断错误就是很麻烦. 整理出一个非常简单的方法,将文件以二进制方式打开,然后判断文件的开头是否符 ...
- (四)Activiti之流程定义部署之ZIP方式和流程定义查询
一.流程定义部署之ZIP方式 上一章节我们使用classpath的方式加载流程定义文件,这里我们使用ZIP的方式. 1.1 用activiti插件生成bpmn和png图片之后,压缩到一个zip格式的压 ...
- 搭建SSM环境(淘淘商城)
本文用到的资料: 链接:https://pan.baidu.com/s/1Pk_aI_PRbqRFP9i3o9Xodg 提取码:o4o4 1.1. 数据库 1.1.1. 使用navicat创建数据库连 ...