Giter Site home page Giter Site logo

stevenincode / stock_industry_clustering_kmeans Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 1.0 173.18 MB

Analyzing correlations between stock market industries by studying 500 stocks with their 10 years of time-series data, using R (Kernel K-Means clustering, data wrangling) and Python (web data scraping).

Python 34.48% R 65.52%

stock_industry_clustering_kmeans's Introduction

MSDS-ML

Stock Clustering Project

Prepare 10 industries x 50 stocks with 10 years of stock data (check IPOs), which both are ranked by the Market Cap from high to low:

Follow the code file indexes (*note: always date the files since stock list may change daily):

  • 0_ind_g_gener.py:

    • scraps industry names by URLs (or hand pick), then generates "ind_g.csv" ("G1, G2..." assigned to each industry names).
  • 1_tik_scraper_ind.py:

    • automatically scraps stock tickers page by page, then generates "tik500.csv" (500 tickers saved in one column).
  • 2-3 Data preparation:

    • "master.Rda" (2266 x 4000) file is created by combining stock data from each CSV files (standardized close price, (1 + 3) shifts for each, with inversed data (500 x 4 x 2 = 4000 columns, "AIG, AIG.s1, AIG.s5, AIG.s30...", and 2266 rows for all dates from 2009 to 2017).
  • 4_ind_g_centerer.R:

    • reorders shift columns of "master" data by groups, transpose rows & columns (we want rows -> tickers, since K-Means in R clusters by row), and generates "master_tran.Rda" and "ind_center.Rda" (10 industries x 4 shifts x 2 inverses = 80 centers by taking means of the groups in master.tran, dim = 80 x 2266).
  • 5_kmeans_cluster.R:

    • cleans NAs, NaNs and Infs by 0 in master.tran (zero mean of standardized data, and K-Means cannot run if data has NA, NaN or Inf), and generates "master_kmready.Rda" & "master_tik4000_g.csv" (a list of 4000 tickers with assigned group numbers).
    • loops multiple runs of K-Means (package "ClusterR" has "K-Means++" initializer) and records computing time for each run. 100 result summaries are saved in "kmean_results_100runs_fullsummary.Rda", and 100 cluster results & center results are saved in "kmean_results_100runs.Rda".
  • 6_km_result_evaluator:

    • generates plots for the rankings (by Between.SS / Total.SS, COST values and combined ranking average).
    • saved variables in file "km_cost_rank_plot.Rda": eval.avg, ssdiv.df, cost.df, plot.ssdiv, plot.cost, plot.rankavg, plot.rankavg.candle.
  • 7_cluster_heat_mapper:

    • creates heat maps for the comparsion between any two K-Means results (by inner joining tickers of each two clusters).
    • needs more updates...

stock_industry_clustering_kmeans's People

Contributors

stevenincode avatar

Stargazers

WcW avatar  avatar ShihSyun Huang avatar

Forkers

ryanleveille

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.