NumPy详细API第一篇

以下代码是基于python3.5.0写的
import numpy

# 读取文件内容
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype="float",skip_header=1)
print(type(world_alcohol))
print(world_alcohol)

vector = numpy.array([1, 2, 3, 4])                    #构造数组
print(vector.shape)                                   #打印数组形状
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])     #构造矩阵
print(matrix.shape)                                   #打印矩阵形状

numbers = numpy.array([1, 2, 3, 4])                   # 查看数组中元素类型
print(numbers.dtype)

uruguay_other_1986 = world_alcohol[1,4]                # 读取第1行第4个元素
third_country = world_alcohol[2,2]                     # 读取第2行第2个元素

vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])                                     # 查找数组前三个值

matrix = numpy.array([
                    [5, 10, 15],
                    [20, 25, 30],
                    [35, 40, 45]])
print(matrix[:,1])                  # :代表所有的意思,查找所有行第一列的值[10 25 40]

matrix = numpy.array([
                    [5, 10, 15],
                    [20, 25, 30],
                    [35, 40, 45]])
print(matrix[:,0:2])                 # 查找所有行,前两列的值[[ 5 10][20 25][35 40]]

matrix = numpy.array([
                    [5, 10, 15],
                    [20, 25, 30],
                    [35, 40, 45]])
print(matrix[1:3,0:2])               # 查找23行,前两列的值[[20 25][35 40]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值