<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6000112949648041507</id><updated>2012-01-24T09:50:53.890-08:00</updated><category term='high performance'/><category term='mobile'/><category term='chaco'/><category term='clustering'/><category term='pugpe'/><category term='wordTree'/><category term='data mining'/><category term='sms'/><category term='recall'/><category term='logs'/><category term='confusion matrix'/><category term='web'/><category term='expert systems'/><category term='projects'/><category term='bayesian'/><category term='ipython'/><category term='mapreduce'/><category term='library'/><category term='jogos'/><category term='kmeans'/><category term='prolog'/><category term='classification'/><category term='nlp'/><category term='I.A.'/><category term='location'/><category term='applications'/><category term='cell phones'/><category term='SOM'/><category term='cython'/><category term='otimizacao'/><category term='tips'/><category term='scipy'/><category term='e-mail'/><category term='symbian'/><category term='sensores'/><category term='video'/><category term='performance'/><category term='traits'/><category term='crab'/><category term='review'/><category term='probability'/><category term='preprocessing'/><category term='joblib'/><category term='scientific'/><category term='no sql'/><category term='visualization'/><category term='keynotes'/><category term='logic'/><category term='intelligent agents'/><category term='computacao evolucionaria'/><category term='pymongo'/><category term='perceptron'/><category term='groups'/><category term='robots'/><category term='mobile payment'/><category term='ghmm'/><category term='sentiment analysis'/><category term='algorithm'/><category term='scikit'/><category term='Kohonen'/><category term='game'/><category term='bayes theorem'/><category term='distances metrics'/><category term='infographic'/><category term='online'/><category term='logic programming'/><category term='adaline'/><category term='lecture'/><category term='mlp'/><category term='numpy'/><category term='social networks'/><category term='problems'/><category term='alert'/><category term='practices'/><category term='minimax'/><category term='multiscaling'/><category term='optimization'/><category term='design'/><category term='framework'/><category term='architecture'/><category term='hmm'/><category term='funcoes'/><category term='compiler'/><category term='examples'/><category term='json'/><category term='google'/><category term='matplotlib'/><category term='pypso'/><category term='automobilistica'/><category term='operator'/><category term='problemas'/><category term='slides'/><category term='challenge'/><category term='introduction'/><category term='roc curves'/><category term='inteligencia de enxame'/><category term='collaborative filtering'/><category term='chatterbot'/><category term='information filtering'/><category term='diagnosticar'/><category term='text mining'/><category term='github'/><category term='pso'/><category term='interface grafica'/><category term='computer-assistance'/><category term='normalization'/><category term='graph'/><category term='conference'/><category term='large datasets'/><category term='tf-idf'/><category term='ubigraph'/><category term='GUI'/><category term='agentes inteligentes'/><category term='pys60'/><category term='nokia'/><category term='evaluation'/><category term='opensource'/><category term='caixeiro viajante'/><category term='python'/><category term='analysis'/><category term='shell'/><category term='amazon'/><category term='algoritmos geneticos'/><category term='course'/><category term='class'/><category term='script'/><category term='inteligencia artificial'/><category term='scientifc programming'/><category term='crawler'/><category term='linear regression'/><category term='apontador'/><category term='artificial intelligence'/><category term='redes neurais'/><category term='prediction'/><category term='slope one recommender'/><category term='database'/><category term='bots'/><category term='portuguese'/><category term='logistic regression'/><category term='arvores de decisao'/><category term='atepassar'/><category term='recommendation systems'/><category term='soap'/><category term='cloud computing'/><category term='REST'/><category term='markov'/><category term='tutorial'/><category term='abduction'/><category term='pythonbrasil'/><category term='hierarchical clustering'/><category term='precision'/><category term='web services'/><category term='reasoning'/><category term='chart'/><category term='blog'/><category term='book'/><category term='mongodb'/><category term='API'/><category term='product recommendations'/><category term='friendship'/><category term='hello world'/><category term='3D'/><category term='neuronios'/><category term='swi'/><category term='twitter'/><category term='regularization'/><category term='article'/><category term='master degree'/><category term='machine learning'/><category term='pyfoursquare'/><category term='social media'/><category term='data'/><category term='pypy'/><category term='profiling'/><category term='foursquare'/><category term='mobile marketing'/><category term='gource'/><title type='text'>Artificial Intelligence in Motion</title><subtitle type='html'>Blog about topics related to Artificial Intelligence.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>85</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-4167564418501026213</id><published>2012-01-12T20:10:00.000-08:00</published><updated>2012-01-12T20:10:44.038-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='infographic'/><category scheme='http://www.blogger.com/atom/ns#' term='practices'/><category scheme='http://www.blogger.com/atom/ns#' term='recommendation systems'/><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='product recommendations'/><title type='text'>The Anatomy of Product Recommendations - Infographic - Tips and Best Practices</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;I'd like to share a post that I found recently from a product's blog called &lt;b&gt;&lt;a href="http://www.liftsuggest.com/"&gt;LiftSuggest&lt;/a&gt;&lt;/b&gt;. It is focused on recommender engines for e-commerces and give several tips about how to design your product recommender at your website for maximum clicks and to increase your sales.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;They posted a infographic about the anatomy of a product recommender engine. The tips they give are quite important when you are designing a recommender system for your e-commerce.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://www.liftsuggest.com/blog/wp-content/uploads/Infographic-product-recommendations-13.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="640" src="http://www.liftsuggest.com/blog/wp-content/uploads/Infographic-product-recommendations-13.jpg" width="278" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Product Recommender Engine&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;Further information please access their &lt;a href="http://www.liftsuggest.com/blog/"&gt;blog&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;&lt;br /&gt;Marcel Caraciolo&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-4167564418501026213?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/4167564418501026213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2012/01/anatomy-of-product-recommendations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/4167564418501026213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/4167564418501026213'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2012/01/anatomy-of-product-recommendations.html' title='The Anatomy of Product Recommendations - Infographic - Tips and Best Practices'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-2279521221936228567</id><published>2012-01-04T18:16:00.000-08:00</published><updated>2012-01-05T04:47:32.819-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='mobile'/><category scheme='http://www.blogger.com/atom/ns#' term='social media'/><category scheme='http://www.blogger.com/atom/ns#' term='recommendation systems'/><category scheme='http://www.blogger.com/atom/ns#' term='architecture'/><category scheme='http://www.blogger.com/atom/ns#' term='artificial intelligence'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='web'/><category scheme='http://www.blogger.com/atom/ns#' term='social networks'/><category scheme='http://www.blogger.com/atom/ns#' term='large datasets'/><title type='text'>Deepjewel -  Social Media powering Recommendations</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Happy new year! &amp;nbsp;My first post this year will be about an idea that I had with my friend Ricardo Caspirro about the next generation of social recommenders in commerce applications and retail stores. What excites me is that this idea came from a conversation that we had in 2009, and since that year we discussed more about what it would be the "&lt;b&gt;Deepjewel&lt;/b&gt;".&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Deepjewel is a giant knowledge base that encapsulates interesting entities and relationships of the social world in the web. &amp;nbsp;The social world in this context means all the millions and billions of tweets, Facebook messages, profiles, relationships, blog posting, YouTube videos, and more - a living organism itself, constantly evolving.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-yJ9N6iEZpIQ/TwWYO9LJeeI/AAAAAAAAAsU/OrQSslN29Dk/s1600/Screen+shot+2012-01-05+at+9.34.19+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="351" src="http://4.bp.blogspot.com/-yJ9N6iEZpIQ/TwWYO9LJeeI/AAAAAAAAAsU/OrQSslN29Dk/s400/Screen+shot+2012-01-05+at+9.34.19+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;The Deepjewel&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: verdana, arial, helvetica; margin-bottom: 12px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: center; vertical-align: baseline;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;But what motivated us to create the Deepjewel? &amp;nbsp;One of main problems that we face nowadays is the discovery of content and items of our interest. &amp;nbsp;Many times, for instance, to find a book or a movie that we like, it is required to search at several websites and social networks through the web. &amp;nbsp;There isn't a tool that allow this connection between items of many domains in a organized and structured way, even for easy access. Those objects are spread over the web, and the recommendations are placed in social networks by comments, results of machine learning techniques or by queries at several web pages or search engines. &amp;nbsp;The problem becomes worst when we don't know anything about the existence of a certain item, which it could result at never finding out that possible item that would be of our interest.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;The social media is huge and we need tools that performs a deep analysis of all this data, filter out items of my interest, specially from the historical data &amp;nbsp;(with our permission, of course) &amp;nbsp;from our presence in the web and bring items and products relevant to us without loosing the discovery process associated with the serendipity. &amp;nbsp;One of the solutions is a powerful recommender engine fed by this Social Genome.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-VJZOo1xDzGg/TwUKJniTmKI/AAAAAAAAArw/t2qN7YXrvSM/s1600/RecDayRecommenderEngine.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="163" src="http://1.bp.blogspot.com/-VJZOo1xDzGg/TwUKJniTmKI/AAAAAAAAArw/t2qN7YXrvSM/s400/RecDayRecommenderEngine.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Hybrid Social Context-Aware Recommender RecDay&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span id="internal-source-marker_0.6988762901164591"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div dir="ltr" style="margin-bottom: 0pt; margin-top: 0pt;"&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;This hybrid social context-aware recommender (which we call &lt;b&gt;recday&lt;/b&gt;) is a engine composed by several modular components, which we employ a broad range of semantic analysis techniques, including information extraction and integration, natural language processing and machine learning. The main task of this recommender is to analyze information about his posts, bios and relationships/lists collected from the social genome and summarize it (all this data would represent the interests of the user) in profiles, which we could call DNA. Those profiles built by the recday would infer the possible interests of the users and would serve as basis for personalized recommendations of products and services from the retail stores/e-commerce applications.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-family: Georgia; font-size: 15px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;A perfect example for this proccess, which we call the &lt;b&gt;translation&lt;/b&gt;, is when you mentioned several times about Apple products (such as macbook, ipod, iphone, etc) at your tweets. Even you never used the word "Apple", we can use the Social Genome to detect the products and infer that you are interested in Apple products. The following figure illustrate certain kinds of entities and relationships collected in the Social Genome:&lt;br /&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-DS1HR6dVWfg/TwWTM9f2_zI/AAAAAAAAAsI/2bUhRBy1jno/s1600/Screen+shot+2012-01-05+at+9.12.51+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="299" src="http://3.bp.blogspot.com/-DS1HR6dVWfg/TwWTM9f2_zI/AAAAAAAAAsI/2bUhRBy1jno/s400/Screen+shot+2012-01-05+at+9.12.51+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="text-align: center;"&gt;&lt;span style="font-size: x-small;"&gt;The relationships extracted from the social web data&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;The second step of this engine is to build the user profile. Different from another approaches which it only uses the content or the historical data from reviews or ratings from the user, the Recday would go further and would analyze the temporal context included in the interests of the user. Several reports on consumer behavior show that the user desires are influented by external factors and even the humour or feelings of the person at the certain moment. It is required to collect in a stealth way (with the user permission of course) his social data and build his personality defined by several dimensions. Those dimensions represent the current state of the user which may define what kind of suggestions he would like to receive at that particular moment. For example, if you are happy today because you got a new job and posted at your Facebook about that event or even updated your profile about this new position, it would be a valuable information for your DNA profile in order to recommend products and services to celebrate this occasion (You are happy and excited, don't you think ?).&lt;br /&gt;&lt;br /&gt;Another important component in this proccess is the product side. We need extract more information from their products portfolio. Items must be juiced in order to get all its meta-data available. Imagine the movie Batman where we have details about the year, genre, cast, production, direction, sinoypsis, etc. All this data can be used to build the DNA Item and be expressed by a collection of dimensions that represent the item profile. With those profiles (DNA User and DNA Item) we compute the similarity between user and items in order to produce a list of top recommendations and related explanations.&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;img height="266" src="https://docs.google.com/drawings/image?id=sccJBlRR-_Fzn3ehHwjxOdQ&amp;amp;w=695&amp;amp;h=463&amp;amp;rev=184&amp;amp;ac=1" style="margin-left: auto; margin-right: auto;" width="400" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;The Social Architecure of the Recommender&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;b id="internal-source-marker_0.7363749158103019"&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;The final result can be shown in several mediums: mobile apps, widgets, web, API's, pluggins, etc. It is important the usability and how you will present all this information for a particular user. That's why the user interface must be simple and easy to navigate and have mechanisms to collect the user's feedback for the suggestions given by the engine. This proccess is cyclic, so when you give a feedback (a like or dislike or a comment about the suggestion), this piece of information is handled and leveraged to power your DNA profile.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div style="text-align: -webkit-auto;"&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;img height="285" src="https://lh3.googleusercontent.com/yrWzr6EudYkSiEi_oW_tSdzja6QmH60zyFYBP-V_UPZ5XphzOG2zww1fgsLBTd3pSKXZ9ITfwPmWuhhevG-3S_b2uo3hpZ-WVFuFt9Latv-gIrN5bLo" style="margin-left: auto; margin-right: auto;" width="400" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;A particular medium of the recommendations: Ipad Demo&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In order to build all these interesting technical challenges, we needed to start developing our in-house solution called &lt;b&gt;Crab&lt;/b&gt;, which proccesses all this data and employ several analysis and filtering techniques &amp;nbsp;to deal with the percularities of this heterogeneous sources of data. The first start is the launch of the &lt;b&gt;Deepjewel Labs&lt;/b&gt;. Deepjewel is a principle that we can mine the wealth in data, by identifying interesting entities and relationships and converting them into valuable information as input in the recommendation proccess of items and services.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In summary, all those human and computation techniques can be used to perform a deep semantic analysis of web and social data, where the result for a commerce application or retail store is the ability to offer what the users want before they know that they really want in a personalized way. The RecDay &amp;nbsp;would be able to daily offer relevant product and services to their customers without they even know it exists. This is a new way to shop in which you don't have to go find products, service and information; the machine will help them find their way to you.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;To know more about our Deepjewel labs, visit the website (currently in portuguese): &amp;nbsp;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-s9rMIaSEW_A/TwUkiBLb5yI/AAAAAAAAAr8/eDBrWqeCN9A/s1600/deepjewel_logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="140" src="http://3.bp.blogspot.com/-s9rMIaSEW_A/TwUkiBLb5yI/AAAAAAAAAr8/eDBrWqeCN9A/s200/deepjewel_logo.png" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;a href="http://draft.blogger.com/%C2%A0%20http://www.deepjewel.com"&gt;http://www.deepjewel.com&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I hope you enjoyed,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-2279521221936228567?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/2279521221936228567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2012/01/deepjewel-social-media-powering.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2279521221936228567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2279521221936228567'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2012/01/deepjewel-social-media-powering.html' title='Deepjewel -  Social Media powering Recommendations'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-yJ9N6iEZpIQ/TwWYO9LJeeI/AAAAAAAAAsU/OrQSslN29Dk/s72-c/Screen+shot+2012-01-05+at+9.34.19+AM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-6929937174974243984</id><published>2011-12-21T20:23:00.000-08:00</published><updated>2011-12-21T20:23:55.756-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='pyfoursquare'/><category scheme='http://www.blogger.com/atom/ns#' term='mobile'/><category scheme='http://www.blogger.com/atom/ns#' term='foursquare'/><category scheme='http://www.blogger.com/atom/ns#' term='API'/><category scheme='http://www.blogger.com/atom/ns#' term='library'/><category scheme='http://www.blogger.com/atom/ns#' term='web services'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='web'/><category scheme='http://www.blogger.com/atom/ns#' term='social networks'/><category scheme='http://www.blogger.com/atom/ns#' term='database'/><title type='text'>Playing with Foursquare API with Python</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I'd like to share a project that I am developing that it may be useful for anyone who wants to create datasets from mobile location networks. &amp;nbsp;Specifically, I developed a wrapper in Python for accessing the Foursquare API called &lt;a href="http://pypi.python.org/pypi/pyfoursquare/0.0.1"&gt;PyFoursquare&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For anyone who doesn't know what is &lt;b&gt;&lt;a href="http://www.foursquare.com/"&gt;Foursquare&lt;/a&gt;&lt;/b&gt;, it is a popular mobile social-location network with more 10.000.000 of users around the world. The idea is that you can share your current location with your friends and as result discover new places, find where your friends are and even check some tips and recommendations about a place and what to do when you arrive there. It is an amazing project with lots of data available for anyone who wants to develop new apps for connect or mine (data mining) its data!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://staenz.com/wp-content/uploads/2010/09/TodoonFoursquare.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="221" src="http://staenz.com/wp-content/uploads/2010/09/TodoonFoursquare.jpg" width="320" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Foursquare Mobile Application&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;This Python API is one of the results of my master degree project where I proposed a new architecture for mobile recommenders that fetches reviews from social networks to improve the explanation and the quality of the given recommendations. &amp;nbsp;I &amp;nbsp;used this library to collect tips&amp;nbsp;(text reviews)&amp;nbsp;from Foursquare from places at my neighborhood Recife, Brazil. &amp;nbsp;This API was a little messy, so I decided to clean it up, organize and documment it for publish for the open-source community.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;One of advantages of this API is that you can handle each entity from the Foursquare data as Model object. So instead of handling with json dictionaries, I encapsulate the results in the respective models (Venue, Tips, User, etc.) and access its attributes as common object in Python!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I inspired myself at the work of Joshua at &lt;a href="https://github.com/tweepy/tweepy"&gt;Tweepy&lt;/a&gt;, which is a Python library for Twitter. &amp;nbsp;In this version released 0.0.1 I only implemented some API's such as search/venues, &amp;nbsp;venue_details and venue_tips. &amp;nbsp;In future releases I pretend to add more models and support for more API methods available at Foursquare.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;How can you use it at your project ?&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It is simple! Just install it by downloading at the &lt;a href="https://github.com/marcelcaraciolo/foursquare"&gt;Github's home project&lt;/a&gt;, extract the source from the tar.gz and &amp;nbsp;at the directory of the project run the command below:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;blockquote class="tr_bq"&gt;&lt;i&gt;$ python setup.py install&lt;/i&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;or the easier way is to install by the command easy_install:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;blockquote class="tr_bq"&gt;&lt;i&gt;$ easy_install pyfoursquare&lt;/i&gt;&lt;/blockquote&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;After that, you can &amp;nbsp;simple test by running the command below at your Python Shell&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;i&gt;&amp;gt;&amp;gt;&amp;gt; import pyfoursquare&lt;/i&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;Now let's see how you can get started with the PyFoursquare:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;First you need to create an application at Foursquare. The link is &lt;a href="https://foursquare.com/oauth/"&gt;this&lt;/a&gt;. &amp;nbsp;There &amp;nbsp;you can also get further information about the API, another libraries and several applications using the Foursquarw API's. &amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-51uOmmlQmBA/TvKsDWyMSMI/AAAAAAAAArQ/hxIMG1uqevI/s1600/Screen+shot+2011-12-22+at+1.02.55+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="113" src="http://3.bp.blogspot.com/-51uOmmlQmBA/TvKsDWyMSMI/AAAAAAAAArQ/hxIMG1uqevI/s400/Screen+shot+2011-12-22+at+1.02.55+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;The Foursquare Developer's Settings&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;After creating your application, you must get the &lt;b&gt;client_id&lt;/b&gt; and your &lt;b&gt;client_secret&lt;/b&gt;. Those keys will be important to connect the app to the users' accounts. &amp;nbsp;Foursquare uses the secure authentication based on &lt;a href="http://oauth.net/2/"&gt;OAuth2&lt;/a&gt;. &amp;nbsp;In PyFoursquareAPI, you won't need to handle with all steps provided by OAuth2. &amp;nbsp;It already encapsulates all the steps and handshakes between your app and Foursquare servers. \m/&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Below the &amp;nbsp;&lt;a href="https://gist.github.com/1508880"&gt;code&lt;/a&gt; you must write for authenticate an user to connect to your app:&lt;/div&gt;&lt;script src="https://gist.github.com/1508880.js?file=pyfoursquare.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;After the user &amp;nbsp;authorized, you now can instantiate the PyFoursquare API. &amp;nbsp;It will give you access to the Foursquare API methods. &amp;nbsp;I implemented several methods, but feel free to add new ones! Don't forget to submit the final results as pull requests at the project's repository at Github.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In this example I fetched a venue by giving as input the latitude and longitude and querying for the place with the name 'Burburinho'. &amp;nbsp;Burburinho is a popular bar nearby where I work! &lt;br /&gt;&lt;br /&gt;&lt;a href="https://gist.github.com/1508884"&gt;Source code&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1508884.js?file=pyfoursquare.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now you can access the result and access the Venue as a Python Object. All elements of the Venue are represented as attributes of the object Venue at PyFoursquare. The goal is to make easier the life of the developer when he access the Foursquare API by parsing all the JSON (the result) and placing in the correct model for him.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-TWTeou4gNiM/TvKwX22NUsI/AAAAAAAAArY/lhHZGh8Vk0Y/s1600/Screen+shot+2011-12-22+at+1.23.28+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="140" src="http://1.bp.blogspot.com/-TWTeou4gNiM/TvKwX22NUsI/AAAAAAAAArY/lhHZGh8Vk0Y/s640/Screen+shot+2011-12-22+at+1.23.28+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I expect you enjoyed this API. Feel free to use it at your applications or research! &amp;nbsp;I'd like to thank the Foursquare team for expose their data by providing those API's! &amp;nbsp;For data mining researchers instered in mobile location data, it is a mine of gold!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Further information about PyFoursquare, you can find &lt;a href="https://github.com/marcelcaraciolo/foursquare"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Feel free to give sugestions, improvements and comments,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Regards,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-6929937174974243984?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/6929937174974243984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/12/playing-with-foursquare-api-with-python.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6929937174974243984'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6929937174974243984'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/12/playing-with-foursquare-api-with-python.html' title='Playing with Foursquare API with Python'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-51uOmmlQmBA/TvKsDWyMSMI/AAAAAAAAArQ/hxIMG1uqevI/s72-c/Screen+shot+2011-12-22+at+1.02.55+AM.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-8465476301818289486</id><published>2011-12-19T21:34:00.000-08:00</published><updated>2012-01-12T19:39:58.777-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='text mining'/><category scheme='http://www.blogger.com/atom/ns#' term='pyfoursquare'/><category scheme='http://www.blogger.com/atom/ns#' term='foursquare'/><category scheme='http://www.blogger.com/atom/ns#' term='data mining'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='machine learning'/><category scheme='http://www.blogger.com/atom/ns#' term='information filtering'/><category scheme='http://www.blogger.com/atom/ns#' term='tf-idf'/><title type='text'>Machine Learning with Python: Meeting TF-IDF for Text Mining</title><content type='html'>&lt;div style="text-align: justify;"&gt;3Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;This month I was studying about information retrieval and text mining, specially how to convert the textual representation of information into a &lt;b&gt;Vector Space Model (VSM)&lt;/b&gt;. &amp;nbsp;The VSM is an algebraic model representing the importance of a term (tf-idf) or even the absence or presence (&lt;b&gt;Bag of Words&lt;/b&gt;) of it in a document. I'd like to mention the excellent post from the researcher Christian Perone at his blog Pyevolve about &lt;a href="http://pyevolve.sourceforge.net/wordpress/?p=1589"&gt;Machine learning and Text Mining with TF-IDF&lt;/a&gt;, a great post to read.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I decided in this post to be shorter and give some examples using Python . I expect at the end of this post you feel confortamble to use tf-idf at your tasks handling with text mining.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;By the way, I extremely recommend you to check the &lt;a href="http://scikit-learn.org/stable/"&gt;scikit.learn&lt;/a&gt; machine learning toolkit. There is a whole package to work with text classification, including TF-IDF with Python!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;What is TF-IDF ?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;&lt;a href="http://en.wikipedia.org/wiki/Tf%E2%80%93idf"&gt;Term Frequency - Inverse Document Frequency&lt;/a&gt;&lt;/b&gt; is a weighting scheme that is commonly used in information retrieval tasks. The goal is to model each document into a vector space, ignoring the exact ordering of the words in the document while retaining information about the occurrences of each word.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It is composed by two terms: one first computes the normalized Term Frequency, which is the number of times a word appears in a documnet, divided by the total number of words in that document. Then, the second term is the Inverse Document Frequency, which is computed as the logarithm of the number of the documents in the corpus divided by the number of documents where the term ti appears. Or, in symbols:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://www.elusivesnark.com/images/computing-tf-idf-with-couchdb-and/formula1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://www.elusivesnark.com/images/computing-tf-idf-with-couchdb-and/formula1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;and&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://www.elusivesnark.com/images/computing-tf-idf-with-couchdb-and/formula2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://www.elusivesnark.com/images/computing-tf-idf-with-couchdb-and/formula2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;The TF-IDF gives how important is a word to a document in a collection, since it takes in consideration not only the isolated term but also the term within the document collection. The intuition is that a term that occurs frequently in many documents is not a good discriminator ( why emphasize a term which is almost present in the entire corpus of your documents ?) &amp;nbsp;So it will scale down the frequent terms while scaling up the rare terms; for instance, a term that occurs 10 times more than another isn't 10 times more important thant it.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For computing the TF-IDF weights for each document in the corpus, it is required in the corpus a series of steps: &amp;nbsp;1) Tokenize the corpus &amp;nbsp;2) &amp;nbsp;Model the Vector &amp;nbsp;Space &amp;nbsp;and 3) Compute the TF-IDF weight for each document in the corpus.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;Let's going through each step:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Tokenization&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;First we need to tokenize the text. To do this, we can use the &lt;a href="http://www.nltk.org/"&gt;NLTK&lt;/a&gt; library which is a collection of natural language processing algorithms written in Python. The process of tokenizing the documents in the corpous is a two steps: &amp;nbsp;First the text is splint into sentences, and then the sentences are split into the individual words. It is important to notice that there are several words that are not relevant, that is, terms like "the, is, at, on", etc... &amp;nbsp;aren't going to help us, so in the information extraction, we ignore them. Those words are commonly called&lt;b&gt; stop words&lt;/b&gt; and they are present in almost all documents, so it is not relevant for us. In portuguese we also have those stop words such as (a, os , as , os, um , umas, que, etc.)&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;So considering our example below:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-6RKsflsvVn4/TvocvsqmmHI/AAAAAAAAArk/QDeTkhw_9fg/s1600/Screen+shot+2011-12-27+at+4.31.31+PM.png" imageanchor="1"&gt;&lt;img border="0" height="194" src="http://1.bp.blogspot.com/-6RKsflsvVn4/TvocvsqmmHI/AAAAAAAAArk/QDeTkhw_9fg/s640/Screen+shot+2011-12-27+at+4.31.31+PM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;We will tokenize this collection of documents and represent them as vectors (rows) of a matrix with |D| x F shape, where |D| &amp;nbsp;is the cardinality of the document space, or how many documents we have and the F is the number of features, in our example it is represented by the vocabulary size.&lt;br /&gt;&lt;br /&gt;So the matrix representation of our vectors above is:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-PALuMT57UNI/Tw82HdjbTQI/AAAAAAAAAsc/0mgx8V9UjUM/s1600/Screen+shot+2012-01-12+at+4.38.18+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="258" src="http://1.bp.blogspot.com/-PALuMT57UNI/Tw82HdjbTQI/AAAAAAAAAsc/0mgx8V9UjUM/s400/Screen+shot+2012-01-12+at+4.38.18+PM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;As you have noticed, these matrices representing the term frequencies (tf) tend to be very sparse (lots of &amp;nbsp;zero-elements), &amp;nbsp;so you will usually see the representation of these matrices as sparse matrices. The &lt;a href="https://gist.github.com/1602607"&gt;code&lt;/a&gt; shown below will tokenize each document in the corpus and compute the term frequencies.&lt;br /&gt;&lt;script src="https://gist.github.com/1602607.js?file=term_frequency.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div style="text-align: -webkit-auto;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Model the Vector Space&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now that each of the documents in the corpus has been tokenized, the next step is to compute the document frequency quantity, that is, for each term, how many documents that term appears in. Before going to IDF, it is important to normalize the term-frequencies. Why ? &amp;nbsp;Imagine that we have a repeated term in document with porpuse of improving its ranking on an Information Retrieval System or even create a bias torwards long documents, making them look more important than they are just because of the high frequency of the term in the document. By normalizing the TF vector we can overcome this problem.&lt;br /&gt;The &lt;a href="https://gist.github.com/1603237"&gt;code&lt;/a&gt;.&lt;/div&gt;&lt;script src="https://gist.github.com/1603237.js?file=term_frequency_normalization.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: -webkit-auto;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Compute the TF-IDF&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: -webkit-auto;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: -webkit-auto;"&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now that you saw how the vector normalization was applied, we will now have to compute the second term of tf-idf: the inverse document frequency. The code is provided &lt;a href="https://gist.github.com/1604265"&gt;below&lt;/a&gt;:&lt;/div&gt;&lt;script src="https://gist.github.com/1604265.js?file=idf.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The TF-IDF is the product between the TF and IDF. &amp;nbsp;So a high weight of the tf-idf &amp;nbsp;is reached when you have a high term frequency (tf) in the given document and low document frequency of the term in the whole collection. Now let's see the tf-idf computed for each term present in the vector space.&lt;/div&gt;&lt;br /&gt;The &lt;a href="https://gist.github.com/1604356"&gt;code&lt;/a&gt;.&lt;br /&gt;&lt;script src="https://gist.github.com/1604356.js?file=tf_idf.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Putting everything together, the following code will compute the TF-IDF weights for each document. And the result matrix it will be:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://s.wordpress.com/latex.php?latex=%20%20%20M_%7Btf%5Cmbox%7B-%7Didf%7D%20%3D%20M_%7Btrain%7D%20%5Ctimes%20M_%7Bidf%7D%20%20%20&amp;amp;bg=ffffff&amp;amp;fg=000000&amp;amp;s=0" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://s.wordpress.com/latex.php?latex=%20%20%20M_%7Btf%5Cmbox%7B-%7Didf%7D%20%3D%20M_%7Btrain%7D%20%5Ctimes%20M_%7Bidf%7D%20%20%20&amp;amp;bg=ffffff&amp;amp;fg=000000&amp;amp;s=0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;A row of this matrix would be:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-rAEAUIPikVE/Tw-jRpaFxuI/AAAAAAAAAsk/oiTV1nVgtC4/s1600/Screen+shot+2012-01-13+at+12.22.08+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="326" src="http://3.bp.blogspot.com/-rAEAUIPikVE/Tw-jRpaFxuI/AAAAAAAAAsk/oiTV1nVgtC4/s400/Screen+shot+2012-01-13+at+12.22.08+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;I ommited the zero-values elements of the row.&lt;br /&gt;&lt;br /&gt;If we would decide to check the most relevant words for this place, by using the tf-idf I could see that the place has a nice hot chocolate drink (0.420955 &amp;lt;= chocolate quente ótimo), the soft drink nega maluca is also delicious (0.315716 - nega maluca uma delicia), &amp;nbsp;its&amp;nbsp;Cheese bun is also quite good (0.252573 -&amp;nbsp;pao de queijo muito bom).&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;And that is how we comput &amp;nbsp;our&amp;nbsp;&lt;img alt="M_{tf\mbox{-}idf}" class="latex" src="http://s.wordpress.com/latex.php?latex=M_%7Btf%5Cmbox%7B-%7Didf%7D&amp;amp;bg=ffffff&amp;amp;fg=000000&amp;amp;s=0" style="background-color: white; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; color: #555555; font-family: Palatino, 'Palatino Linotype', Georgia, Times, 'Times New Roman', serif; font-size: 15px; line-height: 22px; text-align: -webkit-auto; vertical-align: middle;" title="M_{tf\mbox{-}idf}" /&gt;&amp;nbsp;matrix. &amp;nbsp;You can take a look at this &lt;a href="http://radimrehurek.com/gensim/tutorial.html"&gt;link&lt;/a&gt; and this &lt;a href="http://scikit-learn.sourceforge.net/stable/auto_examples/document_classification_20newsgroups.html#example-document-classification-20newsgroups-py"&gt;one&lt;/a&gt; to know how to use it with GenSim and Scikit.Learn respectively.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;That's all, &amp;nbsp;I hope that &amp;nbsp;you enjoyed this article and help more people to know how to implement the tf-idf weight to mine your collection of texts. &amp;nbsp;Feel free to comment and make suggestions.&lt;br /&gt;&lt;br /&gt;The source code of this example is also &lt;a href="https://gist.github.com/1604487"&gt;available&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Regards,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-8465476301818289486?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/8465476301818289486/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/12/machine-learning-with-python-meeting-tf.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/8465476301818289486'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/8465476301818289486'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/12/machine-learning-with-python-meeting-tf.html' title='Machine Learning with Python: Meeting TF-IDF for Text Mining'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-6RKsflsvVn4/TvocvsqmmHI/AAAAAAAAArk/QDeTkhw_9fg/s72-c/Screen+shot+2011-12-27+at+4.31.31+PM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-5770364820088068788</id><published>2011-12-07T14:42:00.001-08:00</published><updated>2011-12-07T14:55:12.121-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='online'/><category scheme='http://www.blogger.com/atom/ns#' term='class'/><category scheme='http://www.blogger.com/atom/ns#' term='scientifc programming'/><category scheme='http://www.blogger.com/atom/ns#' term='course'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='matplotlib'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><title type='text'>Annoucing a Scientific Computing With Python Course !</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;I am announcing the launch of the website &lt;b&gt;PyCursos&lt;/b&gt;. Pycursos is a online-course and training platform for anyone who wants to learn Python programming language and its related extensions. The first course is being already announced, which is the &lt;b&gt;Scientific Computing Programming with Python&lt;/b&gt;, &amp;nbsp;with me as teacher.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-5-nu7eUKNnM/Tt_qWby1u_I/AAAAAAAAAqs/rJ2pGnXdYN0/s1600/Screen+shot+2011-12-07+at+7.36.52+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="141" src="http://3.bp.blogspot.com/-5-nu7eUKNnM/Tt_qWby1u_I/AAAAAAAAAqs/rJ2pGnXdYN0/s400/Screen+shot+2011-12-07+at+7.36.52+PM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The goal of the course is to teach scientific computing, specially on how to solve scientific problems in your daily routine by using the packages that Python provides for free: &lt;b&gt;Scipy, Numpy and Matplotlib.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;With those tools, the student will learn how to reproduce their problems into a simple and legible code and to use the helpful tools to plot graphs, write reports, mathematics optmization, matrices manipulation, linear algebra and more.&lt;br /&gt;&lt;br /&gt;The requirement to attend the course is only the student be motivated to learn and have some experience with programming. &amp;nbsp;The course will start in 2012, January in on-line mode, where the students will apply and follow a schedule of video-classes on-line and review exercises regularly.&lt;br /&gt;&lt;br /&gt;We have also the option of the in-company training, where the student may watch the classes in a classroom with another students. In both modes the students, at the end of the course, will receive a conclusion certificate. &lt;br /&gt;&lt;br /&gt;It is important to tell that the course now is all &amp;nbsp;in &lt;b&gt;Portuguese&lt;/b&gt;! Sorry for anyone for another countries!&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For further information please visit our website : &lt;a href="http://www.pycursos.com/"&gt;http://www.pycursos.com&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Anyone who whant to know more about scientific computing with python, can check out those&amp;nbsp;&amp;nbsp;&lt;a href="http://www.slideshare.net/marcelcaraciolo/computao-cientfica-com-python-numpy-e-scipy"&gt;slides&lt;/a&gt;&amp;nbsp;of a keynote that I lectured at some institutions from here at Recife, Pernambuco, Brazil.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;center&gt;&lt;div id="__ss_9123741" style="width: 425px;"&gt;&lt;strong style="display: block; margin: 12px 0 4px;"&gt;&lt;a href="http://www.slideshare.net/marcelcaraciolo/computao-cientfica-com-python-numpy-e-scipy" target="_blank" title="Computação Científica com Python, Numpy e Scipy"&gt;Computação Científica com Python, Numpy e Scipy&lt;/a&gt;&lt;/strong&gt; &lt;iframe frameborder="0" height="355" marginheight="0" marginwidth="0" scrolling="no" src="http://www.slideshare.net/slideshow/embed_code/9123741" width="425"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;div style="padding: 5px 0 12px;"&gt;View more &lt;a href="http://www.slideshare.net/" target="_blank"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/marcelcaraciolo" target="_blank"&gt;Marcel Caraciolo&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;Regards,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-5770364820088068788?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/5770364820088068788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/12/annoucing-scientific-computing-with.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/5770364820088068788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/5770364820088068788'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/12/annoucing-scientific-computing-with.html' title='Annoucing a Scientific Computing With Python Course !'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-5-nu7eUKNnM/Tt_qWby1u_I/AAAAAAAAAqs/rJ2pGnXdYN0/s72-c/Screen+shot+2011-12-07+at+7.36.52+PM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-6321977169643259199</id><published>2011-11-26T13:05:00.001-08:00</published><updated>2011-12-01T06:08:22.496-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='matplotlib'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='review'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='book'/><title type='text'>Review of the book Numpy 1.5 - Beginner's Guide</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I'd like to share my review of the book &lt;a href="http://www.packtpub.com/numpy-1-5-using-real-world-examples-beginners-guide/book"&gt;Numpy 1.5 the Beginner's Guide&lt;/a&gt; by Ivan Idris, which is one of the latest books in a series of manuals covering scientific computing libraries written in Python. &amp;nbsp;This book covers the &lt;a href="http://numpy.scipy.org/"&gt;Numpy&lt;/a&gt; library for manipulating vectors and matrices and support for mathematical libraries.&lt;br /&gt;&lt;br /&gt;&lt;table cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Ff5P5WEmQP0/TtF6Pm4XYVI/AAAAAAAAAp0/SfgFk3SfpqY/s1600/Screen+shot+2011-11-26+at+8.46.00+PM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="200" src="http://3.bp.blogspot.com/-Ff5P5WEmQP0/TtF6Pm4XYVI/AAAAAAAAAp0/SfgFk3SfpqY/s200/Screen+shot+2011-11-26+at+8.46.00+PM.png" width="161" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Numpy 1.5 &amp;nbsp;from Packt Publisher&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;Quick Review&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The book is a great and useful resource for anyone who wants to explore further the Numpy scientific library since it covers almost all of the modules available at Numpy 1.5. &amp;nbsp;It comes with several examples, specially for finantial researchers and developers that work with finantial data. The author explored several modules using stocks and historical price data. &amp;nbsp;The authors explains each function or operation with code and the expected results, so the reader can follow precisely what's happening when he presents the modules. One of the values of the book is how it is organized: the step-by-step guide when he presents complex functions at Numpy, for example: add.reduceat, add.accumulate and add.reduce operators.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The part that I didn't like was about the exercises which was quite simple. I'd like to see deep exercises exploring the resources given at the book and I missed more information about NaN values. Also, I didn't &amp;nbsp;see information also about the functions squeeze, choose and about more complex structured arrays (arrays with tuples, etc.).&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;To sum up, I recommend this book for anyone whishing to learn about scientific computing with Python using the mathematical library Numpy which is a great alternative (and free !) for Matlab, Mathematica and other packages. I expect quite soon a book covering Scipy library also! &amp;nbsp;By the way, the finantial fans will love this book since it covers almost of the entire book with examples using finance data!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Review&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;The book starts with a step-by-step installation process of Numpy as also giving a litte introduction about what is Numpy, its history, etc. &amp;nbsp; I'd like to mention that even all the platforms covered at the book, Numpy is not so easy as mentioned to install at Mac OS. &amp;nbsp;The problem is that generally the developers don't use the built-in Python that comes with the Mac, since it is outdated (my Snow leopard comes with the Python 2.6.1). So when you install the new Python, that the problems come! Several compilation errors, messages that you can't understand, etc. &amp;nbsp;But if you go by using the MacPorts, &amp;nbsp;you will free of all these errors! ( After all the nightmare of the installation, I discovered the MacPorts :P).&lt;br /&gt;&lt;br /&gt;The following chapters 2-4 presents the Numpy Fundamentals covering the array manipulations and most commonly used operations. &amp;nbsp;The books goes into a cyclic process, where each function that the author presents goes through an introduction about the problem to solve, the actions (how you with Numpy can solve), auxiliar numpy functions and operations and finally what just happened, that is, explain what he has done after showing the solution. The examples covered at book, most of them, are from finantial data and stock market values. An interesting choice since he used the same examples through the chapters in a progression and logical way. &amp;nbsp;Having each function and numpy featured described and explained made the book a good reference guide for someone using the library. &amp;nbsp;There were minor issues &amp;nbsp;related to the imports, he doesn't mention the imports in some examples, &amp;nbsp;for instance the numpy.loadtxt function when he uses the datetime module. &amp;nbsp;For a beginner that is studying Python for the first time, it may be harder to them to follow the examples, since he could not always tell where the functions or modules were coming from.&lt;br /&gt;&lt;br /&gt;The second part of the book includes the matrices, universal functions, some scipy modules and the use of matplotlib and testing. &amp;nbsp;The chapter 5 covers the matrix module and universal functions such as add, divide, prod, sum and so on. &amp;nbsp;I missed some functions that weren't covered at this chapter such as numpy.choose or numpy.squeeze. &amp;nbsp;I believe the author didn't remember or didn't have space to mention these specific functions, but it does not prejudice at all the quality of the book. The chapter that I liked the most at the book was about testing. Several developers, special the scientific researchers are not used to test their code, so I believe it is a great chapter for anyone who wants to assure quality and avoid future bugs using Numpy testing modules. &amp;nbsp;The chapter should be more bigger and include more examples even creating test cases and tips for scientific developers.&lt;br /&gt;Finally the last two chapters focus on plotting and Scipy integration. I think the plotting chapter should be at the beginning of the book, because he already uses lots of examples at the previous chapters with matplotlib and only at the end explain further about the library. The chapter is well-written and gives you sufficient content for beginning with Matplotlib. The last chapter covers the use of several scipy functions but it does not give deeper explanations about how it works as he did at the previous chapters with Numpy. However it gives several useful examples to work with integration, image processing and even optimization. Many developers will enjoy this extra-chapter covering the use of scipy+numpy. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&amp;nbsp;My overall impression of this book is that it could make a useful reference guide for Numpy. For finantial researchers and developers it will be a great book since it also covers lots of examples using finance data to present the numpy fundamentals. &amp;nbsp;There were minor issues related to Scipy and Matplotlib that should be more explained. For anyone who wants to start using Numpy it can a be an excellent book to begin, since it covers all the fundamentals steps with a cyclic progressive introduction of using the scientific packages in Python.&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;&lt;br /&gt;Marcel Caraciolo&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-6321977169643259199?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/6321977169643259199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/11/review-of-book-numpy-15-beginners-guide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6321977169643259199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6321977169643259199'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/11/review-of-book-numpy-15-beginners-guide.html' title='Review of the book Numpy 1.5 - Beginner&apos;s Guide'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-Ff5P5WEmQP0/TtF6Pm4XYVI/AAAAAAAAAp0/SfgFk3SfpqY/s72-c/Screen+shot+2011-11-26+at+8.46.00+PM.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-2423547293296938196</id><published>2011-11-06T22:14:00.000-08:00</published><updated>2011-11-14T17:43:46.630-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='regularization'/><category scheme='http://www.blogger.com/atom/ns#' term='linear regression'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='examples'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='machine learning'/><category scheme='http://www.blogger.com/atom/ns#' term='logistic regression'/><title type='text'>Machine Learning with Python - Logistic Regression</title><content type='html'>&lt;div style="text-align: justify;"&gt;&amp;nbsp;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;I decided to start a new series of posts now focusing on general machine learning with several snippets for anyone to use with real problems or real datasets. &amp;nbsp;Since I am studying machine learning again with a great&amp;nbsp;&lt;a href="http://www.ml-class.org/course/class/index" style="color: #0071bb; text-decoration: none;"&gt;course online&lt;/a&gt;&amp;nbsp;offered this semester by Stanford University, one of &amp;nbsp;the best ways to review the content learned is to write some notes about what I learned. The best part is that it will include examples with Python, Numpy and Scipy. I expect you enjoy all those posts!&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;The series:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;a href="http://aimotion.blogspot.com/2011/10/machine-learning-with-python-linear.html"&gt;Linear Regression&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;In this post I will cover the Logistic Regression and Regularization.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Logistic Regression&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Logistic Regression is a type of regression that predicts the probability of ocurrence of an event by fitting data to a logit function (logistic function). &amp;nbsp;Like many forms of regression analysis, it makes use of several predictor variables that may be either numerical or categorical. For instance, the probability that a person has a heart attack within a specified time period might be predicted from knowledege of the person's age, sex and body mass index. This regression is quite used in several scenarios such as prediction of customer's propensity to purchase a pro&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;duct or cease a subscription in marketing applications and many others.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Visualizing the Data&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;Let's explain the logistic regression by example. Consider you are the administrator of a university department and you want to determine each applicant's chance of admission based on their results on two exams. You have the historical data from previous applicants that you can use as a trainning set for logistic regression. &amp;nbsp;For each training example, you have the applicant's scores on two exams and the admissions decision. &amp;nbsp; We will use logistic regression to build this model that estimates the probability of admission based the scores from those two exams.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Let's first visualize our data on a 2-dimensional plot as show below. As you can see the axes are the two exam scores, and the positive and negative examples are shown with different markers.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-rA1GyvHTJic/TraYYS200-I/AAAAAAAAAoY/6DLCNuy41qg/s1600/plo1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="301" src="http://3.bp.blogspot.com/-rA1GyvHTJic/TraYYS200-I/AAAAAAAAAoY/6DLCNuy41qg/s400/plo1.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Sample training visualization&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;script src="https://gist.github.com/1342942.js?file=log_regression.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The &lt;a href="https://gist.github.com/1342942"&gt;code&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Costing Function and Gradient&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The logistic regression hypothesis is defined as:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-c-C6IGeS_eo/TraZSGODaCI/AAAAAAAAAog/lspXBBwj7FE/s1600/Screen+shot+2011-11-06+at+11.26.53+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-c-C6IGeS_eo/TraZSGODaCI/AAAAAAAAAog/lspXBBwj7FE/s1600/Screen+shot+2011-11-06+at+11.26.53+AM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;where the function g &amp;nbsp;is the sigmoid function. It is defined as:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-PqGvtE_NEmE/TraZYTHEfLI/AAAAAAAAAoo/swDZg20vd4Q/s1600/Screen+shot+2011-11-06+at+11.27.03+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-PqGvtE_NEmE/TraZYTHEfLI/AAAAAAAAAoo/swDZg20vd4Q/s1600/Screen+shot+2011-11-06+at+11.27.03+AM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The sigmoid function has special properties that can result values in the range [0,1]. &amp;nbsp;So you have large positive values of X, the sigmoid should be close to 1, while for large negative values, &amp;nbsp;the sigmoid should be close to 0.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_Tndn7IbKcao/Syu0vkRlGtI/AAAAAAAAAIk/TQ-K2fOr9w0/s400/SigmoidPlot1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="203" src="http://1.bp.blogspot.com/_Tndn7IbKcao/Syu0vkRlGtI/AAAAAAAAAIk/TQ-K2fOr9w0/s320/SigmoidPlot1.png" width="320" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Sigmoid Logistic Function&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The cost function and gradient for logistic regression is given as below:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-0vWgkEmE-u4/TraaI_rd-bI/AAAAAAAAAow/Ya5rp0rQS48/s1600/Screen+shot+2011-11-06+at+11.30.37+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="56" src="http://4.bp.blogspot.com/-0vWgkEmE-u4/TraaI_rd-bI/AAAAAAAAAow/Ya5rp0rQS48/s400/Screen+shot+2011-11-06+at+11.30.37+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;and the gradient of the cost is a vector theta where the j element is defined as follows:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-jpwtW1KQIoE/TraaRvy_8MI/AAAAAAAAAo4/9qnO3SyiqaA/s1600/Screen+shot+2011-11-06+at+11.30.41+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="64" src="http://2.bp.blogspot.com/-jpwtW1KQIoE/TraaRvy_8MI/AAAAAAAAAo4/9qnO3SyiqaA/s320/Screen+shot+2011-11-06+at+11.30.41+AM.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;You may note that the gradient is quite similar to the linear regression gradient, the difference is actually because linear and logistic regression have different definitions of h(x).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;Let's see the &lt;a href="https://gist.github.com/1365841"&gt;code&lt;/a&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 18px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 20px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1365841.js?file=log_regression.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Now to find the minimum of this cost function, we will use a scipy built-in function called &lt;a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin_bfgs.html"&gt;fmin_bfgs&lt;/a&gt;. &amp;nbsp;It will find the best parameters theta for the logistic regression cost function given a fixed dataset (of X and Y values).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The parameters are:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The initial values of the parameters you are trying to optimize;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;A function that, when given the training set and a particular theta, computes the logistic regression cost and gradient with respect to theta for the dataset (X,y).&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The final theta value will then be used to plot the decision boundary on the training data, resulting in a figure similar to the figure below.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-eCQJ4j3wqw0/Tr8-bwNir1I/AAAAAAAAApg/mBLTGATfgI0/s1600/plot3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://2.bp.blogspot.com/-eCQJ4j3wqw0/Tr8-bwNir1I/AAAAAAAAApg/mBLTGATfgI0/s320/plot3.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Evaluating logistic regression&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Now that you learned the parameters of the model, you can use the model to predict whether a particular student will be admited. For a student with an Exam1 score of 45 and an Exam 2 score of 85, you should see an admission probability of 0.776.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;But you can go further, and evaluate the quality of the parameters that we have found and see how well the learned model predicts on our training set. &amp;nbsp;If we consider the threshold of 0.5 using our sigmoid logistic function, we can consider that:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://www.mblondel.org/tlml/_images/math/9dd37d56c18555e80d91e8f57a1ceeb83fc72a5a.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://www.mblondel.org/tlml/_images/math/9dd37d56c18555e80d91e8f57a1ceeb83fc72a5a.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Where 1 represents admited and -1 not admited.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Going to the code and calculate the training accuracy of our classifier we can evaluate the percentage of examples it got correct. &amp;nbsp;&lt;a href="https://gist.github.com/1362087"&gt;Source code&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1362087.js?file=prediction.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;89% , not bad hun?!&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Regularized logistic regression&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;But when your data can not be separated into positive and negative examples by a straight-line trought the plot ? &amp;nbsp;Since our logistic regression will be only be able to find a linear decision boundary, we will have to fit the data in a better way. Let's go through an example.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Suppose you are the product manager of the factory and you have the test results for some microships &amp;nbsp;of two different tests. From these two tests you would like to determine whether the microships should be accepted or rejected. &amp;nbsp;We have a dataset of test results on past microships, from which we can build a logistic regression model. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Visualizing the data&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Let's visualize our data. As you can see in the figure below, the axes are the two test scores, and the positive (y = 1, accepted) and negative (y = 0, rejected) examples are shown with different markers.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-jbXb3vs96NU/Trdz-jCkRNI/AAAAAAAAApA/3H_PdT1Wj0A/s1600/plot2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="262" src="http://2.bp.blogspot.com/-jbXb3vs96NU/Trdz-jCkRNI/AAAAAAAAApA/3H_PdT1Wj0A/s400/plot2.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Microship training set&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;You may see that the model built for this task may predict perfectly all training data and sometimes it migh cause some troubling cases. &amp;nbsp;Just because ithe model can perfectly reconstruct the training set does not mean that it had everything figured out. &amp;nbsp;This is known as overfitting. &amp;nbsp; You can imagine that if you &amp;nbsp;were relying on this model to make important decisions, it would be desirable to have at least of regularization in there. Regularization is a powerful strategy to combat the overfitting problem. We will see it in action at the next sections.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Feature mapping&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;One way to fit the data better is to create more features from each data point. We will map the features &amp;nbsp;into all polynomial terms of x1 tand x2 up to the sixth power.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-tr3p_3aMMDk/Trd0mLbo8fI/AAAAAAAAApI/Vd2TB9MQboE/s1600/Screen+shot+2011-11-07+at+3.02.01+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-tr3p_3aMMDk/Trd0mLbo8fI/AAAAAAAAApI/Vd2TB9MQboE/s1600/Screen+shot+2011-11-07+at+3.02.01+AM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;As a result of this mapping, our vector of two features (the scores on two QA tests) has been transformed into a 28-dimmensional vector. A logistic regression classifier trained on this higher dimension feature vector &amp;nbsp;will have a more complex decision boundary and will appear nonlinear when drawn in our 2D plot.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;Although the feature mapping allows us to buid a more expressive classifier, it also me susceptible to overfitting. That comes the regularized logistic regression to fit the data and avoid the overfitting problem.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 21px;"&gt;&lt;a href="https://gist.github.com/1365704"&gt;Source code.&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 20px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1365704.js?file=mapfeature.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div&gt;&lt;b style="font-family: arial, 'times New Roman', tahoma; font-size: large; line-height: 21px;"&gt;Cost function and gradient&lt;/b&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium; line-height: 22px;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The regularized cost function in logistic regression is :&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-qNym-oCdMIg/Trd03YeslWI/AAAAAAAAApQ/GUfXiJ3vpUE/s1600/Screen+shot+2011-11-07+at+3.03.55+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="48" src="http://3.bp.blogspot.com/-qNym-oCdMIg/Trd03YeslWI/AAAAAAAAApQ/GUfXiJ3vpUE/s400/Screen+shot+2011-11-07+at+3.03.55+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Note that you should not regularize the parameter theta, so the final summation is for j = 1 to n, not j= 0 to n. &amp;nbsp;The gradient of the cost function is a vector where the jn element is defined as follows:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-VEpIvg197n0/Trd1EC-G2eI/AAAAAAAAApY/o68ntJrD0ig/s1600/Screen+shot+2011-11-07+at+3.04.49+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="107" src="http://3.bp.blogspot.com/-VEpIvg197n0/Trd1EC-G2eI/AAAAAAAAApY/o68ntJrD0ig/s400/Screen+shot+2011-11-07+at+3.04.49+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Now let's learn the optimal parameters theta. &amp;nbsp;Considering now those new functions and our last numpy optimization function we will be able to learn the parameters theta.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;The all code now provided (&lt;a href="https://gist.github.com/1365847"&gt;code&lt;/a&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1365847.js?file=logistic_reg.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Plotting the decision boundary&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Let's visualize the model learned by the classifier. The plot will display the non-linear decision boundary that separates the positive and negative examples.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-twaJvC29wNE/TsGxfg8n60I/AAAAAAAAApo/JgWINQ1EVn0/s1600/plot4.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="299" src="http://4.bp.blogspot.com/-twaJvC29wNE/TsGxfg8n60I/AAAAAAAAApo/JgWINQ1EVn0/s400/plot4.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Decision Boundary&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 21px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;As you can see our model succesfully predicted our data with accuracy of 83.05%.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;a href="https://gist.github.com/1365849"&gt;Code&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1365849.js?file=decision_boundary.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="line-height: 21px;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Scikit-learn&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Scikit-learn is an amazing tool for machine learning providing several modules for working with classification, regression and clustering problems. It uses python, numpy and scipy and it is open-source!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;If you want to use logistic regression and linear regression you should take consider the scikit-learn. It has several examples and several types of regularization strategies to work with. &amp;nbsp;Take a look at this &lt;a href="http://scikit-learn.sourceforge.net/modules/linear_model.html"&gt;link&lt;/a&gt; and see by yourself! &amp;nbsp;I recommend!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Logistic regression has several advantages over linear regression, one specially it is&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&amp;nbsp;more robust and does not assume linear relationship since it may handle nonlinear effects. However it requires much more data to achieve stable, meaningful results. &amp;nbsp;There are another machine learning techniques to handle with non-linear problems and we will see in the next posts. &amp;nbsp; I hope you enjoyed this article!&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;All source from this article &lt;a href="http://dl.dropbox.com/u/1977573/logistic.zip"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Regards,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Marcel Caraciolo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: #003300; font-family: Geneva, Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; line-height: 22px;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-2423547293296938196?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/2423547293296938196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/11/machine-learning-with-python-logistic.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2423547293296938196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2423547293296938196'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/11/machine-learning-with-python-logistic.html' title='Machine Learning with Python - Logistic Regression'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-rA1GyvHTJic/TraYYS200-I/AAAAAAAAAoY/6DLCNuy41qg/s72-c/plo1.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-5954150033693505172</id><published>2011-11-04T22:23:00.000-07:00</published><updated>2011-11-04T22:23:36.820-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='challenge'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='artificial intelligence'/><category scheme='http://www.blogger.com/atom/ns#' term='machine learning'/><category scheme='http://www.blogger.com/atom/ns#' term='game'/><title type='text'>Google AI Challenge this year is open! Ants Battlefield!</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;One more time Google and the University of Waterloo's computer science club have launched an &lt;a href="http://aichallenge.org/"&gt;Artificial Intelligence challenge&lt;/a&gt;. This year the task is to write a program to compete in the Ants Multiplayer Challenge. The goal is seek and destroy the most enemy ant hills while defending their own hills. &amp;nbsp;You must create a bot that plays the game of Ants as intelligently as possible. &amp;nbsp;The contest supports languages in Python, Java, C# and C++.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The current state of the contest, which you can submit your project, is until December 18th. After, there will be a final tournament between the contestants to decide the ultimate winner!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;See &amp;nbsp;the game in action at the&lt;a href="http://www.youtube.com/watch?v=N9PMN0nBx8Y"&gt; video below&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;center&gt;&lt;iframe allowfullscreen="" frameborder="0" height="315" src="http://www.youtube.com/embed/N9PMN0nBx8Y" width="560"&gt;&lt;/iframe&gt;&lt;/center&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It is a great opportunity to learn Artificial Intelligence and play with your skills at programming, machine learning and logical reason!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Regards,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo &amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-5954150033693505172?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/5954150033693505172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/11/google-ai-challenge-this-year-is-open.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/5954150033693505172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/5954150033693505172'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/11/google-ai-challenge-this-year-is-open.html' title='Google AI Challenge this year is open! Ants Battlefield!'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://img.youtube.com/vi/N9PMN0nBx8Y/default.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-7387615980281248256</id><published>2011-10-27T21:02:00.000-07:00</published><updated>2011-10-27T21:05:59.680-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='linear regression'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='examples'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='machine learning'/><title type='text'>Machine Learning with Python -  Linear Regression</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I decided to start a new series of posts now focusing on general machine learning with several snippets for anyone to use with real problems or real datasets. &amp;nbsp;Since I am studying machine learning again with a great &lt;a href="http://www.ml-class.org/course/class/index"&gt;course online&lt;/a&gt; offered this semester by Stanford University, one of &amp;nbsp;the best ways to review the content learned is to write some notes about what I learned. The best part is that it will include examples with Python, Numpy and Scipy. I expect you enjoy all those posts!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Linear Regression&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In this post I will implement the linear regression and get to see it work on data. &amp;nbsp;Linear Regression is the oldest and most widely used predictive model in the field of machine learning. The goal is to &amp;nbsp;minimize the sum of the squared errros to fit a straight line to a set of data points. &amp;nbsp;(You can find further information at &lt;a href="http://en.wikipedia.org/wiki/Linear_regression"&gt;Wikipedia&lt;/a&gt;).&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The linear regression model fits a linear function to a set of data points. The form of the function is:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif; font-size: 14px;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;i&gt;Y&lt;/i&gt;&amp;nbsp;=&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;0&lt;/sub&gt;&amp;nbsp;+&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;*&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&amp;nbsp;+&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;*&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;&amp;nbsp;+ … +&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;*&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;sub&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/sub&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span"&gt;Where&amp;nbsp;&lt;/span&gt;&lt;i&gt;Y&lt;/i&gt;&lt;span class="Apple-style-span"&gt;&amp;nbsp;is the target variable, &amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;,&amp;nbsp;&lt;/span&gt;&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;span class="Apple-style-span"&gt;, ...&amp;nbsp;&lt;/span&gt;&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&amp;nbsp;&lt;/i&gt;&lt;/sub&gt;are the predictor variables and&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif; font-size: 14px;"&gt;&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;,&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;, …&amp;nbsp;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&amp;nbsp;&lt;/i&gt;&lt;/sub&gt;&lt;/span&gt;are the coefficients that multiply the predictor variables. &amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif; font-size: 14px;"&gt;&lt;i&gt;β&lt;/i&gt;&lt;sub&gt;0&amp;nbsp;&lt;/sub&gt;&lt;/span&gt;is constant.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For example, suppose you are the CEO of a big company of shoes franchise and are considering different cities for opening a new store. The chain already has stores in various cities and you have data for profits and populations from the cities. &amp;nbsp;You would like to use this data to help you select which city to expand next. You could use linear regression for evaluating the parameters of a function that predicts profits for the new store.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The final function would be:&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y = &amp;nbsp; -3.63029144 &amp;nbsp;+ 1.16636235 *&amp;nbsp;&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;There are two main approaches for linear regression: with one variable and with multiple variables. Let's see both!&lt;/div&gt;&lt;div style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;b&gt;Linear regression with one variable&lt;/b&gt;&lt;br /&gt;&lt;div style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-size: 14px;"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;Considering our last example, we have a file that contains the dataset of our linear regression problem. The first column is the population of the city and the second column is the profit of having a store in that city. A negative value for profit indicates a loss.&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;Before starting, it is useful to understand the data by visualizing it. &amp;nbsp;We will use the scatter plot to visualize the data, since it has only two properties to plot (profit and population). Many other problems in real life are multi-dimensional and can't be plotted on 2-d plot.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1321551.js?file=linregr.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;If you run this code above (you must have the Matplotlib package installed in order to present the plots), you will see the scatter plot of the data as shown at Figure 1.&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;a href="http://3.bp.blogspot.com/-nZcik2kHLZM/TqofdW2e6cI/AAAAAAAAAnE/Yd2xSKNErMU/s1600/plot1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="301" src="http://3.bp.blogspot.com/-nZcik2kHLZM/TqofdW2e6cI/AAAAAAAAAnE/Yd2xSKNErMU/s400/plot1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;Now you must fit the linear regression parameters to our dataset using gradient descent.&amp;nbsp;The objective of linear regression is to minimize the cost function:&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://nimbledais.com/wp-content/uploads/2011/09/image8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://nimbledais.com/wp-content/uploads/2011/09/image8.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;where the hypothesis&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: serif; font-size: 16px; white-space: nowrap;"&gt;&lt;span class="mi1" style="font-family: serif; font-style: italic; padding-left: 2px; vertical-align: middle;"&gt;H&lt;/span&gt;&lt;span class="msub" style="font-size: 13px; padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px; vertical-align: -1ex;"&gt;&lt;span class="mn" style="font-family: serif; vertical-align: middle;"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;is given by the linear model:&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-RUSO0gsCP-0/Tqoh_c-w4cI/AAAAAAAAAnM/Hfs1HX8m7nw/s1600/Screen+shot+2011-10-28+at+12.35.50+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-RUSO0gsCP-0/Tqoh_c-w4cI/AAAAAAAAAnM/Hfs1HX8m7nw/s1600/Screen+shot+2011-10-28+at+12.35.50+AM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;The parameters of your model are the&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;&amp;nbsp;values. These are the values you will adjust to minimize cost J(&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;). One way to do it is to use the batch gradient descent algorithm. In batch gradient, each iteration performs the update:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-SNPgegm1CQ8/TqoiWVjDS3I/AAAAAAAAAnU/48lPu9db1ek/s1600/Screen+shot+2011-10-28+at+12.37.22+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-SNPgegm1CQ8/TqoiWVjDS3I/AAAAAAAAAnU/48lPu9db1ek/s1600/Screen+shot+2011-10-28+at+12.37.22+AM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"&gt;With each step of gradient &amp;nbsp;descent, your parameters&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;&lt;span class="Apple-style-span"&gt;, come close to the optimal values that will achieve the lowest cost J(&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;).&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"&gt;For our initial inputs we start with our initial fitting parameters&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;&lt;span class="Apple-style-span"&gt;, our data and add another dimmension to our data &amp;nbsp;to accommodate the&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θo&lt;/span&gt;&lt;span class="Apple-style-span"&gt;&amp;nbsp;intercept term. As also our learning rate alpha to 0.01.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1321566.js?file=linregr.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"&gt;As you perform gradient descent to learn minimize the cost function J(&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;&lt;span class="Apple-style-span"&gt;), it is helpful to monitor the convergence by computing the cost. The function cost is show below:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1321569.js?file=linregr.py"&gt;&lt;/script&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;A good way to verify that gradient descent is working correctly is to look at the value of J(&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;) and check that it is decreasing with each step. It should converge to a steady valeu by the end of the algorithm.&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"&gt;Your final values for&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;&lt;span class="Apple-style-span"&gt;&amp;nbsp;will be used to make predictions on profits in areas of 35.000 and 70.000 people. &amp;nbsp;For that we will use some matrix algebra functions with the packages &lt;a href="http://www.scipy.org/"&gt;Scipy&lt;/a&gt; and Numpy, &amp;nbsp;powerful Python packages for scientific computing.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;Our final values as shown below:&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y = &amp;nbsp; -3.63029144 &amp;nbsp;+ 1.16636235 *&amp;nbsp;&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;Now &amp;nbsp;you can use this function to predict your profits! &amp;nbsp;If you use this function with our data we will come with plot:&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-RUQ_r8YMX50/TqokhTUfPkI/AAAAAAAAAnc/1XON9qf6zTM/s1600/plot2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="301" src="http://1.bp.blogspot.com/-RUQ_r8YMX50/TqokhTUfPkI/AAAAAAAAAnc/1XON9qf6zTM/s400/plot2.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;Another interesting plot is the contour plots, it will give you how J(&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ&lt;/span&gt;) varies with changes in&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θo&lt;/span&gt;&amp;nbsp;and &amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: MathJax_Math; font-size: 17px; font-style: italic; white-space: nowrap;"&gt;θ1&lt;/span&gt;.&amp;nbsp;&amp;nbsp;The cost function J(θ) is bowl-shaped and has a global mininum as you can see in the figure below.&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-SL7x0MslJak/Tqokv0texUI/AAAAAAAAAnk/tqJ-_jt465s/s1600/plot3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="301" src="http://1.bp.blogspot.com/-SL7x0MslJak/Tqokv0texUI/AAAAAAAAAnk/tqJ-_jt465s/s400/plot3.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;div style="text-align: justify;"&gt;This minimum is the optimal point for θo&lt;span class="s1"&gt;&amp;nbsp;&lt;/span&gt;and θi, and each step of gradient descent moves closer to this point.&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;All the code is shown &lt;a href="https://gist.github.com/1321575"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;b&gt;Linear regression with multiple variables&lt;/b&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Ok, but when you have multiple variables ? How do we work with them using linear regression ? That comes the linear regression with multiple variables. Let's see an example:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;Suppose you are selling your house and you want to know what a good market price would be. One way to do this is to first collect information on recent houses sold and make a model of housing prices.&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;span class="Apple-style-span"&gt;Our training set of housing prices in Recife, Pernambuco, Brazil are formed by three columns &amp;nbsp;(three variables). T&lt;/span&gt;he first column is the size of the house (in square feet), the second column is the number of bedrooms, and the third column is the price of the house.&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;But before going directly to the linear regression it is important to analyze our data. By looking at the values, note that house sizes are about 1000 times the number of bedrooms. When features differ by orders of magnitude, it is important to perfom a feature scaling that can make gradient descent converge much more quickly.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-NXE0EdhQf3E/TqolmYQTAQI/AAAAAAAAAns/D84TZmf3O1c/s1600/Screen+shot+2011-10-28+at+12.51.04+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="235" src="http://1.bp.blogspot.com/-NXE0EdhQf3E/TqolmYQTAQI/AAAAAAAAAns/D84TZmf3O1c/s400/Screen+shot+2011-10-28+at+12.51.04+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;The basic steps are:&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;ul&gt;&lt;li style="text-align: justify;"&gt;Subtract the mean value of each feature from the dataset.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;After subtracting the mean, additionally scale (divide) the feature values by their respective “standard deviations.”&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;The standard deviation is a way of measuring how much variation there is in the range of values of a particular feature (most data points will lie within ±2 standard deviations of the mean); this is an alternative to taking the range of values (max-min).&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;Now that you have your data scaled, you can implement the gradient descent and the cost function.&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;Previously, you implemented gradient descent on a univariate regression problem. The only difference now is that there is one more feature in the matrix X. The hypothesis function and the batch gradient descent update rule remain unchanged.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1321582.js?file=multlin.py"&gt;&lt;/script&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;In the multivariate case, the cost function can also be written in the following vectorized form:&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: serif; font-size: 14px; white-space: nowrap;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 13.095em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.265em 1000em 3.661em -0.479em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.786em; vertical-align: 0px;"&gt;&lt;span class="mrow" id="MathJax-Span-39" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-40" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; J&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-41" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-42" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;θ&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-43" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;)&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-44" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.278em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;=&lt;/span&gt;&lt;span class="mfrac" id="MathJax-Span-45" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.398em; padding-right: 0.12em; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 1.549em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.941em 1000em 2.964em -0.453em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 50%; line-height: normal; margin-bottom: 0px; margin-left: -0.268em; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.462em; vertical-align: 0px;"&gt;&lt;span class="mn" id="MathJax-Span-46" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;1&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.941em 1000em 2.975em -0.487em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 50%; line-height: normal; margin-bottom: 0px; margin-left: -0.714em; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.1em; vertical-align: 0px;"&gt;&lt;span class="mrow" id="MathJax-Span-47" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span class="mn" id="MathJax-Span-48" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;2&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-49" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(2.533em 1000em 2.964em -0.536em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.006em; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-color: initial; border-left-width: 1.549em; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0.074em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0em; width: 0px;"&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-50" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.167em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-51" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;X&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-52" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;θ&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-53" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;−&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-54" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;y&lt;/span&gt;&lt;span class="texatom" id="MathJax-Span-55" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span class="mrow" id="MathJax-Span-56" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span class="msubsup" id="MathJax-Span-57" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 1.043em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.857em 1000em 3.214em -0.481em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.786em; vertical-align: 0px;"&gt;&lt;span class="mo" id="MathJax-Span-58" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;)&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0.492em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.263em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-59" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 12px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;T&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-60" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-61" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;X&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-62" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;θ&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-63" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;−&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-64" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;y&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-65" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-color: initial; border-left-width: 0em; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 14px; font-style: inherit; height: 2.447em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: -0.836em; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;After running our code, it will come with following function:&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;215810.61679138, &amp;nbsp; 61446.18781361,&amp;nbsp;&amp;nbsp; 20070.13313796&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div class="p1" style="text-align: justify;"&gt;The gradient descent will run until convergence to find the final values of θ. &amp;nbsp;Next, we will this value of θ to predict the price of a house with 1650 square feet and 3 bedrooms.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #333333; font-family: serif; font-size: 14px; white-space: nowrap;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 11.071em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.265em 1000em 3.661em -0.501em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.786em; vertical-align: 0px;"&gt;&lt;span class="mrow" id="MathJax-Span-9" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-10" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; θ&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-11" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.278em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;:=&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-12" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.278em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;θ&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-13" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;−&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-14" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;α&lt;/span&gt;&lt;span class="mfrac" id="MathJax-Span-15" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.287em; padding-right: 0.12em; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 1.013em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.941em 1000em 2.964em -0.453em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 50%; line-height: normal; margin-bottom: 0px; margin-left: -0.268em; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.462em; vertical-align: 0px;"&gt;&lt;span class="mn" id="MathJax-Span-16" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;1&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(2.165em 1000em 2.975em -0.515em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 50%; line-height: normal; margin-bottom: 0px; margin-left: -0.446em; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.1em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-17" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;m&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(2.533em 1000em 2.964em -0.536em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.006em; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-color: initial; border-left-width: 1.013em; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0.074em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0em; width: 0px;"&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msubsup" id="MathJax-Span-18" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.167em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 1.221em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(2.164em 1000em 2.975em -0.501em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.786em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-19" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;x&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0.67em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.199em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-20" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 12px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;T&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-21" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-22" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;x&lt;/span&gt;&lt;span class="msubsup" id="MathJax-Span-23" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 0px; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; text-decoration: none; vertical-align: 0px; width: 1.102em;"&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; clip: rect(1.902em 1000em 2.974em -0.501em); display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -2.786em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-24" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;θ&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; left: 0.551em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: absolute; text-decoration: none; top: -3.218em; vertical-align: 0px;"&gt;&lt;span class="mi" id="MathJax-Span-25" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 12px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;T&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-26" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;−&lt;/span&gt;&lt;span class="mi" id="MathJax-Span-27" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Math; font-size: 17px; font-style: italic; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0.222em; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;y&lt;/span&gt;&lt;span class="mo" id="MathJax-Span-28" style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline; font-family: MathJax_Main; font-size: 17px; font-style: inherit; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 17px; font-style: inherit; height: 2.786em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: 0px; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="border-bottom-width: 0px; border-color: initial; border-color: initial; border-left-color: initial; border-left-width: 0em; border-right-width: 0px; border-style: initial; border-style: initial; border-top-width: 0px; display: inline-block; font-family: MathJax_Main, MathJax_Size1, MathJax_AMS; font-size: 14px; font-style: inherit; height: 2.447em; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: -0.836em; width: 0px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Predicted price of a 1650 sq-ft, 3 br house: &lt;/b&gt;183865.197988&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;If you plot the convergence plot of the gradient descent you may see that convergence will decrease as the number of iterations grows.&lt;/div&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-vX3So9OwSBI/Tqom6PGL0iI/AAAAAAAAAn0/wKT7551fymo/s1600/plot4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="301" src="http://3.bp.blogspot.com/-vX3So9OwSBI/Tqom6PGL0iI/AAAAAAAAAn0/wKT7551fymo/s400/plot4.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;The code for linear regression with multi variables is available &lt;a href="https://gist.github.com/1321585"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-size: 14px;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;b&gt;Extra Notes&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The Scipy package comes with several tools for helping you in this task, even with a module that has a linear regression implemented for you to use!&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The module is &lt;a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html"&gt;scipy.stats.linregress&lt;/a&gt; &amp;nbsp;and implements several other techniques for updating the theta parameters. &amp;nbsp;Check more about it &lt;a href="http://www.scipy.org/Cookbook/LinearRegression?highlight=%28regression%29"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: arial, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;The goal of regression is to determine the values of the&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white;"&gt;ß&amp;nbsp;&lt;/span&gt;parameters that minimize the sum of the squared residual values (difference betwen predicted and the observed) for the set of observations. Since linear regression is restricted to fiting linear (straight line/plane) functions to data, it's not adequate to real-world data as more general techniques such as neural networks which can model non-linear functions. &amp;nbsp;But linear regression has some interesting advantages:&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;li&gt;Linear regression is the most widely used method, and it is well understood.&lt;/li&gt;&lt;li&gt;Training a linear regression model is usually much faster than methods such as neural networks.&lt;/li&gt;&lt;li&gt;Linear regression models are simple and require minimum memory to implement, so they work well on embedded controllers that have limited memory space.&lt;/li&gt;&lt;li&gt;By examining the magnitude and sign of the regression coefficients (&lt;i&gt;β&lt;/i&gt;) you can infer how predictor variables affect the target outcome.&lt;/li&gt;&lt;li&gt;It's is one of the simplest algorithms and available in several packages, even Microsoft Excel!&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: inherit;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I hope you enjoyed this simple post, and in the next one I will explore another field of machine learning with Python! You can download the code at this &lt;a href="http://dl.dropbox.com/u/1977573/linearregression.zip"&gt;link&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: arial, sans-serif;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: arial, sans-serif;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: arial, sans-serif;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 14px;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-7387615980281248256?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/7387615980281248256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/10/machine-learning-with-python-linear.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/7387615980281248256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/7387615980281248256'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/10/machine-learning-with-python-linear.html' title='Machine Learning with Python -  Linear Regression'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-nZcik2kHLZM/TqofdW2e6cI/AAAAAAAAAnE/Yd2xSKNErMU/s72-c/plot1.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-2122219196495481541</id><published>2011-10-10T21:27:00.000-07:00</published><updated>2011-10-11T05:38:20.071-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='compiler'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='pypy'/><category scheme='http://www.blogger.com/atom/ns#' term='profiling'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='high performance'/><category scheme='http://www.blogger.com/atom/ns#' term='cython'/><title type='text'>High Performance Computation with Python - Part 04</title><content type='html'>&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Hi all,&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This article is the fourth one of the series about High Computation with Python. &amp;nbsp;For anyone that missed the first, second and third parts check this&amp;nbsp;&lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html"&gt;link&lt;/a&gt;&amp;nbsp;about Python Profiling, this&amp;nbsp;&lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with_17.html"&gt;one&lt;/a&gt;&amp;nbsp;about Cython and finally &lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with_24.html"&gt;this&lt;/a&gt; about&amp;nbsp; Numpy Vectors. The goal is to present approaches to make CPU-demanding tasks in Python run much faster.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The techniques that are being covered:&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;ol style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;li&gt;&lt;s&gt;&amp;nbsp;&lt;/s&gt;&lt;b&gt;&lt;s&gt;Python Profiling&lt;/s&gt;&lt;/b&gt;&lt;s&gt;&amp;nbsp;- How to find bottlenecks&lt;/s&gt;&lt;/li&gt;&lt;li&gt;&lt;strike&gt;&amp;nbsp;&lt;b&gt;Cython&lt;/b&gt;&amp;nbsp;- &amp;nbsp;Annotate your code and compile to C&lt;/strike&gt;&lt;/li&gt;&lt;li&gt;&lt;strike&gt;&amp;nbsp;&lt;b&gt;Numpy Vectors&lt;/b&gt;&amp;nbsp;- Fast vector operations using numpy arrays&lt;/strike&gt;&lt;/li&gt;&lt;li&gt;&lt;strike&gt;&lt;b&gt;&amp;nbsp;Numpy integration with Cython&lt;/b&gt;&amp;nbsp;- fast numerical Python library wrapped by Cython&lt;/strike&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;PyPy&lt;/b&gt;&amp;nbsp;- Python's new Just in Time &amp;nbsp;Compiler&lt;/li&gt;&lt;/span&gt;&lt;/ol&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;In this post I will talk about PyPy - the JIT Compiler for Python!&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;The Problem&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;div&gt;&lt;div style="font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;In this series we will analyze how to optimize the statistical&amp;nbsp;&lt;b&gt;Spearman Rank's Correlation coefficient,&lt;/b&gt;&amp;nbsp;&amp;nbsp;which it is a particular measure used to compute the similarity between items in recommender systems and assesses how well the relationship between two variables can be described using a monotonic function. The source code for this metric can be found in the&amp;nbsp;&lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html" style="color: #0071bb; text-decoration: none;"&gt;first post&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Pypy&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;PyPy is a Just in TIme compiler for the Python programming language. It is multi-platform and it runs Python 2.7. &amp;nbsp;With your code running in PyPy, it will make your code (depending on how you write your code) run faster (2 - 10 x speed-ups). &amp;nbsp; Sometimes some work has to be done in the code because of the use of shortcuts that works in CPython that aren't actually correct in the Python specification.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;You can download and install PyPy &lt;a href="http://pypy.org/download.html#installing"&gt;here&lt;/a&gt;. To install it, just place it in your home directory and put a symlink from somewhere to it. &amp;nbsp;Let's run the spearman.py with PyPy and without Python and see the performance difference;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: justify;"&gt;&lt;a href="http://2.bp.blogspot.com/-V2sUwfESY9o/TpPAnMKAIrI/AAAAAAAAAmw/BG9AMmRl20Y/s1600/Screen+shot+2011-10-11+at+1.09.13+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="88" src="http://2.bp.blogspot.com/-V2sUwfESY9o/TpPAnMKAIrI/AAAAAAAAAmw/BG9AMmRl20Y/s640/Screen+shot+2011-10-11+at+1.09.13+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;"&gt;The difference is about &lt;b&gt;34.77%&lt;/b&gt; faster with PyPy against pure Python &amp;nbsp;considering the input with 190340 on my Macbook. The amazing part is that I didn't change any line of my code! \m/ &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;"&gt;If you aren't using a C library like numpy then you should check PyPy - it might just make your code run several times faster. They are still porting PyPy to support Numpy since it some C libraries required &amp;nbsp; for running Numpy must be rewritten. &amp;nbsp; You can see some benchmarks of the porting &lt;a href="http://morepypy.blogspot.com/2011/05/numpy-in-pypy-status-and-roadmap.html"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;Although the PyPy team gave us a simple integration with array interfaces that behaves in a numpy-like fashion, &amp;nbsp;for now it has very few functions and only supports double arithmetic.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: justify;"&gt;&lt;a href="http://1.bp.blogspot.com/-4pxqKwpDXUc/TpPDY47etUI/AAAAAAAAAm4/c6kEfpAnxBk/s1600/Screen+shot+2011-10-11+at+1.21.17+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="158" src="http://1.bp.blogspot.com/-4pxqKwpDXUc/TpPDY47etUI/AAAAAAAAAm4/c6kEfpAnxBk/s640/Screen+shot+2011-10-11+at+1.21.17+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: normal;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;I strongly recommend you to take a look at PyPy, it shows a great promise for high performance Python with little effort and specially for the scientific community with the support with existing numpy would be a great advance!&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;I didn't mention until now by I will write a special post to close this series with High Performance with Python: It is about the module &lt;a href="http://docs.python.org/library/multiprocessing.html"&gt;multiprocessing&lt;/a&gt; and how you can work with it. I will show some examples and a library called JobLib that wraps it where you can easily use the power of the processors of your machine and do some parallell work.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;See you next time,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;Regards,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;Marcel Caraciolo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 22px;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-2122219196495481541?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/2122219196495481541/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/10/high-performance-computation-with.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2122219196495481541'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/2122219196495481541'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/10/high-performance-computation-with.html' title='High Performance Computation with Python - Part 04'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-V2sUwfESY9o/TpPAnMKAIrI/AAAAAAAAAmw/BG9AMmRl20Y/s72-c/Screen+shot+2011-10-11+at+1.09.13+AM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-6310174991084886032</id><published>2011-10-10T06:32:00.000-07:00</published><updated>2011-10-10T06:32:39.991-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='slides'/><category scheme='http://www.blogger.com/atom/ns#' term='recommendation systems'/><category scheme='http://www.blogger.com/atom/ns#' term='lecture'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='social networks'/><category scheme='http://www.blogger.com/atom/ns#' term='atepassar'/><category scheme='http://www.blogger.com/atom/ns#' term='crab'/><title type='text'>Presentation at VII Brazilian Symposium of Collaborative Systems (SBSC) about Recommender Systems</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;I am sharing the slides from my keynote at&lt;a href="http://www.addlabs.uff.br/sbsc_site/index.html"&gt; VII Brazilian Symposium of Collaborative Systems (SBSC) &lt;/a&gt;where I presented my current work at recommender systems focusing in social networks.&lt;br /&gt;&lt;br /&gt;My work&lt;a href="http://www.slideshare.net/marcelcaraciolo/content-recommendation-based-on-data-mining-in-adaptive-social-networks"&gt; "Content Recommendation based on Data Mining in Adaptive Social Networks"&lt;/a&gt; &amp;nbsp;presents how I built the recommender system at the educational social network &lt;a href="http://atepassar.com/"&gt;AtéPassar&lt;/a&gt; and the current results behind it. It is a novel project in brazilian social networks specially because I worked hard at the explanations that come along with each recommendation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;div id="__ss_9630540" style="width: 425px;"&gt;&lt;strong style="display: block; margin: 12px 0 4px;"&gt;&lt;a href="http://www.slideshare.net/marcelcaraciolo/content-recommendation-based-on-data-mining-in-adaptive-social-networks" target="_blank" title="Content Recommendation Based on Data Mining in Adaptive Social Networks"&gt;Content Recommendation Based on Data Mining in Adaptive Social Networks&lt;/a&gt;&lt;/strong&gt; &lt;iframe frameborder="0" height="355" marginheight="0" marginwidth="0" scrolling="no" src="http://www.slideshare.net/slideshow/embed_code/9630540" width="425"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;div style="padding: 5px 0 12px;"&gt;View more &lt;a href="http://www.slideshare.net/" target="_blank"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/marcelcaraciolo" target="_blank"&gt;Marcel Caraciolo&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;Soon I will provide the paper. &amp;nbsp;The special part of this event was the track only for recommender systems! I had the opportunity to meet brazilian researchers and developers interested in this field.&lt;br /&gt;&lt;br /&gt;I hope I will participate again next year! It is a great event for the researchers who miss those events focused on recommender systems.&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;&lt;br /&gt;Marcel&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-6310174991084886032?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/6310174991084886032/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/10/presentation-at-vii-brazilian-symposium.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6310174991084886032'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/6310174991084886032'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/10/presentation-at-vii-brazilian-symposium.html' title='Presentation at VII Brazilian Symposium of Collaborative Systems (SBSC) about Recommender Systems'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-8555650387269536150</id><published>2011-10-03T08:59:00.000-07:00</published><updated>2011-10-03T08:59:15.774-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='pugpe'/><category scheme='http://www.blogger.com/atom/ns#' term='joblib'/><category scheme='http://www.blogger.com/atom/ns#' term='slides'/><category scheme='http://www.blogger.com/atom/ns#' term='keynotes'/><category scheme='http://www.blogger.com/atom/ns#' term='recommendation systems'/><category scheme='http://www.blogger.com/atom/ns#' term='ipython'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='pythonbrasil'/><category scheme='http://www.blogger.com/atom/ns#' term='scikit'/><category scheme='http://www.blogger.com/atom/ns#' term='crab'/><title type='text'>Slides from Keynotes at VII PythonBrasil</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I'd like to share the slides of the keynotes I lectured at the &lt;a href="http://www.pythonbrasil.org.br/"&gt;VII PythonBrasil&lt;/a&gt;, the Brazilian Python Users Meeting that happens once a year. &amp;nbsp; This year I had the opportunity to give two talks: One is about the &lt;a href="http://www.slideshare.net/marcelcaraciolo/construindo-comunidades-opensource-bem-sucedidas-experincias-do-pugpe"&gt;Open-Source Communities and the experience with the local community of Pernambuco&lt;/a&gt;: The Python User Group of Pernambuco &lt;a href="http://pug.pe/"&gt;(PUG-PE&lt;/a&gt;) and about the framework I am currently working on: &lt;a href="http://muricoca.github.com/crab"&gt;Crab - A Python Framework for Building Recommender Systems.&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It was an amazing event and with lots of amazing keynotes, opportunities to meet people and make some friends. I also had the opportunity to give two more lighting talks: the pipeline toolkit for scientific computations &lt;a href="http://packages.python.org/joblib/"&gt;JobLib&lt;/a&gt; and about &lt;a href="http://ipython.org/"&gt;Ipython&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Below the slides provided:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;center&gt;&lt;div id="__ss_9509238" style="width: 425px;"&gt;&lt;strong style="display: block; margin: 12px 0 4px;"&gt;&lt;a href="http://www.slideshare.net/marcelcaraciolo/crab-a-python-framework-for-building-recommender-systems" target="_blank" title="Crab: A Python Framework for Building Recommender Systems "&gt;Crab: A Python Framework for Building Recommender Systems &lt;/a&gt;&lt;/strong&gt; &lt;iframe frameborder="0" height="355" marginheight="0" marginwidth="0" scrolling="no" src="http://www.slideshare.net/slideshow/embed_code/9509238" width="425"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;div style="padding: 5px 0 12px;"&gt;View more &lt;a href="http://www.slideshare.net/" target="_blank"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/marcelcaraciolo" target="_blank"&gt;Marcel Caraciolo&lt;/a&gt; &lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;The Crab slides for &lt;a href="http://www.slideshare.net/marcelcaraciolo/crab-a-python-framework-for-building-recommender-systems"&gt;download&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/center&gt;&lt;center&gt;&lt;div id="__ss_9480970" style="width: 425px;"&gt;&lt;strong style="display: block; margin: 12px 0 4px;"&gt;&lt;a href="http://www.slideshare.net/marcelcaraciolo/joblib-the-pipelining" target="_blank" title="JobLib: Running Python function as pipeline jobs"&gt;JobLib: Running Python function as pipeline jobs&lt;/a&gt;&lt;/strong&gt; &lt;iframe frameborder="0" height="355" marginheight="0" marginwidth="0" scrolling="no" src="http://www.slideshare.net/slideshow/embed_code/9480970" width="425"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;div style="padding: 5px 0 12px;"&gt;View more &lt;a href="http://www.slideshare.net/" target="_blank"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/marcelcaraciolo" target="_blank"&gt;Marcel Caraciolo&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;The JobLib slides for &lt;a href="http://www.slideshare.net/marcelcaraciolo/joblib-the-pipelining"&gt;download&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I'd like also to announce the launch of the &lt;a href="http://muricoca.github.com/crab"&gt;new home page&lt;/a&gt; of the project Crab with a reformulated design. It still in development, with lots of work to do, but it's coming! The first release 0.1 will be launched until the second week of October.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-iI2f9O3yHek/TonbS6waVMI/AAAAAAAAAms/-IoG11HIqSU/s1600/Screen+shot+2011-10-03+at+1.00.13+PM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="369" src="http://4.bp.blogspot.com/-iI2f9O3yHek/TonbS6waVMI/AAAAAAAAAms/-IoG11HIqSU/s640/Screen+shot+2011-10-03+at+1.00.13+PM.png" width="640" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Crab new Home Page&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Thanks for the feedback from all developers at PythonBrasil and I expect new contributors at the project.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Regards,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Marcel Caraciolo&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-8555650387269536150?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/8555650387269536150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/10/slides-from-keynotes-at-vii.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/8555650387269536150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/8555650387269536150'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/10/slides-from-keynotes-at-vii.html' title='Slides from Keynotes at VII PythonBrasil'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-iI2f9O3yHek/TonbS6waVMI/AAAAAAAAAms/-IoG11HIqSU/s72-c/Screen+shot+2011-10-03+at+1.00.13+PM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-1935413937695174072</id><published>2011-09-24T14:10:00.000-07:00</published><updated>2011-09-24T14:10:47.150-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='profiling'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='high performance'/><category scheme='http://www.blogger.com/atom/ns#' term='cython'/><title type='text'>High Performance Computation with Python - Part 03</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Hi all,&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This article is the third one of the series about High Computation with Python. &amp;nbsp;For anyone that missed the first and the second parts check this &lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html"&gt;link&lt;/a&gt; and this &lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with_17.html"&gt;one&lt;/a&gt;. &amp;nbsp;The goal is to present approaches to make CPU-demanding tasks in Python run much faster.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The techniques that are being covered:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-size: 14px; line-height: 22px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;li&gt;&lt;s&gt;&amp;nbsp;&lt;/s&gt;&lt;b&gt;&lt;s&gt;Python Profiling&lt;/s&gt;&lt;/b&gt;&lt;s&gt;&amp;nbsp;- How to find bottlenecks&lt;/s&gt;&lt;/li&gt;&lt;li&gt;&lt;strike&gt;&amp;nbsp;&lt;b&gt;Cython&lt;/b&gt;&amp;nbsp;- &amp;nbsp;Annotate your code and compile to C&lt;/strike&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Numpy Vectors&lt;/b&gt;&amp;nbsp;- Fast vector operations using numpy arrays&lt;/li&gt;&lt;li&gt;&lt;b&gt;&amp;nbsp;Numpy integration with Cython&lt;/b&gt;&amp;nbsp;- fast numerical Python library wrapped by Cython&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;PyPy&lt;/b&gt;&amp;nbsp;- Python's new Just in Time &amp;nbsp;Compiler&lt;/li&gt;&lt;/span&gt;&lt;/ol&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;In this post I will talk about Numpy Vectors and how you can wrap it with Cython!&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;The Problem&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;In this series we will analyze how to optimize the statistical&amp;nbsp;&lt;b&gt;Spearman Rank's Correlation coefficient,&lt;/b&gt;&amp;nbsp;&amp;nbsp;which it is a particular measure used to compute the similarity between items in recommender systems and assesses how well the relationship between two variables can be described using a monotonic function. The source code for this metric can be found in the&amp;nbsp;&lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html" style="color: #0071bb; text-decoration: none;"&gt;first post&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Numpy&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;a href="http://numpy.scipy.org/"&gt;Numpy&lt;/a&gt; is a powerful extension to Python, adding support for large, multi-dimensional array and matrices, along with several mathematical functions to manipulate these arrays. To install it you can type this command at your terminal&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;$ sudo easy_install numpy&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;Or&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;$ pip install numpy&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;In our example we will change the spearman.py . Import the numpy library and change the&amp;nbsp;spearman_correlation to look &amp;nbsp;the one below. If you run and test it you will ger the same output as before.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;script src="https://gist.github.com/1239786.js?file=spearman_numpy.py"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;The numpy strength is that can simplify lots of operations on vectors or matrixes of numbers since they work directly in all list rather than on individual elements&amp;nbsp;at one time. &amp;nbsp;So before we had nested for loops over individual terms in a list, now with numpy you could do the same job in a faster and simple way.&lt;br /&gt;Some notes:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You define an array with numpy.array statement, in our case a list of tuples indexed by the labels keys and ranks. (lines 29 and 30).&lt;/li&gt;&lt;li&gt;Lots of operations already implemented in numpy, such as numpy.in1d which finds where the elements in the first vector are in the second vector returning an array os bools.&lt;/li&gt;&lt;li&gt;&amp;nbsp;We have numpy.sort which sort the elements based on a key, in this example (ranks) (lines 16 and 17).&lt;/li&gt;&lt;li&gt;diffs * diffs does a pairwise multiplication, think of it as diff[0] = diff[0] * diff[0]; diff[1] = diff[1] * diff[1]...; diff[n-1] = diff[n-1] * diff[n-1]. (line 36)&lt;/li&gt;&lt;li&gt;size is an attribute from numpy.array to fetch the m*n elements (count) from an array.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;If it stills unclear I suggest you to try it at the command line, step-by-step to look over the results. Put a small number of elements in the array and see it in action.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;div style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Numpy with Cython&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;div style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Numpy is a powerful library and uses very fast C optimized math libraries to perform these calculations very quickly. You can also wrap your python code with Cython. The main difference is the annotation of the numpy arrays. You can see the &lt;a href="http://cython.readthedocs.org/en/latest/src/tutorial/numpy.html"&gt;tutorial&lt;/a&gt; for further details. &amp;nbsp;The difference are how we import: cimport numpy as np and the assinature of the function _rank_dists.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: small; line-height: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Special Notes - Meeting Scipy&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;Another poweful library is Scipy, it is a package for Python that brings several algebra techniques for dealing with matrices and vectors. &amp;nbsp;One special module is the &lt;a href="http://docs.scipy.org/doc/scipy/reference/stats.html"&gt;scipy.stats&lt;/a&gt;, which comes with the &lt;a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html"&gt;spearmanr&lt;/a&gt; function. It receives two arrays with the observations and returns the spearman coefficient. Amazing! Let's see our code below:&lt;/span&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1239843.js?file=spearman_scipy.py"&gt;&lt;/script&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px;"&gt;In the next post we will study the Pypy, a JIT Compiler which can speed your code with minimal changes at your code!&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;I hope you enjoyed this article,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;Marcel Caraciolo&lt;span id="goog_1173159328"&gt;&lt;/span&gt;&lt;span id="goog_1173159329"&gt;&lt;/span&gt;&lt;a href="http://draft.blogger.com/"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-1935413937695174072?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/1935413937695174072/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with_24.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/1935413937695174072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/1935413937695174072'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with_24.html' title='High Performance Computation with Python - Part 03'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-161673904568613458</id><published>2011-09-17T05:54:00.000-07:00</published><updated>2011-09-17T05:54:28.971-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='profiling'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='high performance'/><category scheme='http://www.blogger.com/atom/ns#' term='cython'/><title type='text'>High Performance Computation with Python - Part 02</title><content type='html'>&lt;div style="text-align: justify;"&gt;Hi all,&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Let's continue our series of posts about &lt;b&gt;High Performance with Python.&lt;/b&gt;&amp;nbsp;Last post I presented how you analyze your code using &lt;b&gt;Python Profiling&lt;/b&gt;. &amp;nbsp;If you missed the first part please check this &lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html"&gt;link&lt;/a&gt;. &amp;nbsp;To sum up, our goal is to present several techniques to make CPU-demanding tasks in Python run much faster.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;The techniques that will be covered:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px; text-align: justify;"&gt;&lt;ol&gt;&lt;li&gt;&lt;s&gt;&amp;nbsp;&lt;/s&gt;&lt;b&gt;&lt;s&gt;Python Profiling&lt;/s&gt;&lt;/b&gt;&lt;s&gt;&amp;nbsp;- How to find bottlenecks&lt;/s&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Cython&lt;/b&gt;&amp;nbsp;- &amp;nbsp;Annotate your code and compile to C&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Numpy Vectors&lt;/b&gt;&amp;nbsp;- Fast vector operations using numpy arrays&lt;/li&gt;&lt;li&gt;&lt;b&gt;&amp;nbsp;Numpy integration with Cython&lt;/b&gt;&amp;nbsp;- fast numerical Python library wrapped by Cython&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;PyPy&lt;/b&gt;&amp;nbsp;- Python's new Just in Time &amp;nbsp;Compiler&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;In this post I will talk about Cython and how do you compile your code to C with this powerful tool!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;The Problem&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="font-family: Times; font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;In this series we will analyze how to optimize the statistical&amp;nbsp;&lt;b&gt;Spearman Rank's Correlation coefficient,&lt;/b&gt;&amp;nbsp;&amp;nbsp;which it is a particular measure used to compute the similarity between items in recommender systems and assesses how well the relationship between two variables can be described using a monotonic function. The source code for this metric can be found in the &lt;a href="http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html"&gt;last post&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Times; font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Times; font-size: medium; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: small; line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; line-height: 22px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;Cython&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px;"&gt;Cython&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt; is a Python extension that lets developers annotate functions so they can be compiled to C. It takes a little time to develop but typically give a nice speed-up. &amp;nbsp;If you're starting now with Cython, I recommend you to check this &lt;a href="http://docs.cython.org/src/userguide/tutorial.html"&gt;tutorial&lt;/a&gt;, it quite useful for beginners.&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px;"&gt;In our previous example we decided to optimize the function spearman_correlation. So first we will start a new module called&amp;nbsp;spearman_correlation_cython.py&amp;nbsp;, and move the&amp;nbsp;spearman_correlation&amp;nbsp;function into this module. In the original source you will have to import the&amp;nbsp;spearman_correlation_cython&amp;nbsp;and replace the reference to&amp;nbsp;spearman_correlation(...) &amp;nbsp;with&amp;nbsp;spearman_correlation_cython.&amp;nbsp;spearman_correlation(...).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;So the code for your&amp;nbsp;spearman_correlation_cython.py&amp;nbsp;now is:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; font-weight: normal; line-height: 14px; white-space: normal;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC20" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;def&lt;/span&gt; &lt;span class="nf" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;spearman_correlation&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC21" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"""Returns the Spearman correlation coefficient for two rankings, which&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC22" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    should be dicts or sequences of (key, rank). The coefficient ranges from&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC23" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    -1.0 (ranks are opposite) to 1.0 (ranks are identical), and is only&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC24" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    calculated for keys in both rankings (for meaningful results, remove keys&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC25" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    present in only one list before ranking)."""&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC26" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC27" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC28" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sorted&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;key&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;lambda&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC29" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sorted&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;key&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;lambda&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC30" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC31" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;],&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;enumerate&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC32" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;],&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;enumerate&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC33" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC34" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;_rank_dists&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC35" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC36" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC37" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;try&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC38" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;float&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;/&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)))&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC39" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;except&lt;/span&gt; &lt;span class="ne" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC40" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# Result is undefined if only one item is ranked&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC41" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0.0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Next we will have to rename the&amp;nbsp;spearman_correlation_cython.py to&amp;nbsp;spearman_correlation_cython.pyx. &amp;nbsp;Cython uses .pyx &amp;nbsp;to indicate that it is a file that will compile to C. &amp;nbsp;Add also a new setup.py with the following contents:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC1" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# setup.py&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC2" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;distutils.core&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;setup&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;distutils.extension&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Extension&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC4" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Cython.Distutils&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC5" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# for notes on compiler flags see:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# http://docs.python.org/install/index.html&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC7" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;setup&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC8" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;cmdclass&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;{&lt;span class="Apple-style-span" style="color: #a61717;"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;span class="Apple-style-span" style="color: #a61717;"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC9" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ext_modules&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Extension&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"spearman_correlation_cython"&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"spearman_correlation_cython.pyx"&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;Now run the command:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;$ python setup.py build_ext --inplace&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This &amp;nbsp;command runs the setup.py script that we just created by calling the build_ext command. The new module is built in-place in the directory. &amp;nbsp;You will see that it will be generated a new r&amp;nbsp;spearman_correlation_cython.so in the directory.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Run the new code using python cython_spearman 199999 and you will see a slight improvement in the speed of the calculation (very minor yet!). &amp;nbsp;You can take a look to see how well the slower Python calls are being replace with faster Cython calls by using:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;$ cython -a rank_dists.pyx&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It &amp;nbsp;will generate a rank_dists.html file. If you open it in your browser, you will see something like:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-7g1t5RRXxMU/TnQo9Ca-2_I/AAAAAAAAAmk/GV06bBiu6Kc/s1600/Screen+shot+2011-09-17+at+1.59.48+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="350" src="http://3.bp.blogspot.com/-7g1t5RRXxMU/TnQo9Ca-2_I/AAAAAAAAAmk/GV06bBiu6Kc/s400/Screen+shot+2011-09-17+at+1.59.48+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Result of cython -a spearman_correlation_cython&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="font-size: small; line-height: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The workflow now that you will use at your code is progressive. Each time you add a type with Cython, it may improve the resulting code. When it does so successfully, you will see that the dark yellow lines will turn lighter and eventually they will turn white (it will represent that there is no need for improvements, it is faster!). &amp;nbsp;If you are interested to analyze deeper, you could expand the code by double clicking at one of the lines with yellow code. It will show the C Python API calls that it is making.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-IbOcjCu--cI/TnQiWiJhK2I/AAAAAAAAAmg/R4d2KVwupWA/s1600/Screen+shot+2011-09-17+at+1.32.17+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="368" src="http://4.bp.blogspot.com/-IbOcjCu--cI/TnQiWiJhK2I/AAAAAAAAAmg/R4d2KVwupWA/s640/Screen+shot+2011-09-17+at+1.32.17+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Double-Clicking at one of the yellow lines of code at html it will show the C &amp;nbsp;Python API calls&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You could also add annotations. So if you add type definitions (such as cdef int or cdef double... ) &amp;nbsp;and run the cython -a ... command, you will can monitor the reduction in yellow in your browser. &amp;nbsp;Don't forget to recompile using the setup.py command and confirm that the result is slightly faster!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-J2kbIaoRJzA/TnQqlbUscMI/AAAAAAAAAmo/IutzyzK6nhA/s1600/Screen+shot+2011-09-17+at+2.07.42+AM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="222" src="http://2.bp.blogspot.com/-J2kbIaoRJzA/TnQqlbUscMI/AAAAAAAAAmo/IutzyzK6nhA/s400/Screen+shot+2011-09-17+at+2.07.42+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Added some Cython types at the source code.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;Cython Compiler Directives&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;You could also set several &lt;a href="http://wiki.cython.org/enhancements/compilerdirectives"&gt;compiler directives&lt;/a&gt; that comes with Cython. &amp;nbsp;To enable them, you could use a comment at the top of the file or by changing the setup.py or even decorating the function individually.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Using the comment at top of the file.&lt;/div&gt;&lt;div&gt;&lt;div class="codearea" dir="ltr" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: solid; border-bottom-width: 1pt; border-left-color: rgb(174, 189, 204); border-left-style: solid; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: solid; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: solid; border-top-width: 1pt; color: black; font-family: Arial, 'Lucida Grande', sans-serif; font-size: 16px; line-height: normal; margin-bottom: 0.5em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;pre dir="ltr" id="CA-84ce8379484f3ab0fa78d78fb97512c96309aaa9" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="LineNumber" style="color: grey;"&gt;   1 &lt;/span&gt;&lt;span class="Comment" style="color: green;"&gt;#cython: boundscheck=False&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, 'Lucida Grande', sans-serif; font-size: 16px; line-height: normal;"&gt;&lt;span class="anchor" id="line-27"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;Using the decorate function&lt;/div&gt;&lt;div&gt;&lt;div class="codearea" dir="ltr" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: solid; border-bottom-width: 1pt; border-left-color: rgb(174, 189, 204); border-left-style: solid; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: solid; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: solid; border-top-width: 1pt; color: black; font-family: Arial, 'Lucida Grande', sans-serif; font-size: 16px; line-height: normal; margin-bottom: 0.5em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;pre dir="ltr" id="CA-ed434df0715872a7b4b2d44675c656a80a843432" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="LineNumber" style="color: grey;"&gt; &lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;import&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;cython&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre dir="ltr" id="CA-ed434df0715872a7b4b2d44675c656a80a843432" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="ID" style="color: black;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, 'Lucida Grande', sans-serif; white-space: normal;"&gt;&lt;pre dir="ltr" id="CA-c42725b645439a41109e112bffd2d4069b28d84f" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre dir="ltr" id="CA-c42725b645439a41109e112bffd2d4069b28d84f" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;@&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;cython&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;boundscheck&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;(&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;False&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;)&lt;/span&gt; &lt;span class="Comment" style="color: green;"&gt;# turn off boundscheck for this function&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;&lt;span class="ResWord" style="color: #a00000;"&gt;def&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;f&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;(&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;)&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;    &lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;    &lt;span class="ResWord" style="color: #a00000;"&gt;with&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;cython&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;boundscheck&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;(&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;True&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;)&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;:&lt;/span&gt; &lt;span class="Comment" style="color: green;"&gt;# turn it temporarily on again for this block&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;        &lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, 'Lucida Grande', sans-serif; font-size: 16px; line-height: normal;"&gt;&lt;span class="anchor" id="line-41"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;Using the setup.py&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, 'Lucida Grande', sans-serif; font-size: 16px; line-height: normal;"&gt;&lt;pre dir="ltr" id="CA-43e62b04056df026d74b9906bf48baf5ddab28a5" lang="en" style="background-color: #f3f5f7; border-bottom-color: rgb(174, 189, 204); border-bottom-style: none; border-bottom-width: 1pt; border-color: initial; border-left-color: rgb(174, 189, 204); border-left-style: none; border-left-width: 1pt; border-right-color: rgb(174, 189, 204); border-right-style: none; border-right-width: 1pt; border-top-color: rgb(174, 189, 204); border-top-style: none; border-top-width: 1pt; border-width: initial; font-family: courier, monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 10pt; padding-left: 10pt; padding-right: 10pt; padding-top: 10pt; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="line"&gt;&lt;span class="ID" style="color: black;"&gt;ext_modules&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;=&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;[&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;Extension&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;(&lt;/span&gt;&lt;span class="String" style="color: #004080;"&gt;"spam"&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;,&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;[&lt;/span&gt;&lt;span class="String" style="color: #004080;"&gt;"spam.pyx"&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;]&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;)&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;               &lt;span class="ID" style="color: black;"&gt;Extension&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;(&lt;/span&gt;&lt;span class="String" style="color: #004080;"&gt;"ham"&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;,&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;[&lt;/span&gt;&lt;span class="String" style="color: #004080;"&gt;"ham.pyx"&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;]&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;)&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;&lt;span class="ResWord" style="color: #a00000;"&gt;for&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;e&lt;/span&gt; &lt;span class="ResWord" style="color: #a00000;"&gt;in&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;ext&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;modules&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="line"&gt;    &lt;span class="ID" style="color: black;"&gt;e&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;.&lt;/span&gt;&lt;span class="ID" style="color: black;"&gt;pyrex_directives&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;=&lt;/span&gt; &lt;span class="Operator" style="color: #0000c0;"&gt;{&lt;/span&gt;&lt;span class="String" style="color: #004080;"&gt;"boundscheck"&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;:&lt;/span&gt; &lt;span class="ID" style="color: black;"&gt;False&lt;/span&gt;&lt;span class="Operator" style="color: #0000c0;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;One of the most used is the &lt;b&gt;cProfile&lt;/b&gt;, that is useful for profiling cython code. It gives you exactly same output as running cProfile on a normal python module. Another common directive is the &lt;b&gt;boundscheck&lt;/b&gt;. &amp;nbsp;It desables out-of-bounds index checking on buffered arrays (common in numpy arrays, so since it does not check for IndexError exceptions it will run faster. Remember that any mistake prepare to expect a segmentation fault. So be careful when you decide to use boundscheck, that is, be sure that code is working correctly as you planned. &amp;nbsp;There is also another one, the &lt;b&gt;infer_types&lt;/b&gt; which is supposed to guess the type of variables.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;Cython directly with C&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;But you may asking yourself if it is possible to wrap with Cython your existing libraries of C code. Yes it is possible! &amp;nbsp;Cython uses external declarations to declare the C functions and variables from the library that you want to use. So let's see a quick example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Let's consider a simple fatorial function written in C and we want to wrap it and call with Python/Cython:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;fatorialEx.c&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC2" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="cp" style="color: #999999; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC4" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt; &lt;span class="nf" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;fatorial&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;){&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC5" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;	&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;if&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;==&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC8" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;		&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;	&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC11" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC12" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;	&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;fatorial&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC14" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC15" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC16" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 12px; line-height: 16px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC16" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-size: 14px; line-height: 22px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 12px; line-height: 16px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Now, you have to wrap it at your fatorial.pyx module:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;fatorial.pyx&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;i&gt;cdef extern from "fatorialEx.c":&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;i&gt;	&lt;/i&gt;&lt;/span&gt;&lt;i&gt;int fatorial(int n)&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;def fat(n):&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;i&gt;	&lt;/i&gt;&lt;/span&gt;&lt;i&gt;return fatorial(n)&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;See the line cdef extern (it's how Cython knows how to include external libraries). Finally create your setup.py module to build the extension:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC1" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# setup.py&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC2" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;distutils.core&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;setup&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;distutils.extension&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Extension&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC4" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;from&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Cython.Distutils&lt;/span&gt; &lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC5" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# for notes on compiler flags see:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# http://docs.python.org/install/index.html&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC7" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;setup&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC8" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;cmdclass&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;{&lt;span class="Apple-style-span" style="color: #a61717;"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;span class="Apple-style-span" style="color: #a61717;"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;build_ext&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC9" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ext_modules&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Extension&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"fatorial"&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"fatorial.pyx"&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium; line-height: 22px; white-space: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;br /&gt;Build it:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;i&gt;$python setup.py build_ext --inplace&amp;nbsp;&lt;/i&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;You will see the module fatorial.so in the directory, this is the file that you will use now to import your code at Python. So in the Python console , type the following commands to test it:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&amp;gt;&amp;gt;&amp;gt; from fatorial import fat&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&amp;gt;&amp;gt;&amp;gt; fat(5)&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;120&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It is working! &amp;nbsp;I am providing the &lt;a href="http://dl.dropbox.com/u/1977573/fatorial.zip"&gt;source&lt;/a&gt; for this example. For further information about writing your extension check the &lt;a href="http://docs.cython.org/src/userguide/external_C_code.html"&gt;Cython docs&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div style="display: inline !important;"&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div style="display: inline !important;"&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div style="display: inline !important;"&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div style="display: inline !important;"&gt;That's all, &amp;nbsp; I hope &amp;nbsp;you enjoyed!&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: medium;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: arial, 'times New Roman', tahoma; font-size: 14px; font-weight: normal;"&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Regards,&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Marcel Caraciolo&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-161673904568613458?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/161673904568613458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with_17.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/161673904568613458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/161673904568613458'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with_17.html' title='High Performance Computation with Python - Part 02'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-7g1t5RRXxMU/TnQo9Ca-2_I/AAAAAAAAAmk/GV06bBiu6Kc/s72-c/Screen+shot+2011-09-17+at+1.59.48+AM.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-3840246042456269091</id><published>2011-09-11T21:04:00.000-07:00</published><updated>2011-09-11T21:04:20.319-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='article'/><category scheme='http://www.blogger.com/atom/ns#' term='profiling'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='high performance'/><title type='text'>High Performance Computation with Python - Part 01</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;I decided to start a series of posts about &lt;b&gt;High Performance with Python&lt;/b&gt;. &amp;nbsp;I will give a brief explanation about how you can make CPU-bound tasks in Python run much faster. &amp;nbsp;For machine learning developers and who works with systems that demand processing and memory it is really important to have optimized algorithms to handle with large data sets.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I've been working since last year with recommender systems, and I will show some techniques that I used to make CPU-demanding tasks in Python run much faster. The techniques that will be covered:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;ol&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Python Profiling&lt;/b&gt; - How to find bottlenecks&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Cython&lt;/b&gt; - &amp;nbsp;Annotate your code and compile to C&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;Numpy Vectors&lt;/b&gt; - Fast vector operations using numpy arrays&lt;/li&gt;&lt;li&gt;&lt;b&gt;&amp;nbsp;Numpy integration with Cython&lt;/b&gt; - fast numerical Python library wrapped by Cython&lt;/li&gt;&lt;li&gt;&amp;nbsp;&lt;b&gt;PyPy&lt;/b&gt; - Python's new Just in Time &amp;nbsp;Compiler&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;In this post I will begin with the Python Profiling, so let's get started!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;The Problem&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;In this series we will analyze how to optimize the statistical &lt;b&gt;Spearman Rank's Correlation coefficient,&lt;/b&gt; &amp;nbsp;which it is a particular measure used to compute the similarity between items in recommender systems and assesses how well the relationship between two variables can be described using a monotonic function. It looks like the Pearson Correlation, but compares relative ranking of the preference values instead of preference values themselves. That is, each user's preferences are sorted, and then assign a rank as their preference value, with 1 being assigned to the least preferred item.&lt;/div&gt;&lt;div&gt;If you want a background on the correlation, please check this &lt;a href="http://en.wikipedia.org/wiki/Spearman's_rank_correlation_coefficient"&gt;link&lt;/a&gt;. We will look at improvements in Python to make the code run a bit faster and then we will look at fast ways to convert the code directly to C for the best speed-ups.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Consider the input:&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC41" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;   &amp;gt;&amp;gt;&amp;gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'a'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'b'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;3.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'c'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;3.0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'d'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;3.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC42" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'c'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;3.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'b'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'e'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1.0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;),&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'d'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1.5&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This is the output we’re after.&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white;"&gt;Let's look the pure python implementation for this measure.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;print&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;spearman_correlation&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    0.5&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #dd1144; font-family: 'Bitstream Vera Sans Mono', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: 12px; line-height: 16px; white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Pure Python implementation&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;pre style="font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC2" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: large;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;div&gt;Below we have the basic pure-python implementation. As you can see, we have the function spearman, which does the hard work calculating the output (correlation). &amp;nbsp;We also use a function _rank_dists that will compare common keys in both ranks and compute the difference between them. &amp;nbsp;The inputs are arrays of tuples, each with the key and the rating for the item. &amp;nbsp;For this snippet I will use as parameter the number of elements for each array (n) so we can evaluate the coefficient and see the difference in computation time when we vary the input.&lt;/div&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;pre style="font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC2" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times; font-size: small;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC1" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;datetime&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC2" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sys&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="kn" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;import&lt;/span&gt; &lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="nn" style="color: #555555; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;def&lt;/span&gt; &lt;span class="nf" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;_rank_dists&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC7" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"""Finds the difference between the values in ranks1 and ranks2 for keys&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC8" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    present in both dicts. If the arguments are not dicts, they are converted&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC9" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    from (key, rank) sequences.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC10" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    """&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC11" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;dict&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC12" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;dict&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC13" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;v1&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;iteritems&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;():&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC14" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;try&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC15" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;yield&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;v1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC16" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;except&lt;/span&gt; &lt;span class="ne" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;KeyError&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC17" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;pass&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC17" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC20" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;def&lt;/span&gt; &lt;span class="nf" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;spearman_correlation&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC21" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"""Returns the Spearman correlation coefficient for two rankings, which&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC22" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    should be dicts or sequences of (key, rank). The coefficient ranges from&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC23" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    -1.0 (ranks are opposite) to 1.0 (ranks are identical), and is only&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC24" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    calculated for keys in both rankings (for meaningful results, remove keys&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC25" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="sd" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;    present in only one list before ranking)."""&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC26" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC27" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC28" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sorted&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;key&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;lambda&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC29" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sorted&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;key&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;lambda&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC30" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC31" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;],&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;enumerate&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC32" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;],&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ix&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;t&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;enumerate&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC33" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC34" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;k&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;_rank_dists&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC35" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;d&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC36" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+=&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC37" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;try&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC38" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;float&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;res&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;/&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)))&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC39" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;except&lt;/span&gt; &lt;span class="ne" style="color: #990000; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC40" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;# Result is undefined if only one item is ranked&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC41" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;0.0&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC41" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC44" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;if&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;__name__&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;==&lt;/span&gt; &lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;'__main__'&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC45" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sys&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;argv&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC46" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC47" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC48" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_x&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;range&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt;  &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC49" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_y&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;range&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt;  &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC50" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;shuffle&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_y&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC51" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;shuffle&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_x&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC52" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC53" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC54" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[]&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC55" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;for&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;x&lt;/span&gt; &lt;span class="ow" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;in&lt;/span&gt; &lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;xrange&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;n&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;):&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC56" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;item1&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_x&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;pop&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC57" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;item2&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;possible_items_y&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;pop&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC58" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;append&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;((&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;item1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4.0&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1.0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;))&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC59" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;append&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;((&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;item2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;random&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;*&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4.0&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;+&lt;/span&gt; &lt;span class="mf" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1.0&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;))&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC60" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC61" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;start_time&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;datetime&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;datetime&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;now&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC62" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;print&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;spearman_correlation&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks1&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;ranks2&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC63" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;end_time&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;datetime&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;datetime&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;now&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC64" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;secs&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;end_time&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;-&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;start_time&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC65" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;print&lt;/span&gt; &lt;span class="s" style="color: #dd1144; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;"Main took"&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;,&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;secs&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;Download the code &lt;a href="http://dl.dropbox.com/u/1977573/spearman.py"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Profiling with CProfile, LineProfiler&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The profile module is the most common method to profile the Python Code. Take a look at it &lt;a href="http://docs.python.org/library/profile.html"&gt;here&lt;/a&gt;. &amp;nbsp;To run our Python using it just call at your terminal the following command:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;$ python -m cProfile -o rep.prof spearman.py&amp;nbsp;199999&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;This generates a out.prof output containing the profiling results. Then, we can now load this into the pstats module and print out the top 10 slowest functions for example.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;print&lt;/span&gt; import pstats&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; p = pstats.Stats('rep.prof')&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; p.sort_stats('cumulative').print_stats(10)&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;div&gt;&lt;div&gt;This generates a out.prof output containing the profiling results. Then, we can now load this into the &lt;a href="http://www.doughellmann.com/PyMOTW/profile/"&gt;pstats&lt;/a&gt; module and print out the top 10 slowest functions for example.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-mhN8UJekXg4/Tm160PoeFDI/AAAAAAAAAmQ/2AF8Vk2RAVg/s1600/Screen+shot+2011-09-12+at+12.23.49+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="203" src="http://2.bp.blogspot.com/-mhN8UJekXg4/Tm160PoeFDI/AAAAAAAAAmQ/2AF8Vk2RAVg/s640/Screen+shot+2011-09-12+at+12.23.49+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As you can see at the result above, it tells us that the spearman_correlation costs 0.755 seconds for its lines of code and in total (including all the function it calls) costs &amp;nbsp;about 3.024 CPU seconds. Two functions, we could analyze and improve it. One is the sort operation, which is called only once and costs about 20% of the total time as also the _rank_dists which is called 200000 times, and could be optimized to be faster (maybe?!).&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But sometimes the output when comes with several lines, it becomes hard to understand. &amp;nbsp;For this task you can use the libray &lt;b&gt;&lt;a href="http://www.vrplumber.com/programming/runsnakerun/"&gt;runsnake&lt;/a&gt;&lt;/b&gt;, which it is a visualization tool for display the profiled results.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So if you run at your terminal:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;$ runsnake rep.prof&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It will generate the following report. This tool is really useful specially when you have a complex project with several modules. You can easily identify which functions are worth dealing with first of all!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-NkKEQm7AhKA/Tm1-ACx10OI/AAAAAAAAAmU/eY5Lf2xVdMw/s1600/Screen+shot+2011-09-12+at+12.37.29+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="303" src="http://4.bp.blogspot.com/-NkKEQm7AhKA/Tm1-ACx10OI/AAAAAAAAAmU/eY5Lf2xVdMw/s400/Screen+shot+2011-09-12+at+12.37.29+AM.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But maybe you're wondering if is it possible to check which lines that are causing the code ro run slow ? &amp;nbsp;As you saw cProfile couldn't answer that. &amp;nbsp;So &amp;nbsp;we have to check the &lt;a href="http://packages.python.org/line_profiler/"&gt;line_profiler&lt;/a&gt; module. &amp;nbsp;All we need is to decorate our chosen function with @profile: &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;@profile&lt;/div&gt;&lt;div&gt;&lt;div&gt;def spearman_correlation(ranks1, ranks2):&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After we will run kernprof.py and make it to do line-by-line profiling and which module we want to profile. &lt;b&gt;Warning: Since the profiling now is line-by-line depending on your problem, it will take longer to profile it!&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&amp;gt;&amp;gt; kernprof.py -l -v pure_python.py 300 100&lt;/div&gt;&lt;div&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-A-tdIZZxYlk/Tm2AqnKS8eI/AAAAAAAAAmY/8gOUXVlOulY/s1600/Screen+shot+2011-09-12+at+12.48.40+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="310" src="http://4.bp.blogspot.com/-A-tdIZZxYlk/Tm2AqnKS8eI/AAAAAAAAAmY/8gOUXVlOulY/s640/Screen+shot+2011-09-12+at+12.48.40+AM.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;Here we can see that the most of the time spent is in the &amp;nbsp;for in _rank_dists loop (line 34) &amp;nbsp;as also in the building of the ranks (line 31,32). So if we could make those lines run faster then the entire function would run much faster. &amp;nbsp;The advantage of the line_profiler profiling is that you can easily identify which lines are causing you the biggest problems and focus directly on the bottlenecks rather than guessing which lines are the slow ones!&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;REMEMBER&lt;/b&gt; to remove the @profile decorator after you're done with the line_profiler or the Python will throw an exception since it won't recognize the @profile decorator outsied of kernprof.py&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In the next post I will cover the first optimization using Cython, where we will make some small modifications to the code. Stay tunned!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I expect you enjoyed this step-by-step analysis and it may be useful not only in scientific computing, but as also in non-CPU bound tasks. &amp;nbsp;You could profile your web serve to check out which lines are running slowly!&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;PS: &amp;nbsp;You could use the Python &lt;a href="http://docs.python.org/library/dis.html"&gt;module dis&lt;/a&gt; to disassemble your custom module and get a deeper analysis seeing the underlying&amp;nbsp;bytecode. Just type:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; import spearman&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC43" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;gt;&amp;gt; dis.dis(spearman)&lt;/div&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;That's all,&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Cheers,&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Marcel Caraciolo&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6000112949648041507-3840246042456269091?l=aimotion.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aimotion.blogspot.com/feeds/3840246042456269091/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/3840246042456269091'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6000112949648041507/posts/default/3840246042456269091'/><link rel='alternate' type='text/html' href='http://aimotion.blogspot.com/2011/09/high-performance-computation-with.html' title='High Performance Computation with Python - Part 01'/><author><name>Marcel Caraciolo</name><uri>https://profiles.google.com/108673643727142772243</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-tJwd_9sL0u0/AAAAAAAAAAI/AAAAAAAAAAA/KrHpwX8s-9Y/s512-c/photo.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-mhN8UJekXg4/Tm160PoeFDI/AAAAAAAAAmQ/2AF8Vk2RAVg/s72-c/Screen+shot+2011-09-12+at+12.23.49+AM.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6000112949648041507.post-577782762787969320</id><published>2011-09-03T15:38:00.000-07:00</published><updated>2011-09-03T15:38:18.924-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='slides'/><category scheme='http://www.blogger.com/atom/ns#' term='scipy'/><category scheme='http://www.blogger.com/atom/ns#' term='lecture'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='scientific'/><category scheme='http://www.blogger.com/atom/ns#' term='scikit'/><category scheme='http://www.blogger.com/atom/ns#' term='numpy'/><category scheme='http://www.blogger.com/atom/ns#' term='machine learning'/><title type='text'>Keynote about Scientific Computing with Python, Scipy, Numpy and Matplotib</title><content type='html'>Hi all,&lt;br /&gt;&lt;br /&gt;I 'd like to share the slides from the keynote that I gave today at &lt;a href="http://pug-pe.python.org.br/encontro/xv/"&gt;XV Pernambuco Python User Group Meeting&lt;/a&gt; about Scientific Computing with Python. &amp;nbsp;This keynote presents how you can start in the scientific world using several tools in Python such as Matplotlib, Scipy and Numpy.&lt;br /&gt;&lt;br /&gt;&lt;div id="__ss_9123741" style="width: 425px;"&gt;&lt;strong style="display: block; margin: 12px 0 4px;"&gt;&lt;a href="http://www.slideshare.net/marcelcaraciolo/computao-cientfica-com-python-numpy-e-scipy" target="_blank" title="Computação Científica com Python, Numpy e Scipy"&gt;Computação Científica com Python, Numpy e Scipy&lt;/a&gt;&lt;/strong&gt; &lt;iframe frameborder="0" height="355" marginheight="0" marginwidth="0" scrolling="no" src="http://www.slideshare.net/slideshow/embed_code/9123741" width="425"&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;div style="padding: 5px 0 12px;"&gt;View more &lt;a href="http://www.slideshare.net/" target="_blank"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/marcelcaraciolo" target="_blank"&gt;Marcel Caraciolo&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;&lt;br /&gt;You can see it &lt;a href="http://www.slideshare.net/marcelcaraciolo/computao-cientfica-com-python-numpy-e-scipy"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;It gives an overview about how you can start and basic introduction for those tools. It is in portuguese, since it was for a brazilian audience.&lt;br /&gt;&lt;br /&gt;Any doubts, let me know.&lt;br /&gt;&
