模块讲解---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 ...
随机推荐
- sql中能使用charindex 不要用 in 。charindex比in快很多
写SQL语句我们经常需要判断一个字符串中是否包含另一个字符串,但是SQL SERVER中并没有像C#提供了Contains函数,不过SQL SERVER中提供了一个叫CHAEINDX的函数,顾名思义就 ...
- 解决maven项目无法读取src/main/java目录下面的配置文件问题
我们在用Mybatis去操作底层数据库的时候,需要用到xml配置文件,一般我们是把配置文件和dao放置在同一层目录. 但是在用idea操作maven项目的时候,我们可能会遇到无法读取到dao对应的ma ...
- jquery绑定input的change事件
### jquery绑定input的change事件 背景:在做一个登录页时,如果用户未输入验证码则无法点击登录按钮,所以想到了用input的change事件,但是在写完后发现无法监听input值的改 ...
- 汉字在unicode中的位置
在www.unicode.org中查找汉字.china找不到,后来查资料才明白,应该查CJK,为什么内? unicode这个组织吧中国日本韩国的字合并了 中日韩统一表意文字(CJK Unified ...
- C 循环统计输入的单词个数和字符长度
C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...
- 关于Python的导入覆盖解决办法
这种问题一般来说还是不会引起的,可能会出现在datetime和time这样类型的模块中. 例如: import datetime from datetime import datetime 如果写在一 ...
- 《CAP定理》
分布式系统的最大难点,就是各个节点的状态如何同步.CAP 定理是这方面的基本定理,也是理解分布式系统的起点. 分布式系统的三个指标 这三个指标不可能同时做到——这个结论就叫做 CAP 定理. Part ...
- 深度剖析Kubernetes API Server三部曲 - part 3
在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...
- SQL Server系统函数:类型转换函数
原文:SQL Server系统函数:类型转换函数 1.基本的转化 SELECT CAST(2008 as varchar(4)) + ' year!' SELECT CONVERT(varchar(4 ...
- cli create ssl certkey
cli create ssl certkey ############################### # 创建CA密钥 create ssl rsakey bwsrv-root.key -ex ...