मशीन लर्निंग बेसिक्स

Python में Time Series Data के लिए Machine Learning

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

हमेशा शुरुआत डेटा देखकर करें

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]]) 
Python में Time Series Data के लिए Machine Learning

हमेशा शुरुआत डेटा देखकर करें

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
Python में Time Series Data के लिए Machine Learning

हमेशा डेटा विज़ुअलाइज़ करें

देखें कि यह आपकी अपेक्षा जैसा दिख रहा है।

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

# Using pandas
fig, ax = plt.subplots()
df.plot(..., ax=ax)
Python में Time Series Data के लिए Machine Learning

Scikit-learn

Scikit-learn Python में सबसे लोकप्रिय मशीन लर्निंग लाइब्रेरी है

from sklearn.svm import LinearSVC
Python में Time Series Data के लिए Machine Learning

scikit-learn के लिए डेटा तैयार करना

  • scikit-learn डेटा की एक खास संरचना अपेक्षित करता है:

    (samples, features)

  • सुनिश्चित करें कि आपका डेटा कम से कम दो-आयामी हो

  • सुनिश्चित करें कि पहली आयाम samples हो

Python में Time Series Data के लिए Machine Learning

अगर डेटा का shape सही नहीं है

  • अगर axes उलटे हों:
array.T.shape
(10, 3)
Python में Time Series Data के लिए Machine Learning

अगर डेटा का shape सही नहीं है

  • अगर एक axis गायब है, तो .reshape() उपयोग करें:
array.shape
(10,)
array.reshape(-1, 1).shape
(10, 1)
  • -1 उस axis को शेष मानों से स्वतः भर देगा
Python में Time Series Data के लिए Machine Learning

scikit-learn से मॉडल फिट करना

# एक सपोर्ट वेक्टर क्लासिफायर इम्पोर्ट करें
from sklearn.svm import LinearSVC

# मॉडल instantiate करें
model = LinearSVC()

# कुछ डेटा पर मॉडल fit करें
model.fit(X, y)

अक्सर y का shape (samples, 1) होता है

Python में Time Series Data के लिए Machine Learning

मॉडल की जाँच

# हर input feature के लिए एक coefficient होता है
model.coef_
array([[ 0.69417875, -0.5289162 ]])
Python में Time Series Data के लिए Machine Learning

फिट मॉडल से प्रेडिक्ट करना

# predictions जनरेट करें
predictions = model.predict(X_test)
Python में Time Series Data के लिए Machine Learning

अभ्यास करते हैं

Python में Time Series Data के लिए Machine Learning

Preparing Video For Download...