Python内置模块02

numpy

numpy:专门用来做数组(矩阵)的运算

import numpy as np

# numpy数组
arr = np.array([1, 2, 3])  # 一维的numpy数组
arr2 = np.array([[1, 2, 3], [4, 5, 6]])  # 二维的numpy数组(一般就是二维)
# 三维的不使用numpy模块,使用tensorflow/pytorch模块

# 属性
'''
T  数组的转置(对高维数组而言)
dtype  数组元素的数据类型
size   数组元素的个数
ndim   数组的维数
shape  数组的维度大小(以元组形式)
astype 类型转换
'''

# 切片
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print('arr2', arr2)  # 二维的numpy数组(一般就是二维)
print('arr2[:, :]', arr2[:, :])  # 第一个冒号表示行,第二个冒号表示列,顾头不顾尾
print('arr2[0:1, :]', arr2[0:1, :])
print('arr2[0:1, 0:1]', arr2[0:1, 0:1])
print('arr2[0, :]', arr2[0, :])
print('arr2[0, 0], type(arr2[0, 0])', arr2[0, 0], type(arr2[0, 0]))
print('arr2[0, [0, 2]]', arr2[0, [0, 2]])
print('arr2[0, 0] + 1', arr2[0, 0] + 1)

# 修改值
arr2 = np.array([[1, 2, 3], [4, 5, 6]])  # 可变数据类型
print(arr2)  # 二维的numpy数组(一般就是二维)
arr2[0, :] = 0
print(arr2)
arr2[1, 1] = 1
print(arr2)
arr2[arr2 < 3] = 3  # 布尔取值
print(arr2)

# 合并
arr1 = np.array([[1, 2, 3], [4, 5, 6]])  # 可变数据类型
print(arr1)
arr2 = np.array([[7, 8, 9], [10, 11, 12]])  # 可变数据类型
print(arr2)

print(np.hstack((arr1,arr2)))  # 行合并
print(np.vstack((arr1,arr2)))  # 列合并

print(np.concatenate((arr1, arr2)))  # 默认列合并
print(np.concatenate((arr1, arr2),axis=1))  # 1表示行;0表示列

# 通过函数创建numpy数组
arr = np.array([[1, 2, 3], [4, 5, 6]])  # 可变数据类型

print(np.zeros((5, 5)))  # 生成五行五列数值为0的二维数组
print(np.ones((5, 5)) * 100)  # 生成五行五列数值为100的二维数组

# 数组运算
# +-*/ // % **

# numpy随机数
print(np.random.rand(3, 4))
print(np.random.randint(1, 10, (3, 4)))  ## 最小值1,最大值10,3*4

pandas模块

# pandas基于Numpy,主要用来处理表格数据
import pandas as pd
import numpy as np

dates = pd.date_range('20190101', periods=6, freq='M')
values = np.random.randint(1, 10, (6, 4))
columns = ['c1', 'c2', 'c3', 'c4']

# 生成DataFrame数据结构
df =pd.DateFrame(values, index=dates, columns=columns)
# 属性
'''
T  数组的转置(对高维数组而言)
dtype  数组元素的数据类型
size   数组元素的个数
ndim   数组的维数
shape  数组的维度大小(以元组形式)
astype 类型转换
'''

# 排序
df = df.sort_index(axis=1)  # 0表示列,1表示行
df = df.sort_values('c3')

# 取值
df['c1']
df[['c1', 'c3']]
df.loc['2019-01-31': '2019-02-28']  # 按行取值
df.values[1, 1]  # 按值取值,第二行第二列
df[df['c1']>3]

# 替换值
df.iloc[1,1] = 1

# pandas操作表格
from io import StringIO

test_data = '''
5.1,,1.4,0.2
4.9,3.0,1.4,0.2
4.7,3.2,,0.2
7.0,3.2,4.7,1.4
6.4,3.2,4.5,1.5
6.9,3.1,4.9,
,,,
test_data = StringIO(test_data)  # 把test_data读入内存中相当于变成文件

df = pd.read_csc('test.csv', header=None)  # 读取文件
df.columns = ['c1', 'c2', 'c3', 'c4']
df.index = ['nick', 'jason', 'tank']

df.to_csv('test1.csv')

matplotlib模块

matplotlib是一个绘图库,它可以创建常用的统计图,包括条形图、箱型图、折线图、散点图、饼图和直方图。

import matplotlib.pyplot as plt  # 默认支持英文,不支持中文
from matplotlib.font_manager import FontProperties
font = Fontproperties(fname='D:\msyh.ttc')

# 条形图
classer = ['3班', '4班', '5班', '6班']
students = [50, 60, 55, 67]
ind = range(len(classes))

plt.bar(ind, students, color='r')
plt.xticks(ind, classer, fontproperties=font)
plt.show()

# 直方图
import numpy as np
mu1, mu2, sigma = 50, 100, 10
x1 = mu1 + sigma * np.random.randn(10000)
x2 = mu2 + sigma * np.random.randn(10000)

plt.hist(x1, bins=50)  # 每50个数据一根柱子
plt.show()

# 折线图
plt.plot(x1,marker='o',color='r',label='红线',linestyle='--')
plt.plot(x2,marker='*',color='y',label='黄线',linestyle='-.')
plt.plot(x3,marker='s',color='green',label='绿色',linestyle=':')
plt.plot(x4,marker='x',color='b',label='蓝色',linestyle='-')
x1 = np.random.randn(1,40).cumsum()
x2 = np.random.randn(1,40).cumsum()
x3 = np.random.randn(1,40).cumsum()
x4 = np.random.randn(1,40).cumsum()

plt.legend(prop=font)  # label的字体的需要在这里换
plt.show()
posted @ 2019-08-19 15:20  17vv  阅读(124)  评论(0编辑  收藏  举报