做网站都需要学什么,世界500强企业名字,网址格式,做网站报价公司人工智能研究生前置知识—科学计算库numpy numpy是python中做科学计算的基础库#xff0c;对数组进行操作 整个numpy的操作和使用比较简单因此可以通过案例的学习掌握基本的用法在之后的学习中不断的进行熟悉和补充 创建数组#xff08;矩阵 #xff09;
创建的ndarray数组…人工智能研究生前置知识—科学计算库numpy numpy是python中做科学计算的基础库对数组进行操作 整个numpy的操作和使用比较简单因此可以通过案例的学习掌握基本的用法在之后的学习中不断的进行熟悉和补充 创建数组矩阵
创建的ndarray数组与python中的列表有格式上的相似之处。所有创建的数据类型都是ndarray类型
import numpy as npt1 np.array([1,2,3])
print(t1)
print(type(t1))运行结果 [1 2 3] class ‘numpy.ndarray’ 创建顺序数组的方式二
#创建方式二生成顺序数组
t2 np.array(range(6))
#等价形式
t3 np.arange(6)print(t2)
print(t3)运行结果 [0 1 2 3 4 5] [0 1 2 3 4 5] 创建方式三设置起始位置与步长的创建方式
t4 np.arange(4,10,2)
print(t4)
[4 6 8]补充查看当前数组中存放的数据类型可以使用.dtype的类型来进行获取和实现。对于数据类型可以参考numpy中常见的数据类型。
print(t4.dtype)
t5 np.array([aaa,12,5.2])
print(t5)
print(t5.dtype)运行结果 int32 [‘aaa’ ‘12’ ‘5.2’] U32 创建方式四上面了解了numpy中存储数据常见的数据类型由此可以引出第四种创建方式。在创建时引入指定的数据类型
t6 np.arange(4,dtypefloat)
print(t6)
print(t6.dtype)[0. 1. 2. 3.]
float64 补充调整数据类型的操作 #调整数据类型的操作
t7 t6.astype(int8)
print(t7)
[0 1 2 3]创建方式五通过列表生成式随机生成十个小数
#列表生成式
t8 np.array([random.random() for i in range(10)])
print(t8)
print(t8.dtype)[0.29322004 0.66179523 0.49565288 0.91203122 0.36098369 0.390472660.67372706 0.28380916 0.08134317 0.96597784]
float64
补充numpy中的去指定位数的小数的操作步骤固定小数的位数
t9 np.round(t8,2)
print(t9)
[0.95 0.03 0.31 0.06 0.18 0.19 0.47 0.71 0.28 0.11]数组的形状
查看数组的形状可以使用shape来调用而修改数组的形状可以通过reshape来进行调用 创建一个二维数组每个数组中含有6个元素的数据 import numpy as npa np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])
print(a)
print(a.shape)# 修改数组的形状并进行输出
print(a.reshape(3,4))
print(a.shape)
print(a)[[3 4 5 6 7 8] [4 5 6 7 8 9]] (2, 6) [[3 4 5 6] [7 8 4 5] [6 7 8 9]] (2, 6) [[3 4 5 6 7 8] [4 5 6 7 8 9]] 问题思考为什么a还是2行6列的数组格式呢原因reshape函数的操作是原地操作只对数组本身进行修改而没有返回值存在 对于shape的输出是一个元祖二维数据中含有两个数值三维数组中含有三个数值来进行表示以此类推。通常将高维数组降为低维数组进行计算 t5 np.arange(24).reshape(2,3,4)
print(t5)[[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]][[12 13 14 15][16 17 18 19][20 21 22 23]]]# 降维操作
t6 t5.reshape(4,6)
print(t6)t7 t5.reshape(24,)
print(t7)[[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]将高维数组降为低维数组可以使用numpy中内置的方法flatten将其展平为一维数组
# 一维数组展平
t8 t7.flatten()
print(t8)数组的计算
数组和数值进行计算
计算规则数组中的每一个元素都加上该数值来进行计算应用到数组中的每一个数字也称为广播机制同时可以应用减法乘法除法等相关运算
print(t6)
print(t62)[[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]]
[[ 2 3 4 5 6 7][ 8 9 10 11 12 13][14 15 16 17 18 19][20 21 22 23 24 25]]补充特殊的运算方式除0进行运算 [[nan inf inf inf inf inf] [inf inf inf inf inf inf] [inf inf inf inf inf inf] [inf inf inf inf inf inf]] inf无穷 nan:不是数值 同维数数组进行计算
计算规则满足矩阵之间的运算规则相同对应位置的元素之间进行相加的运算操作同理加法乘法除法相同。
b1 np.arange(100,124).reshape(4,6)
print(t6)
print(b1)
print(b1t6)[[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]]
[[100 101 102 103 104 105][106 107 108 109 110 111][112 113 114 115 116 117][118 119 120 121 122 123]]
[[100 102 104 106 108 110][112 114 116 118 120 122][124 126 128 130 132 134][136 138 140 142 144 146]]不同维数的数组之间进行计算 使用二维数组与一维数组进行计算采用的计算规则是高维数组中的每个低维元素与低维数组进行运算举例说明 print(t6)
b2 np.arange(0,6)
print(b2)
print(t6-b2)
# 类似于取反的操作
print(b2-t6)[[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]]
[0 1 2 3 4 5]
[[ 0 0 0 0 0 0][ 6 6 6 6 6 6][12 12 12 12 12 12][18 18 18 18 18 18]]
[[ 0 0 0 0 0 0][ -6 -6 -6 -6 -6 -6][-12 -12 -12 -12 -12 -12][-18 -18 -18 -18 -18 -18]]在判断满足计算条件时至少需要有一个维度满足相同的条件否则会报错在之后的练习使用中可以尝试3维数组与2维数组的计算等
numpy读取本地数据
轴的概念
在numpy中轴可以理解为方向使用数字0,1,2来进行表示对于一维数组只有一个0轴对于2维数组shape(2,2)有0轴和1轴同理对于三维数组shape(2,2,3)有0轴1轴2轴 有了轴的概念之后计算一个二维数组的平均值必须指定在那一个方向 读取csv数据简单了解
numpy索引
去numpy中的指定位置的元素1维2维
一维数组的索引和切片操作
#numpy的索引与切片操作
import numpy as np
a np.arange(3,15)
print(a)
print(a[4])
print(a[:])[ 3 4 5 6 7 8 9 10 11 12 13 14]
7
[ 3 4 5 6 7 8 9 10 11 12 13 14]二维数组的索引和切片操作
b np.arange(1,13).reshape(3,4)
print(b)
# 情况一选中一行元素
print(b[1])
# 等价形式
print(b[1,:])
#情况二选中一个元素
print(b[1][2])
# 等价形式
print(b[1,2])
# 选中一列元素
print(b[:,1])[[ 1 2 3 4][ 5 6 7 8][ 9 10 11 12]]
[5 6 7 8]
[5 6 7 8]
7
7
[ 2 6 10]
[5 6 7 8]
数组的转置与迭代
# 矩阵或数组的转置
print(b.T)
# 遍历与迭代(按列进行迭代)
for col in b.T:print(col)
#按行进行迭代
for rol in b:print(rol)
结果
[[ 1 5 9][ 2 6 10][ 3 7 11][ 4 8 12]]
[1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[1 2 3 4]
[5 6 7 8]
[ 9 10 11 12]numpy的数组合并
一维数组的垂直合并vertical stack
# array数组的合并
import numpy as np
a np.array([1,1,1])
b np.array([2,2,2])
print(np.vstack((a,b)))
# 测试合并之后变为二维数组
print(np.vstack((a,b)).shape)[[1 1 1][2 2 2]]
(2, 3)要确保满足格式的条件下进行合并操作
一维数组的水平合并左右合并 horizontal stack
print(np.hstack((a,b)))
print(np.hstack((a,b)).shape)[1 1 1 2 2 2]
(6,)向量的转置操作
# 向量进行转置可以在转置之后进行合并
print()
c a[:,np.newaxis]
print(c)
print(c.shape)
d a[np.newaxis,:]
print(d)
print(d.shape)
[[1][1][1]]
(3, 1)
[[1 1 1]]
(1, 3)
(3,)numpy数组的分割 若进行横向分割axis 0 进行纵向分割是axis 1 等量分割
import numpy as np
a np.arange(12).reshape((3,4))
print(a)
print(np.split(a,3,axis0))
print(np.split(a,4,axis1))[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0],[4],[8]]), array([[1],[5],[9]]), array([[ 2],[ 6],[10]]), array([[ 3],[ 7],[11]])]
不等量分割
print(np.array_split(a,3,axis1))
print(np.array_split(a,6,axis0))不等量分割的结果 [array([[0, 1], [4, 5], [8, 9]]), array([[ 2], [ 6], [10]]), array([[ 3], [ 7], [11]])] [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]]), array([], shape(0, 4), dtypeint32), array([], shape(0, 4), dtypeint32), array([], shape(0, 4), dtypeint32)] 对应的简单等价形式
# 等价分割的形式
print()
print(np.vsplit(a,3))
print(np.hsplit(a,4))
print(np.hsplit(a,2))
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0],[4],[8]]), array([[1],[5],[9]]), array([[ 2],[ 6],[10]]), array([[ 3],[ 7],[11]])]
[array([[0, 1],[4, 5],[8, 9]]), array([[ 2, 3],[ 6, 7],[10, 11]])]
文章转载自: http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.jrkzk.cn.gov.cn.jrkzk.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.rbrd.cn.gov.cn.rbrd.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.ksjmt.cn.gov.cn.ksjmt.cn