Python的Numpy数组np.array()基本用法详解(一)

  1. 从list到array
import numpy as np

# np.arrays
m_int_list = [2, 0, 2, -1, 9, 0]
m_two_list = [[2, 0, 2], [2, -1, 9], [9, 0, 2]]
# attempt to get an array from list, and use function tolist() to get back
np_int_array_from_list = np.array(m_int_list)
print(np_int_array_from_list)   # [ 2  7  2  8  9 10]
  1. 指定数据类型
# specify the datatype like: int str complex float bool
np_bool_array_from_list = np.array(m_int_list, dtype=bool)
print(np_bool_array_from_list)  # [ True False  True  True  True False]
  1. 查看形状、大小、数据类型【shape size dtype】
np_two_from_list = np.array(m_two_list)
print(np_two_from_list.shape)   # (3, 3)
print(np_two_from_list.size)   # 9
print(np_two_from_list.dtype)   # int32
  1. 数组中的基本数学运算操作
# Mathematical Operation: +, -, *, /, //, %, **
np_square_from_two = np_two_from_list ** 2
print(np_square_from_two)
"""
[[ 4  0  4]
 [ 4  1 81]
 [81  0  4]]
"""
以上是np.array()的一些最基本的用法、属性(似乎与list没有区别?),实际上array还有更多的特性,详见下一篇:Python的Numpy数组np.array()基本用法详解(二)
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值