numpy库的使用
‘
| T |
数组的转置(对高维数组而言) |
| dtpye |
数组元素的数据类型 |
| size |
数组元素的个数 |
| ndim |
数组的维数 |
| shape |
数组的维度大小(以元组形式) |
| astype |
类型转换 |
| shape |
返回由数组行数和列数组成的元组 |
| array[rol, col] |
按行和列切割,与列表相似 |
| hstack((arr1, arr2)) |
合并数组的行,数组应具有相同的行数 |
| vstack((arr1, arr2)) |
合并数组的列,数组应具有相同的列数 |
| concatenate(arrays, asis = None) |
合并数组,当axis=None时,返回一维数组;当axis=0时,相当于vstack;当axis=1时,相当于hstack |
#数组的切割
arr = np.array([[1,2,3],[3,4,5],[7,8,9,]])
#第一行,所有列
print(arr[:1, :])
#第一列所有行
print(arr[;, :1])
# 取大于5的元素,返回一个数组
print(arr[arr > 5])
# numpy数组按运算符取元素的原理,即通过arr > 5生成一个布尔numpy数组
print(arr > 5)
数组的合并
#合并数组,数组应具有相同的行数
np.hstack((arr1, arr2))
| array() |
将列表转换为数组,可选择显式指定dtype |
| arange() |
range的numpy版,支持浮点数 |
| linspace() |
类似arange(),第三个参数为数组长度 |
| zeros() |
根据指定形状和dtype创建全0数组 |
| ones() |
根据指定形状和dtype创建全1数组 |
| eye() |
创建单位矩阵 |
| empty() |
创建一个元素全随机的数组 |
| reshape() |
重塑形状 |
matlotlib
# 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()
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
print(df)