python--numpy学习笔记

 
import numpy   #导入numpy扩展包
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")  #使用genfromtxt指令读取数据文件,设置分割符为","
print(type(world_alcohol)) #打印格式
输出:
<class 'numpy.ndarray'>   #numpy.ndarray为numpy的核心格式

一、NUMPY数组的创建(numpy.array):

#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
vector = numpy.array([5, 10, 15, 20])
#When we input a list of lists, we get a matrix as a result:
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print (vector)
print("-----------")
print (matrix)

#输出:
[ 5 10 15 20]
-----------
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]
#通过numpy.array可以直接创建向量或矩阵数

二、查看数组的尺寸(numpy.shape):

#We can use the ndarray.shape property to figure out how many elements are in the array
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#For matrices, the shape property contains a tuple with 2 elements.
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
(4,)   #一维数组,拥有四个元素
(2, 3) #二维数组,每个数组拥有三个元素,注意在创建多维数组的时候需要使用等量的[]
#通过shape可以查看array对象的维数

三、查看元素的类型(dtype):

#Each value in a NumPy array has to have the same data type
#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. 
#You can check the data type of a NumPy array using the dtype property.
numbers = numpy.array([1, 2, 3, 4])
numbers.dtype
dtype('int32') 
#通过dtype函数可以查看array中数据的类型
#需要注意的是,在numpy.array类型中的数据,数据格式需要完全统一
numbers = numpy.array([1, 2, 3, 4.])
numbers.dtype
print(numbers)

dtype('float64')
[1. 2. 3. 4.]
#修改了数组中其中一个元素的值,那么整个数组中所有元素的格式都会随之变化以保障完全统一

四:数据的截取:

#1、定向数据截取:
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1) print(world_alcohol)
#输出:
[[u'1986' u'Western Pacific' u'Viet Nam' u'Wine' u'0']
 [u'1986' u'Americas' u'Uruguay' u'Other' u'0.5']
 [u'1985' u'Africa' u"Cte d'Ivoire" u'Wine' u'1.62']
 ..., 
 [u'1987' u'Africa' u'Malawi' u'Other' u'0.75']
 [u'1989' u'Americas' u'Bahamas' u'Wine' u'1.5']
 [u'1985' u'Africa' u'Malawi' u'Spirits' u'0.31']]
uruguay_other_1986 = world_alcohol[1,4]   #第一行,第四列的数据
third_country = world_alcohol[2,2]        #第二行,第二列导数据
print uruguay_other_1986
print third_country
#输出:
0.5
Cte d'Ivoire
#只需要在参数中传入所需要截取数据所在的行和列,就可以取到对应的数据
#2、范围数据截取:
2.1、一维数组的范围截取: vector = numpy.array([5, 10, 15, 20]) print(vector[0:2])   #截取第2个数据之前的所有数据(不包括第2个)
#输出:
[ 5 10]
2.2、多维数组的单列范围截取:
matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,1])
#在矩阵数组中,用冒号代替所有行,截取所有行的某一列
#输出:
[10 25 40]
2.3、多维数组的多列范围截取:
matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[:,0:2])   #截取从第0列开始依次往右的共2列
#输出:
[[ 5 10]
 [20 25]
 [35 40]]
#2.4、多维数组的指定行列范围截取:
matrix = numpy.array([
                    [5, 10, 15], 
                    [20, 25, 30],
                    [35, 40, 45]
                 ])
print(matrix[1:3,0:2])  #第1行开始共3行,第0列开始共2列
#输出
[[20 25]
 [35 40]]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值