您的位置:首页 > 编程语言 > Python开发

Numpy的array数组操作

2017-03-23 23:02 471 查看

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]


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python numpy 机器学习