科学计算库Numpy

  1. 读取txt

    import numpy
    world_lcohol=numpy.genfromtxt('world_alcohol.txt',delimiter=',',dtype=str)
    print(type(world_lcohol))
    print(world_lcohol)
    print(help(numpy.genfromtxt))
    

    运行结果:
    在这里插入图片描述

  2. numpy.array

    vector=([5,10,15,20])
    matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
    print (vector)
    print (matrix)
    

    运行代码:
    在这里插入图片描述

  3. **.shape

    vector=numpy.array([1,2,3,4])
    print(vector.shape)
    matrix=numpy.array([[5,10,15],[20,25,30]])
    print(matrix.shape)
    

    运行结果:
    在这里插入图片描述

  4. dtype

    import numpy
    numbers=numpy.array([1,2,3,4.0])
    print(numbers)
    numbers.dtype
    

    运行结果:
    在这里插入图片描述

  5. 跳过标题栏

    world_lcohol=numpy.genfromtxt('world_alcohol.txt',delimiter=',',dtype=str,skip_header=1)
    print(world_lcohol)
    

    运行结果:
    在这里插入图片描述

  6. 取值

    uruguay_other_1986=world_lcohol[1,4]
    third_country=world_lcohol[2,2]
    print(uruguay_other_1986)
    print(third_country)
    

    在这里插入图片描述

  7. 切片

    vector=numpy.array([5,10,15,20])
    print(vector[0:3])
    

    运行结果:
    在这里插入图片描述

  8. 取一列

    matrix=numpy.array([[5,10,15],
                       [20,25,30],
                       [35,40,45]
                       ])
    print(matrix[:,1])
    

    运行结果:在这里插入图片描述

  9. 两列

    print(matrix[:,0:2]) 
    

    运行结果:
    在这里插入图片描述

  10. 指定行列

    print(matrix[1:3,0:2])
    

    运行结果:
    在这里插入图片描述

  11. 查找

    import numpy
    vector=numpy.array([5,10,15,20])
    vector==10
    

    运行结果:
    在这里插入图片描述

  12. 数组查找

    matrix=numpy.array=([[5,10,15],
                        [20,25,30],
                        [35,40,45]])
    matrix==25
    

    运行结果:
    在这里插入图片描述

  13. 返回值

    equal_to_ten=(vector==10)
    print (equal_to_ten)
    print(vector[equal_to_ten])
    

    运行结果:
    在这里插入图片描述

  14. 二维数组

    matrix=numpy.array([[5,10,15],
                        [20,25,30],
                        [35,40,45]])
    second_column_25=(matrix[:,1]==25)
    print(second_column_25)
    print(matrix[second_column_25])
    

    运行结果:
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值