day 18

python day18 模块

numpy模块

numpy模块: 用来做数据分析,对numpy数组(既有行又有列)--矩阵进行科学运算

import numpy as np

arr1 = np.array([1,2,3])
arr1 = np.array([4,5,6])
#numpy数组 -->  可变数据类型
#二位数组
arr = np.array([
    [1,2,3],
    [4,5,6]
])

#T 数组的转置(对高维数组而言)  --->  行列互换,转置
print(arr,'\n',arr.T)

#dtype 数组元素的数据类型,numpy数组是属于python解释器的;int32/float64 属于numpy
print(aar.dtype)

#size 数组元素的个数
print(arr.size)

#ndim 数组的维数
print(arr.ndim)

# shape 数组的维度大小(以元祖形式)
print(arr.shape[0])
print(arr.shape[1])

#astype  类型转换
arr = arr.astype(np.float64)
print(arr)

#切片numpy数组
arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr[:,:])#行,列

print(arr[0,0])

#逻辑取值
print(arr[arr>4])
 
#赋值
arr = np.array([
    [1,2,3],
    [4,5,6]
])
arr[0,0] = 0
print(arr)

arr[0,:] = 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)))  #创建一个2行3列,,值全为1的数组

print(np.zeros((2, 3)))

print(np.eye(3, 3))  #创建一个3行3列对角线上的值为1的数组

print(np.linspace(1,100,10))## 构造一个等差数列,取头也取尾,从0取到100,取10个数

print(np.arange(2,10))#[2....9]

print(arr1.reshape((3,4)))#重构形状

#numpy数组运算函数
print(np.sin(arr1)

#矩阵运算 -- 点乘
arr1 = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

arr2 = np.array([
    [1, 2],
    [4, 5],
    [6, 7]
])
print(np.dot(arr1,arr2))

#求逆  
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])

# numpy数组数学和统计方法
print(np.sum(arr[0, :]))

# numpy.random生成随机数(******)
print(np.random.rand(3, 4))   #构造3*4的均匀分布的numpy数组
print(np.random.random((3, 4)))

print(np.random.randint(1, 100, (3, 4)))

matplotlib模块

matplotlib 模块: 画图

##条形图
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

# 修改背景为条纹
plt.style.use('ggplot')

classes = ['3班', '4班', '5班', '6班']

classes_index = range(len(classes))
print(list(classes_index))

student_amounts = [66, 55, 45, 70]

# 画布设置
fig = plt.figure()
# 1,1,1表示一张画布切割成1行1列共一张图的第1个;2,2,1表示一张画布切割成2行2列共4张图的第一个(左上角)
ax1 = fig.add_subplot(1, 1, 1)
ax1.bar(classes_index, student_amounts, align='center', color='darkblue')
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')

plt.xticks(classes_index,
           classes,
           rotation=0,
           fontsize=13,
           fontproperties=font)
plt.xlabel('班级', fontproperties=font, fontsize=15)
plt.ylabel('学生人数', fontproperties=font, fontsize=15)
plt.title('班级-学生人数', fontproperties=font, fontsize=20)
# 保存图片,bbox_inches='tight'去掉图形四周的空白
# plt.savefig('classes_students.png?x-oss-process=style/watermark', dpi=400, bbox_inches='tight')
plt.show()
##直方图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

# 修改背景为条纹
plt.style.use('ggplot')

mu1, mu2, sigma = 50, 100, 10
# 构造均值为50的符合正态分布的数据
x1 = mu1 + sigma * np.random.randn(10000)
print(x1)

fig = plt.figure()
ax1 = fig.add_subplot(121)
# bins=50表示每个变量的值分成50份,即会有50根柱子
ax1.hist(x1, bins=50, color='darkgreen')

ax2 = fig.add_subplot(122)
ax2.hist(x2, bins=50, color='orange')

fig.suptitle('两个正态分布', fontproperties=font, fontweight='bold', fontsize=15)
ax1.set_title('绿色的正态分布', fontproperties=font)
ax2.set_title('橙色的正态分布', fontproperties=font)
plt.show()
##折线图
import numpy as np
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

# 修改背景为条纹
plt.style.use('ggplot')

np.random.seed(1)

# 使用numpy的累加和,保证数据取值范围不会在(0,1)内波动
plot_data1 = randn(40).cumsum()
print(plot_data1)

plot_data2 = randn(40).cumsum()
plot_data3 = randn(40).cumsum()
plot_data4 = randn(40).cumsum()

plt.plot(plot_data1, marker='o', color='red', linestyle='-', label='红实线')
plt.plot(plot_data2, marker='x', color='orange', linestyle='--', label='橙虚线')
plt.plot(plot_data3, marker='*', color='yellow', linestyle='-.', label='黄点线')
plt.plot(plot_data4, marker='s', color='green', linestyle=':', label='绿点图')

# loc='best'给label自动选择最好的位置
plt.legend(loc='best', prop=font)
plt.show()

pandas模块

posted @ 2019-10-09 11:38  simple123  阅读(142)  评论(0编辑  收藏  举报