zoukankan      html  css  js  c++  java
  • Python数据分析与机器学习-NumPy_2

    import numpy
    # it will compare the second value to each element in the vector
    # If the value are equal, the Python interpreter returns True; otherwise, it return False
    vector = numpy.array([5,10,15,20])
    vector == 10
    
    array([False,  True, False, False])
    
    matrix = numpy.array([
        [5,10,15],
        [20,25,30],
        [35,40,45]])
    matrix == 25
    
    array([[False, False, False],
           [False,  True, False],
           [False, False, False]])
    
    # Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
    vector = numpy.array([5,10,15,20])
    equal_to_ten = (vector == 10)
    print(equal_to_ten)
    print(vector[equal_to_ten])
    
    [False  True False False]
    [10]
    
    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,:]) # return the raw corresponding to True
    
    [False  True False]
    [[20 25 30]]
    
    # We can also perform comparisons with multipe conditions
    vector = numpy.array([5,10,15,20])
    equal_to_ten_and_five = (vector == 10) & (vector == 5)
    print(equal_to_ten_and_five)
    
    [False False False False]
    
    vector = numpy.array([5,10,15,20])
    equal_to_ten_or_five = (vector == 10) | (vector == 5)
    print(equal_to_ten_or_five)
    
    [ True  True False False]
    
    # We can convert the date type of an array with the ndarray.astype() method.
    vector = numpy.array(["1","2","3"])
    print(vector.dtype)
    print(vector)
    vector = vector.astype(float)
    print(vector.dtype)
    print(vector)
    
    <U1
    ['1' '2' '3']
    float64
    [1. 2. 3.]
    
    vector = numpy.array([5,10,15,20])
    vector.sum()
    
    50
    
    # The axis dictates which dimension we perform the operator on
    # 1 menas that we want to perform the operation on each row, and 0 means on each column
    matrix = numpy.array([[5,10,15],
                          [20,25,30],
                          [35,40,45]])
    matrix.sum(axis=1)
    
    array([ 30,  75, 120])
    
    matrix = numpy.array([[5,10,15],
                          [20,25,30],
                          [35,40,45]])
    matrix.sum(axis=0)
    
    array([60, 75, 90])
    
    
    
  • 相关阅读:
    Hdu 2389 二分匹配
    Hdu 1156
    Hdu 1255
    Hdu 1542
    python 中初始化二维数组的方法
    chrome扩展小试
    浏览器的重绘与重排
    js执行环境相关
    js 触摸事件
    js柯里化的一个应用
  • 原文地址:https://www.cnblogs.com/SweetZxl/p/11124173.html
Copyright © 2011-2022 走看看