前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >001.python科学计算库numpy(上)

001.python科学计算库numpy(上)

作者头像
qubianzhong
发布2018-09-19 12:54:57
4630
发布2018-09-19 12:54:57
举报
文章被收录于专栏:行者常至行者常至

版权声明:本文为博主原创文章,允许转载,请标明出处。 https://cloud.tencent.com/developer/article/1342933

genfromtxt

  • 从文本文件加载数据,并按指定的方式处理缺失值。
代码语言:javascript
复制
import numpy

# genfromtxt 从文本文件加载数据,并按指定的方式处理缺失值。
# delimiter 用来分隔值的字符串。
#           默认情况下,任何连续的空格都充当分隔符。
#           一个整数或整数序列也可以作为每个字段的宽度提供
# dtype 结果数组的可选数据类型。默认情况下dtype=float。
# encoding 用于解码inputfile的编码。
#           当' fname '是文件对象时不适用。
#           特殊的值'bytes'支持向后兼容的变通方法,确保在可能的情况下接收字节数组,
#           并将latin1编码的字符串传递给转换器。重写此值以接收unicode数组并将字符串作为输入传递给转换器。
#           如果设置为None,则使用系统默认值。默认值是'bytes'。
world_alcohol = numpy.genfromtxt("001_numpy_1.txt", delimiter='——', dtype=str, encoding='utf8')
print(type(world_alcohol))
print(world_alcohol)

array

  • 创建一个数组。
代码语言:javascript
复制
import numpy

vector = numpy.array([5, 10, 15, 20])
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(vector)
print(matrix)

shape

  • 数组维数的元组
代码语言:javascript
复制
import numpy

vector = numpy.array([5, 10, 15, 20])
print(vector.shape)
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)

dtype

代码语言:javascript
复制
import numpy

# NumPy数组中的每个值都必须具有相同的数据类型
# NumPy在读取数据或将列表转换为数组时,将自动找出适当的数据类型
# 可以使用dtype属性检查NumPy数组的数据类型
numbers = numpy.array([1, 2, 3, 4])
print(numbers.dtype)

nan

代码语言:javascript
复制
import numpy

# 当NumPy不能将一个值转换为浮点数或整数之类的数字数据类型时,它使用了一个特殊的nan值,表示的不是数字
# nan是缺失的数据
world_alcohol = numpy.genfromtxt("001_numpy_1.txt", delimiter='——', dtype=float, encoding='utf8')
print(type(world_alcohol))
print(world_alcohol)

切片

代码语言:javascript
复制
import numpy

# 数组切片
vector = numpy.array([5, 10, 15, 20])
# 第0个到第2个
print(vector[0:3])
print('---')
matrix = numpy.array([
    [5, 10, 15],
    [20, 25, 30],
    [35, 40, 45]
])
# 所有行的第1列
print(matrix[:, 1])
print('---')
# 所有行的 第0列到第1列
print(matrix[:, 0:2])
print('---')
# 第1行到第2行的,第0列到第1列
print(matrix[1:3, 0:2])

数组赋值判断、切片赋值判断

代码语言:javascript
复制
import numpy

# 它会将第二个值与向量中的每个元素进行比较
# 如果值相等,Python解释器返回True;否则,返回False
vector = numpy.array([5, 10, 15, 20])
print(vector == 10)
print("---1")
matrix = numpy.array([[5, 10, 15],
                      [20, 25, 30],
                      [35, 40, 45]
                      ])
print(matrix == 25)
print("---2")
# 将vector与值10进行比较,值10生成一个新的布尔向量[False、True、False、True]。它将这个结果赋给equal_to_ten
vector = numpy.array([5, 10, 15, 10])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print("---3")
# 读取布尔数组为True对应的索引数据
print(vector[equal_to_ten])
print("---4")
matrix = numpy.array([[5, 10, 15],
                      [20, 25, 30],
                      [35, 40, 45]
                      ])
# matrix 所有行的第1列是否等于25 [False  True False]
second_column_25 = (matrix[:, 1] == 25)
print(second_column_25)
print("---5")
# 读取布尔数组为True对应的索引数据(第二个元素为True,所以打印了第二个元素的数据)
print(matrix[second_column_25, :])
print("---6")
# 我们还可以对多个条件进行比较
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print(equal_to_ten_and_five)
print("---7")
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print(equal_to_ten_or_five)
print("---8")
vector[equal_to_ten_or_five] = 50
print(vector)
print("---9")
matrix = numpy.array([
    [5, 10, 15],
    [20, 25, 30],
    [35, 40, 45]
])
second_column_25 = matrix[:, 1] == 25
print(second_column_25)
print("---10")
# 将布尔数组中为True的元素,中的第0个元素赋值为10
matrix[second_column_25, 0] = 10
print(matrix)

astype

代码语言:javascript
复制
import numpy

vector = numpy.array(["1", "2", "3"])
print(vector.dtype)
print(vector)
print('---')
vector = vector.astype(float)
print(vector.dtype)
print(vector)

sum

  • 返回给定轴上数组元素的和
代码语言:javascript
复制
import numpy

vector = numpy.array([5, 10, 15, 20])
print(vector.sum())
matrix = numpy.array([[1, 2], [3, 4]])
# 返回给定轴上数组元素的和
print(matrix.sum())
print("---1")
matrix = numpy.array([
    [5, 10, 15],
    [20, 25, 30],
    [35, 40, 45]
])
# 计算1轴所有元素的和
print(matrix.shape)
print(matrix.sum(axis=1))
print("---2")
# 计算0轴所有元素的和
print(matrix.shape)
print(matrix.sum(axis=0))
print("---3")
matrix = numpy.array([
    [[1, 2, 3],
     [4, 5, 6]],
    [[11, 22, 33],
     [44, 55, 66]]
])
print("----")
print(matrix.shape)
print(matrix.sum())
print("---4")
# 原始shape为(2,2,3),返回0轴的总和,结果是的shape是:(2,3)
# 可理解为选中第0层的[],把里面的所有元素(2个(2,3)二维数组)相加,
# 所有的元素相加得到(2,3)二维数组,已无最外层,结果为(2,3)
print(matrix.shape)
print(matrix.sum(axis=0))
print("---5")
# 原始shape为(2,2,3),返回1轴的总和,结果是的shape是:(2,3)
# 可理解为选中第1层的[],把里面的所有元素(2个(1,3)一维数组)相加,
# 所有的元素相加得到(1,3)一维数组,最外层为2,结果为(2,3)
print(matrix.shape)
print(matrix.sum(axis=1))
print("---6")
# 原始shape为(2,2,3),返回2轴的总和,结果是的shape是:(2,2)
# 可理解为选中第2层的[],把里面的所有元素(数字)相加,
# 所有的元素相加得到数字,,最外层为(2,2),结果为(2,2)
print(matrix.shape)
print(matrix.sum(axis=2))

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • genfromtxt
  • array
  • shape
  • dtype
  • nan
  • 切片
  • 数组赋值判断、切片赋值判断
  • astype
  • sum
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档