Numpy的array数组操作

本文介绍了如何使用Numpy进行数组操作,包括从文件加载数据、指定数据类型、跳过文件头部、创建一维和二维数组等基本操作。通过实例演示了如何读取特定的数据格式,并展示了如何选取数组中的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Numpy的array数组操作

(一边学一边更新)
源文件
这里写图片描述

import numpy
world_alcohol = numpy.genfromtxt("E:\machinelearning\machinelearninginaction\Ch02\datingTestSet.txt",delimiter="\t")
print(type(world_alcohol))
world_alcohol

nan:无法转换文档中文件格式,默认转换的类型是float,修改类型需要自己设置dtype属性
这里写图片描述

import numpy
world_alcohol = numpy.genfromtxt("E:\machinelearning\machinelearninginaction\Ch02\datingTestSet.txt",delimiter="\t",dtype='str')
print(type(world_alcohol))
world_alcohol

这里写图片描述

#去掉第一行
import numpy
world_alcohol = numpy.genfromtxt("E:\machinelearning\machinelearninginaction\Ch02\datingTestSet.txt",delimiter="\t",dtype='U75',skip_header=1)
print(type(world_alcohol))
world_alcohol

首行文字不需要,删除掉,可以设置skip_header=1
这里写图片描述

#向量(一维数组)
vector = numpy.array([5,10,15,20])
print vector
vector.dtype
#前三个
print(vector[0:3])
print(vector[0:1])

这里写图片描述

#二维数组
matrix = numpy.array(
    [
    [54563546,1056786,15635465],[27860,22896789,36872],[50006000,68400000000,45400000000000]
    ]
)
print matrix
#科学计数法该如何表示呢?
#所有行的第一列
print '=============================================================='
print matrix[:,1]
#所有行的前两列
print matrix[:,0:2]

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值