Numpy和数据库
数据库
导入pymysql全部包
连接数据库,填相关参数数据
引入cursor命令,填写相关sql语句
打印信息
提交事务(查询操作不需要事务)
关闭
from pymysql import *
conn = connect(“localhost”,“root”,“m123”,“food”,3306)
cur = conn.cursor()
count = cur.execute(“insert into userinfo(loginname,loginpass) values(‘wangwu’,999)”)
print(count)
conn.commit()
conn.close()
nimpy库–方便使用 数组和矩阵
Numpy基础
numpy的主要对象是同种元素的多维数组
维度(ndim)叫做轴(axes)
轴的个数叫秩(rank)
轴上的个数叫shape
类型 dtype
没什么说的就是练,行列定位依旧懵逼,有没有一起探讨的伙伴
'''创建矩阵-1'''
# import numpy as np
# array = np.array([1,2,3])
# # 打印的依次是: 轴 轴上的个数 一共几个值 矩阵类型
# print(array.ndim)
# print(array.shape)
# print(array.size)
# print(array.dtype)
#
# print("**************")
# array1 = np.array([[1,2,3],[4,5,6]])
# print(array1.ndim)
# print(array1.shape)
# print(array1.size)
# print(array1.dtype)
'''创建矩阵-2 arange(star,stop,stape,type)'''
# import numpy as np
# array3 = np.arange(1,10).reshape(3,3)
# print(array3)
# print(array3.ndim)
'''创建矩阵-3 通过随机数生成矩阵 最小值1,最大值9,取12个'''
# import numpy as np
# arrary4 = np.random.randint(1,10,12).reshape(2,3,2)
# print(arrary4)
'''创建矩阵-4 通过固定值生成矩阵'''
# arrary5 = np.ones(9).reshape(3,3)
# print(arrary5)
# arrary5 = np.zeros(9).reshape(3,3)
# print(arrary5)
'''常用函数'''
# # where(条件)
# import numpy as np
# arrary7 = np.random.randint(1,10,9).reshape(3,3)
# print(arrary7)
# # 过滤矩阵中小于5的数据,并用0替换小于5的数据
# print(np.where(arrary7>5,arrary7,0))
# # 以索引位取值
# print(arrary7[2][2])
'''切片'''
import numpy as np
# arrary8 = np.random.randint(1,10,16).reshape(4,4)
# print(arrary8)
# 取出第一行所有值
# print(arrary8[0])
# 取出第一列所有值
# print(arrary8[:,0])
# 取第一行和第三行
# print(arrary8[0::2,])
# 取第二列和第四列
# print(arrary8[::,1::2])
# 取第一行和第三行的取第二列和第四列
# print(arrary8[0::2,1::2])
'''遍历矩阵数据'''
# arrary9 = np.random.randint(1,10,20).reshape(4,5)
# # arrary10 = np.ones(20).reshape(4,5)
# arrary10 = np.ones(20).reshape(5,4)
# print(arrary9)
# print(arrary10)
# 遍历矩阵数据
# for i in range(0,arrary9.shape[0]):
# for j in range(0,arrary9.shape[1]):
# print(arrary9[i][j],end="\t")
# print()
'''基本运算'''
# # 累加矩阵所有值
# print(arrary9.sum())
# # 累加每列值
# print(arrary9.sum(0))
# # 累加每行值
# print(arrary9.sum(1))
# 矩阵加法(45)
# print(arrary9+arrary10)
# 矩阵减法(45)
# print(arrary9-arrary10)
# 矩阵乘法(54)
# print(np.dot(arrary9,arrary10))
# 矩阵转置
# print(arrary9.T)
# 矩阵的逆
# arrary11 = np.random.randint(1,10,9).reshape(3,3)
# print(np.linalg.inv(arrary11))
# 矩阵的数乘
# print(2 * arrary9)
'''形状操作'''
# arrary11 = np.random.randint(1,10,9).reshape(3,3)
# arrary12 = np.ones(9).reshape(3,3)
# print(arrary11)
# # ravel 扁平化处理
# print(arrary11.ravel())
# # vstack、row_stack 按行拼接矩阵,变长 列不动
# print(np.vstack((arrary11,arrary12)))
# # hstack、column_stack 按列拼接矩阵,变长 行不动
# print(np.hstack((arrary11,arrary12)))
# split 切 最后的0是行切,1是列切 注意切分的块不能超过轴数3
# print(np.split(arrary11,3,0))
# # vsplit 行切
# print(np.vsplit(arrary11,3))
# # hsplit 列切
# print(np.vsplit(arrary11,3))