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”)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章