Python程序设计之Numpy

Numpy是Python语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。Numpy内部解除了Python的PIL(全局解释器锁),运算效率极好,是大量机器学习框架的基础库!

Numpy简单创建数组

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

if __name__ == '__main__':
    print(b)

输出结果:
在这里插入图片描述

Numpy查看数组属性

import numpy as np
# 创建简单的列表
a = [1,2,3,4]
# 将列表转换为数组
b = np.array(a)
#数组元素的个数
print(b.size)
#数组形状
print(b.shape)
#数组维度
print(b.ndim)
#数组的元素类型
print(b.dtype)

输出结果:

在这里插入图片描述

快速创建N维数组的api函数

import numpy as np
# 快速创建N维数组的api函数
array_one = np.ones([10, 10])

if __name__ == '__main__':
    print(array_one)

输出结果:
在这里插入图片描述

创建10行10列的数值为浮点0的矩阵:

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

if __name__ == '__main__':
    print(array_zero)

输出结果:
在这里插入图片描述

创建指定形状

import numpy as np
# Numpy创建随机数组np.random
# 均匀分布
#创建指定形状(示例为10行10列)的数组(范围在0至1之间)
print(np.random.rand(10, 10))

输出结果:
在这里插入图片描述

创建指定范围内的一个数

import numpy as np
# Numpy创建随机数组np.random
# 均匀分布
# 创建指定范围内的一个数
print(np.random.uniform(0, 100))

输出结果:
在这里插入图片描述

创建指定范围内的一个整数

import numpy as np
# Numpy创建随机数组np.random
# 均匀分布
# 创建指定范围内的一个整数
print(np.random.randint(0, 100))

输出结果:
在这里插入图片描述

给定均值/标准差/维度的正态分布

import numpy as np
# Numpy创建随机数组np.random
# 正态分布
# 给定均值/标准差/维度的正态分布
print(np.random.normal(1.75, 0.1, (2, 3)))

输出结果:
在这里插入图片描述

数组的索引,切片

import numpy as np
# Numpy创建随机数组np.random
# 正态分布
# 数组的索引, 切片
# 正态生成4行5列的二维数组
arr = np.random.normal(1.75,0.1,(4,5))
print(arr)
# 截取第1至2行的第2至3列(从第0行算起)
after_arr = arr[1:3,2:4]
print(after_arr)

输出结果:
在这里插入图片描述

改变数组形状

import numpy as np
# 改变数组形状(要求前后元素个数匹配)
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)

输出结果:

在这里插入图片描述

Numpy计算

条件判断:
import numpy as np
# Numpy计算
# 条件判断
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
print(stus_score > 80)

输出结果:
在这里插入图片描述

三目运算:

import numpy as np
# Numpy计算
# 三目运算
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
# 如果数值小于80,替换为0,如果大于80,替换为90
print(np.where(stus_score<80,0,90))

输出结果:
在这里插入图片描述

求最大值

import numpy as np
# Numpy计算
# 统计运算 —— 求最大值
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
# 指定轴最大值amax(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result)

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

输出结果:

在这里插入图片描述

求最小值:

import numpy as np
# Numpy计算
# 统计运算 —— 求最小值
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
# 指定轴最小值amin(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result)

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

输出结果:
在这里插入图片描述

指定轴平均值mean

import numpy as np
# Numpy计算
# 统计运算 —— 求平均值
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
# 指定轴平均值mean(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)
print("每一列的平均值为:")
result = np.mean(stus_score, axis=0)
print(result)

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

输出结果:

在这里插入图片描述

方差std

import numpy as np
# Numpy计算
# 统计运算 —— 求方差
stus_score = np.array([[80,88],[82,81],[84,75],[86,83],[75,81]])
# 指定方差std(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)
print("每一列的方差为:")
result = np.std(stus_score, axis=0)
print(result)

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

输出结果:

在这里插入图片描述

数组与数的运算

import numpy as np
# Numpy计算
# 统计运算 —— 数组与数的运算(加法)
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
# Numpy计算
# 统计运算 —— 数组与数的运算(乘法)
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]*0.5
print("减半后:")
print(stus_score)

输出结果:
在这里插入图片描述

数组间运算:

import numpy as np
# Numpy计算
# 统计运算 —— 数组间运算
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
# Numpy计算
# 矩阵运算:总成绩
# 计算规则
# (M行, N列) * (N行, Z列) = (M行, Z列)
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)

输出结果:
在这里插入图片描述

矩阵拼接
矩阵垂直拼接

import numpy as np
# Numpy计算
# 矩阵拼接
# 矩阵垂直拼接
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)

输出结果:

在这里插入图片描述

矩阵水平拼接

import numpy as np
# Numpy计算
# 矩阵拼接
# 矩阵水平拼接
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)

输出结果:

在这里插入图片描述
Numpy读取数据np.genfromtxt

在这里插入图片描述

import numpy as np
# Numpy读取数据np.genfromtxt
# genfromtxt 设置csv文件读取路径,设置分隔符
result = np.genfromtxt("./students_score.csv",delimiter=",")
print(result)

输出结果:

在这里插入图片描述

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只爭朝夕不負韶華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值