Dasar-dasar Machine Learning

Machine Learning untuk Data Deret Waktu di Python

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

Selalu mulai dengan meninjau data Anda

array.shape
(10, 5)
array[:3]
array([[ 0.735528  ,  1.00122818, -0.28315978],
       [-0.94478393,  0.18658748, -0.00241224],
       [-0.74822942, -1.46636618,  0.69835096]]) 
Machine Learning untuk Data Deret Waktu di Python

Selalu mulai dengan meninjau data Anda

df.head()
       col1      col2      col3
0  0.735528  1.001228 -0.283160
1 -0.944784  0.186587 -0.002412
2 -0.748229 -1.466366  0.698351
3  1.038589 -0.171248  0.831457
4 -0.161904  0.003972 -0.321933
Machine Learning untuk Data Deret Waktu di Python

Selalu visualisasikan data Anda

Pastikan tampilannya sesuai harapan.

# Menggunakan matplotlib
fig, ax = plt.subplots()
ax.plot(...)

# Menggunakan pandas
fig, ax = plt.subplots()
df.plot(..., ax=ax)
Machine Learning untuk Data Deret Waktu di Python

Scikit-learn

Scikit-learn adalah pustaka Machine Learning paling populer di Python

from sklearn.svm import LinearSVC
Machine Learning untuk Data Deret Waktu di Python

Menyiapkan data untuk scikit-learn

  • scikit-learn mengharapkan struktur data tertentu:

    (samples, features)

  • Pastikan data Anda minimal dua dimensi

  • Pastikan dimensi pertama adalah samples

Machine Learning untuk Data Deret Waktu di Python

Jika bentuk data Anda tidak benar

  • Jika sumbu tertukar:
array.T.shape
(10, 3)
Machine Learning untuk Data Deret Waktu di Python

Jika bentuk data Anda tidak benar

  • Jika ada sumbu yang hilang, gunakan .reshape():
array.shape
(10,)
array.reshape(-1, 1).shape
(10, 1)
  • -1 akan otomatis mengisi sumbu itu dengan nilai yang tersisa
Machine Learning untuk Data Deret Waktu di Python

Melatih model dengan scikit-learn

# Impor support vector classifier
from sklearn.svm import LinearSVC

# Buat instance model
model = LinearSVC()

# Latih model pada data
model.fit(X, y)

Umumnya y berdimensi (samples, 1)

Machine Learning untuk Data Deret Waktu di Python

Menganalisis model

# Ada satu koefisien per fitur masukan
model.coef_
array([[ 0.69417875, -0.5289162 ]])
Machine Learning untuk Data Deret Waktu di Python

Memprediksi dengan model yang telah dilatih

# Hasilkan prediksi
predictions = model.predict(X_test)
Machine Learning untuk Data Deret Waktu di Python

Ayo berlatih

Machine Learning untuk Data Deret Waktu di Python

Preparing Video For Download...