การรวมข้อมูล timeseries เข้ากับ machine learning

Machine Learning สำหรับข้อมูล Time Series ใน Python

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

ทำความรู้จักกับข้อมูลของเรา

  • ชุดข้อมูลที่ใช้ในคอร์สนี้เผยแพร่ให้ดาวน์โหลดฟรีทางออนไลน์
  • มีชุดข้อมูลให้เลือกมากมาย โดยชุดที่เราใช้มาจาก Kaggle
Machine Learning สำหรับข้อมูล Time Series ใน Python

ชุดข้อมูลเสียงหัวใจ

  • มีการบันทึกเสียงหัวใจจากผู้ป่วยหลายราย
  • บางรายมีหัวใจทำงานปกติ บางรายมีความผิดปกติ
  • ข้อมูลอยู่ในรูปแบบไฟล์เสียง พร้อม label สำหรับแต่ละไฟล์
  • เราจะหาเสียงหัวใจที่ "ผิดปกติ" ได้ไหม?
Machine Learning สำหรับข้อมูล Time Series ใน Python

โหลดข้อมูลเสียง

from glob import glob
files = glob('data/heartbeat-sounds/files/*.wav')

print(files)
['data/heartbeat-sounds/proc/files/murmur__201101051104.wav',
 ...
 'data/heartbeat-sounds/proc/files/murmur__201101051114.wav']
Machine Learning สำหรับข้อมูล Time Series ใน Python

อ่านข้อมูลเสียง

import librosa as lr
# `load` accepts a path to an audio file
audio, sfreq = lr.load('data/heartbeat-sounds/proc/files/murmur__201101051104.wav')

print(sfreq)
2205

ในที่นี้ sampling frequency คือ 2205 หมายความว่ามี 2205 ตัวอย่างต่อวินาที

Machine Learning สำหรับข้อมูล Time Series ใน Python

การอนุมานเวลาจากตัวอย่างข้อมูล

  • หากทราบ sampling rate ของ timeseries เราก็รู้ timestamp ของแต่ละจุดข้อมูล เทียบกับจุดแรก
  • หมายเหตุ: สมมติว่า sampling rate คงที่และไม่มีข้อมูลสูญหาย
Machine Learning สำหรับข้อมูล Time Series ใน Python

การสร้างอาร์เรย์เวลา (I)

  • สร้างอาร์เรย์ของ index หนึ่งตัวต่อหนึ่งตัวอย่าง แล้วหารด้วย sampling frequency

      indices = np.arange(0, len(audio))
      time = indices / sfreq
    
Machine Learning สำหรับข้อมูล Time Series ใน Python

การสร้างอาร์เรย์เวลา (II)

  • หา timestamp ของจุดข้อมูลที่ N-1 แล้วใช้ linspace() สร้างค่าระหว่าง 0 ถึงเวลานั้น

      final_time = (len(audio) - 1) / sfreq
      time = np.linspace(0, final_time, sfreq)
    
Machine Learning สำหรับข้อมูล Time Series ใน Python

ชุดข้อมูล New York Stock Exchange

  • ชุดข้อมูลนี้ประกอบด้วยราคาหุ้นของบริษัทต่าง ๆ ในช่วง 10 ปี
  • เราจะตรวจพบรูปแบบในข้อมูลอดีตเพื่อทำนายมูลค่าหุ้นในอนาคตได้ไหม?
Machine Learning สำหรับข้อมูล Time Series ใน Python

ดูข้อมูล

data = pd.read_csv('path/to/data.csv')

data.columns
Index(['date', 'symbol', 'close', 'volume'], dtype='object')
data.head()
         date symbol       close       volume
0  2010-01-04   AAPL  214.009998  123432400.0
1  2010-01-04    ABT   54.459951   10829000.0
2  2010-01-04    AIG   29.889999    7750900.0
3  2010-01-04   AMAT   14.300000   18615100.0
4  2010-01-04   ARNC   16.650013   11512100.0
Machine Learning สำหรับข้อมูล Time Series ใน Python

Timeseries กับ Pandas DataFrames

  • ตรวจสอบประเภทข้อมูลของแต่ละคอลัมน์ได้ผ่าน attribute dtypes
df['date'].dtypes
0    object
1    object
2    object
dtype: object
Machine Learning สำหรับข้อมูล Time Series ใน Python

การแปลงคอลัมน์ให้เป็น time series

  • หากต้องการให้คอลัมน์ใน DataFrame ถูกจัดการเป็น time series ให้ใช้ฟังก์ชัน to_datetime()
df['date'] = pd.to_datetime(df['date'])

df['date']
0   2017-01-01
1   2017-01-02
2   2017-01-03
Name: date, dtype: datetime64[ns]
Machine Learning สำหรับข้อมูล Time Series ใน Python

มาฝึกกันเถอะ!

Machine Learning สำหรับข้อมูล Time Series ใน Python

Preparing Video For Download...