陣列

Python 金融入門

Adina Howe

Professor

安裝套件

pip3 install package_name_here
pip3 install numpy
Python 金融入門

匯入套件

import numpy
Python 金融入門

NumPy 與陣列

import numpy

my_array = numpy.array([0, 1, 2, 3, 4])
print(my_array)
[0, 1, 2, 3, 4]
print(type(my_array))
<class 'numpy.ndarray'>
Python 金融入門

使用別名

import package_name
package_name.function_name(...)
import numpy as np
my_array = np.array([0, 1, 2, 3, 4])
print(my_array)
[0, 1, 2, 3, 4]
Python 金融入門

為何在財務分析用陣列?

  • 陣列可高效處理超大型資料集
    • 計算與記憶體效率高
    • 計算與分析比清單更快
    • 功能多元(Python 套件提供許多函式)
Python 金融入門

有什麼不同?

NumPy 陣列
my_array = np.array([3, 'is', True])

print(my_array)
['3' 'is' 'True']
清單(List)
my_list = [3, 'is', True]

print(my_list)
[3, 'is', True]
Python 金融入門

陣列運算

陣列
import numpy as np

array_A = np.array([1, 2, 3])
array_B = np.array([4, 5, 6])

print(array_A + array_B)
[5 7 9]
清單(List)
list_A = [1, 2, 3]
list_B = [4, 5, 6]

print(list_A + list_B)
[1, 2, 3, 4, 5, 6]
Python 金融入門

陣列索引

import numpy as np

months_array = np.array(['Jan', 'Feb', 'March', 'Apr', 'May'])

print(months_array[3])
Apr
print(months_array[2:5])
['March' 'Apr' 'May']
Python 金融入門

帶步長的切片

import numpy as np

months_array = np.array(['Jan', 'Feb', 'March', 'Apr', 'May'])
print(months_array[0:5:2])
['Jan' 'March' 'May']
Python 金融入門

一起來練習吧!

Python 金融入門

Preparing Video For Download...