{"id":3302,"date":"2017-05-23T18:08:27","date_gmt":"2017-05-23T16:08:27","guid":{"rendered":"https:\/\/geekosas.com\/?p=3302"},"modified":"2026-05-23T18:08:52","modified_gmt":"2026-05-23T16:08:52","slug":"sparse-matrix-in-r","status":"publish","type":"post","link":"https:\/\/geekosas.com\/index.php\/2017\/05\/23\/sparse-matrix-in-r\/","title":{"rendered":"Sparse Matrix in R"},"content":{"rendered":"<p>The values 0 in matrices are very frequent, especially in dummy variables, so in R there is a package called Matrix which allows creating sparse matrices, in other words, matrices that do not use memory when an element&#8217;s value is 0.<\/p>\n<p>But skipping the zeros is computationally complex, so it is not always convenient to use sparse matrices. This is shown in the following results where different measurements were made:<br \/>\n<strong>Memory Usage<\/strong><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2214\" data-permalink=\"https:\/\/geekosas.com\/index.php\/es\/2017\/10\/14\/sparse-matrix-en-r\/mb\/\" data-orig-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?fit=1999%2C1999&amp;ssl=1\" data-orig-size=\"1999,1999\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"mb\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?fit=1024%2C1024&amp;ssl=1\" class=\"alignnone size-large wp-image-2214\" src=\"https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/mb-1024x1024.png?resize=810%2C810\" alt=\"\" width=\"810\" height=\"810\" srcset=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?resize=1024%2C1024&amp;ssl=1 1024w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?resize=768%2C768&amp;ssl=1 768w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mb.png?w=1999&amp;ssl=1 1999w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/p>\n<p><strong>Time in M^2<\/strong><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2215\" data-permalink=\"https:\/\/geekosas.com\/index.php\/es\/2017\/10\/14\/sparse-matrix-en-r\/mmult\/\" data-orig-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?fit=1921%2C1631&amp;ssl=1\" data-orig-size=\"1921,1631\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"mmult\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?fit=1024%2C869&amp;ssl=1\" class=\"alignnone size-large wp-image-2215\" src=\"https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/mmult-1024x869.png?resize=810%2C687\" alt=\"\" width=\"810\" height=\"687\" srcset=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?resize=1024%2C869&amp;ssl=1 1024w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?resize=300%2C255&amp;ssl=1 300w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?resize=768%2C652&amp;ssl=1 768w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/mmult.png?w=1921&amp;ssl=1 1921w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/p>\n<p><strong>SVD Decomposition<\/strong><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2216\" data-permalink=\"https:\/\/geekosas.com\/index.php\/es\/2017\/10\/14\/sparse-matrix-en-r\/svd\/\" data-orig-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?fit=1921%2C1631&amp;ssl=1\" data-orig-size=\"1921,1631\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"svd\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?fit=1024%2C869&amp;ssl=1\" class=\"alignnone size-large wp-image-2216\" src=\"https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/svd-1024x869.png?resize=810%2C687\" alt=\"\" width=\"810\" height=\"687\" srcset=\"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?resize=1024%2C869&amp;ssl=1 1024w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?resize=300%2C255&amp;ssl=1 300w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?resize=768%2C652&amp;ssl=1 768w, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/svd.png?w=1921&amp;ssl=1 1921w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/p>\n<p>As you can see, in general it is advisable to use sparse matrices from 40% zeros; before that, it will only use more memory and more CPU.<\/p>\n<p>The code used for the experiment is at: <a href=\"https:\/\/github.com\/danielfm123\/R_matrix_benchmark\"><a href=\"https:\/\/github.com\/danielfm123\/R_matrix_benchmark\">https:\/\/github.com\/danielfm123\/R_matrix_benchmark<\/a><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The values 0 in matrices are very frequent, especially in dummy variables, so in R there is a package called Matrix which allows creating sparse <a class=\"mh-excerpt-more\" href=\"https:\/\/geekosas.com\/index.php\/2017\/05\/23\/sparse-matrix-in-r\/\" title=\"Sparse Matrix in R\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2217,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-3302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sin-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/10\/matrix.png?fit=345%2C190&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8vjqF-Rg","jetpack-related-posts":[{"id":3304,"url":"https:\/\/geekosas.com\/index.php\/2017\/05\/23\/r-vs-ms-open-r-2\/","url_meta":{"origin":3302,"position":0},"title":"R vs MS open R","author":"Daniel Fischer","date":"2017-05-23","format":false,"excerpt":"Microsoft has realized the power of R, so it has integrated it into its systems, including Power BI and SQL Server 2017. Microsoft released a free version of R with some improvements, including native SQL Server connection, package versioning in MRAN (Microsoft Cran), and optimizations in the linear algebra package.\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/lm-1024x932.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/lm-1024x932.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/lm-1024x932.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.geekosas.com\/wp-content\/uploads\/2017\/10\/lm-1024x932.png?resize=700%2C400 2x"},"classes":[]},{"id":3337,"url":"https:\/\/geekosas.com\/index.php\/2019\/05\/23\/r-and-super-powerful-pcs\/","url_meta":{"origin":3302,"position":1},"title":"R and super-powerful PCs","author":"Daniel Fischer","date":"2019-05-23","format":false,"excerpt":"R and Python are very popular lately, but have you asked yourself if you are getting the most out of these languages? In fact, you probably have a super\u2011powerful PC to work with them. Well, the truth is that if you are entering the world of Analytics, you might not\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2019\/02\/2922898-a.jpg?fit=350%2C350&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3243,"url":"https:\/\/geekosas.com\/index.php\/2026\/05\/09\/comparison-between-julia-python-and-r\/","url_meta":{"origin":3302,"position":2},"title":"Comparison between Julia, Python, and R","author":"Daniel Fischer","date":"2026-05-09","format":false,"excerpt":"The discussion about which language is best for data analysis can lead to conversations more passionate than topics like religion or politics. But as Data Scientists we must focus on empirical evidence; the dimensions for comparison are many: Community, Performance, Editors, Package Manager, Code Encapsulation, etc. I have evaluated several\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2019\/09\/external-content.duckduckgo.com_.jpg?fit=474%2C312&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3281,"url":"https:\/\/geekosas.com\/index.php\/2016\/05\/23\/multi-core-performance-in-r\/","url_meta":{"origin":3302,"position":3},"title":"Multi-Core Performance in R","author":"Daniel Fischer","date":"2016-05-23","format":false,"excerpt":"Introduction A few days ago, while walking around, I saw that they were selling a used HP Proliant DL360 G6. For those who don't know, it's a high\u2011performance server from 2010. Due to my interest in Data Science and BIG DATA, this toy will be useful for a diploma course\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3296,"url":"https:\/\/geekosas.com\/index.php\/2017\/05\/23\/i-will-teach-an-r-course\/","url_meta":{"origin":3302,"position":4},"title":"I will teach an R course.","author":"Daniel Fischer","date":"2017-05-23","format":false,"excerpt":"The course will be at Microsoft Chile on September 22: The R Intensive is an event designed for those who have data analysis and modeling needs in their work and want to gain in 1 day the theoretical and practical knowledge to start solving their analytical challenges with this tool.\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2016\/11\/RStudio-Ball.png?fit=1000%2C1000&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3300,"url":"https:\/\/geekosas.com\/index.php\/2017\/05\/23\/data-science-course-in-r\/","url_meta":{"origin":3302,"position":5},"title":"Data Science course in R","author":"Daniel Fischer","date":"2017-05-23","format":false,"excerpt":"On October 20, I will teach a Data Science course using R. The topics covered will be: Data Cleaning Supervised classification models: Logistic regression. Naive Bayes. Trees and Random Forest. Neural Networks. Grid Search (hyperparameter tuning). Evaluation of classification models. Unsupervised models: Dimensionality Reduction (PCA) -> Applied example of replicating\u2026","rel":"","context":"In &quot;Sin categor\u00eda&quot;","block_context":{"text":"Sin categor\u00eda","link":"https:\/\/geekosas.com\/index.php\/category\/sin-categoria\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/09\/Rprogramming.jpg?fit=1176%2C664&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/09\/Rprogramming.jpg?fit=1176%2C664&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/09\/Rprogramming.jpg?fit=1176%2C664&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/09\/Rprogramming.jpg?fit=1176%2C664&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/geekosas.com\/wp-content\/uploads\/2017\/09\/Rprogramming.jpg?fit=1176%2C664&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/posts\/3302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/comments?post=3302"}],"version-history":[{"count":1,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/posts\/3302\/revisions"}],"predecessor-version":[{"id":3303,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/posts\/3302\/revisions\/3303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/media\/2217"}],"wp:attachment":[{"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/media?parent=3302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/categories?post=3302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekosas.com\/index.php\/wp-json\/wp\/v2\/tags?post=3302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}