03 ,ndarray 创建,取值 :创建 ndarray,读文件,取数据,取行,取列,取元素

    技术2022-07-14  78

    1 ,创建 ndarray : 1 维向量 ( np.array )

    例如 : if __name__ == '__main__': nd01 = np.array([1,2,3,4,5]) print(nd01) =============================== [1 2 3 4 5]

    2 ,创建 ndarray : 2 维向量 ( np.array )

    例如 : if __name__ == '__main__': nd01 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(nd01) =============================== [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]

    3 ,数据准备 : txt 文件

    文件 : world_alcohol.txt数据意义 : 世界饮料类型统计数据样本 : Year,WHO region,Country,Beverage Types,Display Value 1986,Western Pacific,Viet Nam,Wine,0 1986,Americas,Uruguay,Other,0.5 1985,Africa,Cte d'Ivoire,Wine,1.62

    4 ,读文件 : 分隔符 ( delimiter )

    例如 : if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str") print(nd01[0:3]) ================================ [['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value'] ['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0'] ['1986' 'Americas' 'Uruguay' 'Other' '0.5']]

    5 ,读文件 : 跳过标题行 ( skip_header )

    例如 : if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) print(nd01[0:3]) ====================================== [['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0'] ['1986' 'Americas' 'Uruguay' 'Other' '0.5'] ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']]

    6 ,取数据 : 行 ( 单行,多行 )

    取第 3 行 : nd01[2] if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) res = nd01[2] print(res) ========================================================== ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62'] 取 ( 2,3,4 ) 行 : nd01[1:4] if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) res = nd01[1:4] print(res) ============================================================== [['1986' 'Americas' 'Uruguay' 'Other' '0.5'] ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62'] ['1986' 'Americas' 'Colombia' 'Beer' '4.27']]

    7 ,取数据 : 列 ( 单列,多列 )

    单列 : nd01[:,1] if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) res = nd01[:,1] print(res) print(type(res)) ================================================ 结果 : 得到一个集合 ( 一维的 ndarray ) ['Western Pacific' 'Americas' 'Africa' ....] 多列 : nd01[:,1:3] if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) res = nd01[:,1:3] print(res) print(type(res)) ====================================== [['Western Pacific' 'Viet Nam'] ['Americas' 'Uruguay'] ['Africa' "Cte d'Ivoire"] ... ['Africa' 'Malawi'] ['Americas' 'Bahamas'] ['Africa' 'Malawi']]

    9 ,取数据 : 多行多列 ( nd01[1:5,1:3] )

    目的 : 1 ,从原始数据中提取数据 2 ,行号 : 2-5 行 3 ,列号 : 2-3 列代码 : if __name__ == '__main__': # 文件名,分隔符,数据类型 nd01 = np.genfromtxt("world_alcohol.txt",delimiter=",",dtype="str",skip_header=1) res = nd01[1:5,1:3] print(res) print(type(res)) ================================== [['Americas' 'Uruguay'] ['Africa' "Cte d'Ivoire"] ['Americas' 'Colombia'] ['Americas' 'Saint Kitts and Nevis']]

    10 ,取元素 :nd01[1,2]

    例如 : if __name__ == '__main__': nd01 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) res = nd01[1,2] print(nd01) print(res)

    11 ,创建 ndarray : 带默认值 np.full((2,4),1)

    目的 : 创建 2 行 4 列的矩阵,默认值为 1代码 : if __name__ == '__main__': nd01 = np.full((2,4),1,dtype="int") print(nd01) ========================== [[1 1 1 1] [1 1 1 1]]

    12 ,创建 ndarray : 数组创建,txt 创建,带默认值创建

    直接创建 : np.array([1,2,3,4,5])txt 创建 : np.genfromtxt(“world_alcohol.txt”,delimiter=",",dtype=“str”,skip_header=1)带默认值 : np.full((2,4),1,dtype=“int”)
    Processed: 0.020, SQL: 9