<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aarsh Trivedi on Medium]]></title>
        <description><![CDATA[Stories by Aarsh Trivedi on Medium]]></description>
        <link>https://medium.com/@aarsh.trivedi?source=rss-acc7ef7fcd29------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CLILCmR1n-EgR2RV7aUmTg.jpeg</url>
            <title>Stories by Aarsh Trivedi on Medium</title>
            <link>https://medium.com/@aarsh.trivedi?source=rss-acc7ef7fcd29------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 22:41:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aarsh.trivedi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Time-step wise feature importance in deep learning using SHAP]]></title>
            <link>https://medium.datadriveninvestor.com/time-step-wise-feature-importance-in-deep-learning-using-shap-e1c46a655455?source=rss-acc7ef7fcd29------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1c46a655455</guid>
            <category><![CDATA[feature-importance]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[market-share-prediction]]></category>
            <dc:creator><![CDATA[Aarsh Trivedi]]></dc:creator>
            <pubDate>Fri, 02 Nov 2018 03:22:40 GMT</pubDate>
            <atom:updated>2018-11-14T13:06:55.758Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BhfNkNUlPPi_MH7Q" /><figcaption>“silhouette of coconut trees” by <a href="https://unsplash.com/@ethanrobertson?utm_source=medium&amp;utm_medium=referral">Ethan Robertson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Stock market prediction is usually considered as one of the most challenging issues among time series predictions due to its noise and volatile features.</p><p>We are going to see how to derive feature importance for share market deep learning model using SHAP library in python. One important thing for business related time series is that, it is not enough to just predict the outcome. It is equally important to derive which features considered important by the model to enhance business decisions.</p><p>One advantage of deep learning over machine learning is, deep learning can deal with large amount of data with accuracy but DL is considered by people as a black box models and hence they are less preferred for business or economics modelling. Hence, we are going to see how feature importance can be derived using SHAP library.</p><p><strong>Data and Modelling</strong></p><p>Below is a brief about what data is used and how model prediction is done. For full code you can visit on <a href="https://github.com/aarsht/stock-prediction">Github</a>.</p><p>We will be using BSE share market data from yahoo finance. Technical indicators are derived from OHLC data itself as data features. Using LSTM model, I have got around 60% binary classification accuracy for buy and sell predictions. Which is not so good, may be due to low number of records used for deep learning model (around 3K daily records for last 10 years working days). If premium dataset like hourly or to-the-minute can be used, then we can achieve much higher accuracy.</p><p>For prediction, LSTM model is used which takes below form of 3D data.</p><p>(samples, time steps, features)</p><p>Here, we have taken no of time-steps as 2 which is decided through tuning experiments.</p><p>Below graph shows true and false predictions on test dataset time series. The ups and downs in the graph meant for actual buy and sell decisions. We can see that at which points, the model succeeded to predict the decision and where it fails.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*5IyCWUjsmv9tv4GsayPXHw.png" /></figure><p><strong>SHAP Library and Feature Importance</strong></p><p><a href="https://github.com/slundberg/shap">SHAP</a> (SHapley Additive exPlanations) is a unified approach to explain the output of any machine learning model. As explained well on github page, SHAP connects game theory with local explanations.</p><p>Unlike other black box machine learning explainers in python, SHAP can take 3D data as an input. In this case, the output will be 3D data containing importance for each sample, time steps, features in that order. If we sum up the output on 0th dimension, (i.e. sample axis) we get 2D data containing feature importance as columns and time steps as rows. This way SHAP gives time step wise feature importance.</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>import pandas as pd<br>from keras.models import load_model<br>import shap</pre><pre>regressor = load_model(‘lstm_stock.h5’)<br>pred_x = regressor.predict_classes(X_train)<br>random_ind = np.random.choice(X_train.shape[0], 1000, replace=False)<br>print(random_ind)<br>data = X_train[random_ind[0:500]]<br>e = shap.DeepExplainer((regressor.layers[0].input, regressor.layers[-1].output),data)<br>test1 = X_train[random_ind[500:1000]]<br>shap_val = e.shap_values(test1)<br>shap_val = np.array(shap_val)<br>shap_val = np.reshape(shap_val,(int(shap_val.shape[1]),int(shap_val.shape[2]),int(shap_val.shape[3])))<br>shap_abs = np.absolute(shap_val)<br>sum_0 = np.sum(shap_abs,axis=0)<br>f_names = [‘RSI_14D’,’STOK’,’STOD’,’ROC’,’Momentum’,’CCI’,’ADX’,’MACD’,’Money_Flow_Index’,’WillR’,’INRchange’]<br>x_pos = [i for i, _ in enumerate(f_names)]<br>plt1 = plt.subplot(311)<br>plt1.barh(x_pos,sum_0[1])<br>plt1.set_yticks(x_pos)<br>plt1.set_yticklabels(f_names)<br>plt1.set_title(“Yesterday’s features (time-step 2)”)<br>plt2 = plt.subplot(312,sharex=plt1)<br>plt2.barh(x_pos,sum_0[0])<br>plt2.set_yticks(x_pos)<br>plt2.set_yticklabels(f_names)<br>plt2.set_title(“The day before yesterday’s features(time-step 1)”)<br>plt.tight_layout()<br>plt.show()</pre><p>We have taken 500 random samples from train dataset to pass in DeepExplainer class as this much data gives very accurate results. Passing the entire train data will give highly accurate values, but will be unreasonably expensive as the complexity of this method scales linearly with the number of data points.</p><p>Below graph is showing feature importance on 2 time-steps. The values displayed here are output of shap values summed up on sample axis. 7 of the 11 features of yesterday are more weighted than features of day before yesterday. Hence, near future values matter more in buy-sell decisions for share market prediction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*8V0njCwLoce3AyFMukCkdw.png" /></figure><p><strong>Conclusion</strong></p><p>In the end, I would like to add that economic researchers can use this knowledge to analyse the relationship among macroeconomic variables and stock prices of the share market.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1c46a655455" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/time-step-wise-feature-importance-in-deep-learning-using-shap-e1c46a655455">Time-step wise feature importance in deep learning using SHAP</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>