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

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

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

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

  1. 例如 :
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 文件

  1. 文件 : world_alcohol.txt
  2. 数据意义 : 世界饮料类型统计
  3. 数据样本 :
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 )

  1. 例如 :
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 )

  1. 例如 :
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 ,取数据 : 行 ( 单行,多行 )

  1. 取第 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']
  1. 取 ( 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 ,取数据 : 列 ( 单列,多列 )

  1. 单列 : 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' ....]
  1. 多列 : 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. 目的 :
    1 ,从原始数据中提取数据
    2 ,行号 : 2-5 行
    3 ,列号 : 2-3 列
  2. 代码 :
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]

  1. 例如 :
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)

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

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

  1. 直接创建 : np.array([1,2,3,4,5])
  2. txt 创建 : np.genfromtxt(“world_alcohol.txt”,delimiter=",",dtype=“str”,skip_header=1)
  3. 带默认值 : np.full((2,4),1,dtype=“int”)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值