Numpy基础入门

Numpy基础入门

Numpy创建数组

In [1]: import numpy as np
#创建列表
In [2]: a = [1,2,3,4,5]
#将列表转换为数组
In [3]: b = np.array(a)

In [4]: b
Out[4]: array([1, 2, 3, 4, 5])

Numpy查看数组属性

数组元素个数

In [5]: b.size
Out[5]: 5

数组形状

In [7]: b.shape
Out[7]: (5,)

数组维度

In [8]: b.ndim
Out[8]: 1

数组元素类型

In [9]: b.dtype
Out[9]: dtype('int32')

快速创建N维数组

# 创建10行10列的数值为浮点1的矩阵
In [10]: array_one = np.ones([10,10])

In [11]: array_one
Out[11]:
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

# 创建10行10列的数值为浮点0的矩阵
In [12]: array_zero = np.zeros([10,10])

In [13]: array_zero
Out[13]:
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

Numpy的ndarray:多维数组对象

  • NumPy最重要的一个特点就是其N维数组对象(即ndarray),该对象是一个快速而灵活的大数据集容器。你可以利用这种数组对整块数据执行一些数学运算,其语法跟标量元素之间的运算一样。
  • 引入numpy,生成一个包含随机数的小数数组
In [8]: import numpy as np

#生成一个3行4列的随机小数数组
In [9]: data = np.random.randn(3,4)

In [10]: data
Out[10]:
array([[-0.15362061, -0.34089865, -0.19431841,  2.03184796],
       [ 1.80939146, -1.70192574, -0.43019449, -1.97796665],
       [ 1.12676546, -0.82870349, -0.24376202, -0.03822901]])
#对numpy数组进行数学运算
In [11]: data * 10
Out[11]:
array([[ -1.53620615,  -3.40898647,  -1.94318407,  20.31847962],
       [ 18.09391464, -17.01925738,  -4.30194488, -19.77966648],
       [ 11.26765458,  -8.28703488,  -2.43762024,  -0.38229007]])

In [12]: data + data
Out[12]:
array([[-0.30724123, -0.68179729, -0.38863681,  4.06369592],
       [ 3.61878293, -3.40385148, -0.86038898, -3.9559333 ],
       [ 2.25353092, -1.65740698, -0.48752405, -0.07645801]])
  • ndarray是一个通用的同构数据多维容器,也就是说,其中的所有元素必须是相同类型的。每个数组都有一个shape(一个表示各维度大小的元组)和一个dtype(一个用于说明数组数据类型的对象):
In [13]: data.shape
Out[13]: (3, 4)

In [14]: data.dtype
Out[14]: dtype('float64')

创建随机数组np.random

  • 均匀分布

    • np.random.rand(10,10)创建指定形状的数组(范围在0至1之间)
    • np.random.uniform(0,100)创建指定范围内的一个数
    • np.random.randint(0,100)创建指定范围内的一个整数
  • 正态分布

给定均值/标准差/维度的正态分布np.random.normal(1.75,0.1,(2,3))

  • 数组的索引、切片
#正态生成4行5列的二维数组
In [15]: arr = np.random.normal(1.75,0.1,(4,5))

In [16]: arr
Out[16]:
array([[1.84671763, 1.83650293, 1.69280265, 1.58788698, 1.7766252 ],
       [1.73262366, 1.72583429, 1.8503462 , 1.92228332, 1.52605653],
       [1.73487928, 1.8240943 , 1.83742538, 1.67167133, 1.80888167],
       [1.8066079 , 1.72450508, 1.66927645, 1.65576752, 1.856088  ]])
#截取第1至2行的第2至3列(从第0行起)
In [17]: after_arr = arr[1:3,2:4]

In [18]: after_arr
Out[18]:
array([[1.8503462 , 1.92228332],
       [1.83742538, 1.67167133]])
  • 改变数组的形状(要求前后元素个数匹配)
print("reshape函数的使用!")
one_20 = np.ones([20])
print("-->1行20列<--")
print (one_20)

one_4_5 = one_20.reshape([4, 5])
print("-->4行5列<--")
print (one_4_5)

reshape函数使用

Numpy计算

条件运算

在这里插入图片描述

In [24]: import numpy as np

In [25]: stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])

In [26]: stus_score > 80
Out[26]:
array([[False,  True],
       [ True,  True],
       [ True, False],
       [ True,  True],
       [False,  True]])

In [27]: np.where(stus_score < 80, 0, 90)
Out[27]:
array([[90, 90],
       [90, 90],
       [90,  0],
       [90, 90],
       [ 0, 90]])

统计运算

指定轴最大值amax(参数1:数组;参数2:axis=0/1;0表示列1表示行)

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一列的最大值(0表示列)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result)

print("每一行的最大值为:")
result = np.amax(stus_score, axis=1)
print(result)

在这里插入图片描述

指定轴最小值amin

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的最小值(0表示列)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result)

# 求每一行的最小值(1表示行)
print("每一行的最小值为:")
result = np.amin(stus_score, axis=1)
print(result)

在这里插入图片描述

指定轴平均值mean

# 指定轴平均值mean
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的平均值(0表示列)
print("每一列的平均值:")
result = np.mean(stus_score, axis=0)
print(result)

# 求每一行的平均值(1表示行)
print("每一行的平均值:")
result = np.mean(stus_score, axis=1)
print(result)

在这里插入图片描述

方差std

# 方差std
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的方差(0表示列)
print("每一列的方差:")
result = np.std(stus_score, axis=0)
print(result)

# 求每一行的方差(1表示行)
print("每一行的方差:")
result = np.std(stus_score, axis=1)
print(result)

在这里插入图片描述

数组运算

数组与数的运算

# 数组与数的运算
import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("加之前:")
print(stus_score)

#为所有平时成绩都加5分
stus_score[:,0]=stus_score[:,0]+5
print("加之后:")
print(stus_score)

在这里插入图片描述

# 数组也支持加减乘除运算
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a + b
d = a - b
e = a * b
f = a / b
print("a+b=",c)
print("a-b=",d)
print("a*b=",e)
print("a/b=",f)

在这里插入图片描述

矩阵运算np.dot()

在这里插入图片描述

# 计算规则(计算学生总成绩)
#(M行,N列)*(N行,Z列)=(M行,Z列)
import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
#平时成绩占40%,期末成绩占60%,计算结果
q = np.array([[0.4],[0.6]])
result = np.dot(stus_score,q)
print("最终结果为:")
print(result)

在这里插入图片描述

# 矩阵拼接
## 矩阵垂直拼接
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
      [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17], 
      [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.vstack((v1, v2))
print("v1和v2垂直拼接的结果为")
print(result)

在这里插入图片描述

# 矩阵水平拼接
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
      [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17], 
      [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.hstack((v1, v2))
print("v1和v2水平拼接的结果为")
print(result)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值