Numpy的array数组操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/monkeysheep1234/article/details/65462926

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]

这里写图片描述

猜你喜欢

转载自blog.csdn.net/monkeysheep1234/article/details/65462926