<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SciPy Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/category/scipy/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/category/scipy/</link>
	<description></description>
	<lastBuildDate>Sun, 31 Mar 2024 07:21:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>SciPy Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/category/scipy/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Fit a Curve to Power-law Distributed Data in Python</title>
		<link>https://blog.finxter.com/fitting-a-curve-to-power-law-distributed-data-a-python-tutorial/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Sun, 31 Mar 2024 07:21:01 +0000</pubDate>
				<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<category><![CDATA[Statistics]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1669961</guid>

					<description><![CDATA[<p>In this tutorial, you&#8217;ll learn how to generate synthetic data that follows a power-law distribution, plot its cumulative distribution function (CDF), and fit a power-law curve to this CDF using Python. This process is useful for analyzing datasets that follow power-law distributions, which are common in natural and social phenomena. Prerequisites Ensure you have Python ... <a title="How to Fit a Curve to Power-law Distributed Data in Python" class="read-more" href="https://blog.finxter.com/fitting-a-curve-to-power-law-distributed-data-a-python-tutorial/" aria-label="Read more about How to Fit a Curve to Power-law Distributed Data in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/fitting-a-curve-to-power-law-distributed-data-a-python-tutorial/">How to Fit a Curve to Power-law Distributed Data in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this tutorial, you&#8217;ll learn how to generate synthetic data that follows a power-law distribution, plot its cumulative distribution function (CDF), and fit a power-law curve to this CDF using Python. This process is useful for analyzing datasets that follow power-law distributions, which are common in natural and social phenomena.</p>



<h2 class="wp-block-heading">Prerequisites</h2>



<p>Ensure you have Python installed, along with the <code>numpy</code>, <code>matplotlib</code>, and <code>scipy</code> libraries. If not, you can install them using pip:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install numpy matplotlib scipy</pre>



<h2 class="wp-block-heading">Step 1: Generate Power-law Distributed Data</h2>



<p>First, we&#8217;ll generate a dataset that follows a power-law distribution using <code>numpy</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

# Parameters
alpha = 3.0  # Exponent of the distribution
size = 1000  # Number of data points

# Generate power-law distributed data
data = np.random.power(a=alpha, size=size)</pre>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a href="https://blog.finxter.com/how-to-generate-and-plot-random-samples-from-a-power-law-distribution-in-python/">How to Generate and Plot Random Samples from a Power-Law Distribution in Python?</a></p>



<p>The data looks like this:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="921" height="594" src="https://blog.finxter.com/wp-content/uploads/2024/03/image-58.png" alt="" class="wp-image-1669962" srcset="https://blog.finxter.com/wp-content/uploads/2024/03/image-58.png 921w, https://blog.finxter.com/wp-content/uploads/2024/03/image-58-300x193.png 300w, https://blog.finxter.com/wp-content/uploads/2024/03/image-58-768x495.png 768w" sizes="(max-width: 921px) 100vw, 921px" /></figure>
</div>


<p>Let&#8217;s make some sense out of it and plot it in 2D space: <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4c8.png" alt="📈" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<h2 class="wp-block-heading">Step 2: Plot the Cumulative Distribution Function (CDF)</h2>



<p>Next, we&#8217;ll plot the CDF of the generated data on a log-log scale to visualize its power-law distribution.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import matplotlib.pyplot as plt

# Prepare data for the CDF plot
sorted_data = np.sort(data)
yvals = np.arange(1, len(sorted_data) + 1) / float(len(sorted_data))

# Plot the CDF
plt.plot(sorted_data, yvals, marker='.', linestyle='none', color='blue')
plt.xlabel('Value')
plt.ylabel('Cumulative Frequency')
plt.title('CDF of Power-law Distributed Data')
plt.xscale('log')
plt.yscale('log')
plt.grid(True, which="both", ls="--")
plt.show()</pre>



<p>The plot:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="578" height="459" src="https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-5.png" alt="" class="wp-image-1669964" srcset="https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-5.png 578w, https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-5-300x238.png 300w" sizes="(max-width: 578px) 100vw, 578px" /></figure>
</div>


<h2 class="wp-block-heading">Step 3: Fit a Power-law Curve to the CDF</h2>



<p>To understand the underlying power-law distribution better, we fit a curve to the CDF using the <code>curve_fit</code> function from <code>scipy.optimize</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.optimize import curve_fit

# Power-law fitting function
def power_law_fit(x, a, b):
    return a * np.power(x, b)

# Fit the power-law curve
params, covariance = curve_fit(power_law_fit, sorted_data, yvals)

# Generate fitted values
fitted_yvals = power_law_fit(sorted_data, *params)</pre>



<h2 class="wp-block-heading">Step 4: Plot the Fitted Curve with the CDF</h2>



<p>Finally, we&#8217;ll overlay the fitted power-law curve on the original CDF plot to visually assess the fit.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Plot the original CDF and the fitted power-law curve
plt.plot(sorted_data, yvals, marker='.', linestyle='none', color='blue', label='Original Data')
plt.plot(sorted_data, fitted_yvals, 'r-', label='Fitted Power-law Curve')
plt.xlabel('Value')
plt.ylabel('Cumulative Frequency')
plt.title('CDF with Fitted Power-law Curve')
plt.xscale('log')
plt.yscale('log')
plt.grid(True, which="both", ls="--")
plt.legend()
plt.show()</pre>



<p>Voilà! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="578" height="459" src="https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-6.png" alt="" class="wp-image-1669965" srcset="https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-6.png 578w, https://blog.finxter.com/wp-content/uploads/2024/03/Untitled-6-300x238.png 300w" sizes="(max-width: 578px) 100vw, 578px" /></figure>
</div>


<p>This visualization helps in assessing the accuracy of the power-law model in describing the distribution of the data. </p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Recommended article: </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="664" src="https://blog.finxter.com/wp-content/uploads/2024/03/8a9b7881-bc2d-4100-80df-5da295e73602-1536x996-1-1024x664.png" alt="" class="wp-image-1669966" srcset="https://blog.finxter.com/wp-content/uploads/2024/03/8a9b7881-bc2d-4100-80df-5da295e73602-1536x996-1-1024x664.png 1024w, https://blog.finxter.com/wp-content/uploads/2024/03/8a9b7881-bc2d-4100-80df-5da295e73602-1536x996-1-300x195.png 300w, https://blog.finxter.com/wp-content/uploads/2024/03/8a9b7881-bc2d-4100-80df-5da295e73602-1536x996-1-768x498.png 768w, https://blog.finxter.com/wp-content/uploads/2024/03/8a9b7881-bc2d-4100-80df-5da295e73602-1536x996-1.png 1536w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a href="https://blog.finxter.com/visualizing-wealth-plotting-the-net-worth-of-the-worlds-richest-in-log-log-space/">Visualizing Wealth: Plotting the Net Worth of the World’s Richest in Log/Log Space</a></p>
<p>The post <a href="https://blog.finxter.com/fitting-a-curve-to-power-law-distributed-data-a-python-tutorial/">How to Fit a Curve to Power-law Distributed Data in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Sample a Random Number from a Probability Distribution in Python</title>
		<link>https://blog.finxter.com/sample-a-random-number-from-a-probability-distribution-in-python/</link>
		
		<dc:creator><![CDATA[Shubham Sayon]]></dc:creator>
		<pubDate>Sun, 19 Jun 2022 05:28:58 +0000</pubDate>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=427747</guid>

					<description><![CDATA[<p>Problem Formulation Challenge: Given a list. How will you select a number randomly from the list using probability distribution? When you select a number randomly from a list using a given probability distribution, the output number generated will be a number returned based on the relative weights (probability) of the given numbers. Let&#8217;s try to ... <a title="Sample a Random Number from a Probability Distribution in Python" class="read-more" href="https://blog.finxter.com/sample-a-random-number-from-a-probability-distribution-in-python/" aria-label="Read more about Sample a Random Number from a Probability Distribution in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/sample-a-random-number-from-a-probability-distribution-in-python/">Sample a Random Number from a Probability Distribution in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading"><strong>Problem Formulation</strong></h2>



<p><strong>Challenge:</strong> Given a list. How will you select a number randomly from the list using probability distribution?</p>



<p>When you select a number randomly from a list using a given probability distribution, the output number generated will be a number returned based on the relative weights (probability) of the given numbers. Let&#8217;s try to visualize this with the help of an example.</p>



<p><strong>Example: </strong></p>



<pre class="wp-block-preformatted"><strong>Given:</strong>
numbers = [10, 20, 30]
distributions = [0.3, 0.2, 0.5]

<strong>Expected Output:</strong> Choose the elements randomly from the given list and display 5 elements in the output list: 
[30, 10, 20, 30, 30] 

<strong>Note: </strong>The output can vary.</pre>



<p id="block-28fa2b7c-4f79-452f-8e6f-941e0baf2d29">The expected output has the number &#8217;30&#8217; three times since it has the highest weight/probability. The relative weights assigned are 0.3, 0.2 and 0.5, respectively. This means: </p>



<ul class="wp-block-list"><li>Chances of selecting 10 are 30%.</li><li>Chances of selecting 20 are 20%.</li><li>Chances of selecting 30 are 50%.</li></ul>



<p><strong>Note: </strong>We will first have a look at the numerous ways of solving the given question and then dive into a couple of exercises for further clarity. So without further delay, let&#8217;s dive into our mission-critical question and solve it.</p>



<h2 class="wp-block-heading"><strong>Quick Video Explanation:</strong></h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Sample a Random Number from a Probability Distribution in Python" width="937" height="527" src="https://www.youtube.com/embed/l1ldnzYRhtM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading"><strong>Method 1:</strong> Using random.choices</h2>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="1024" height="393" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-131-1024x393.png" alt="" class="wp-image-428211" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-131-1024x393.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/06/image-131-300x115.png 300w, https://blog.finxter.com/wp-content/uploads/2022/06/image-131-768x294.png 768w, https://blog.finxter.com/wp-content/uploads/2022/06/image-131.png 1051w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<ul class="wp-block-list"><li><code>choices()</code>&nbsp;is a method of the <code>random</code> module in Python that returns a list containing randomly selected items from the specified sequence. This sequence can be a list, tuple,  string, or any other kind of sequence.</li><li>The possibility to pick weights can be specified using the <code>weights</code> or the <code>cum_weights</code> parameter. </li></ul>



<pre class="wp-block-preformatted"><strong>Syntax:</strong><br>random.choices(sequence, weights=None, cum_weights=None, k=1)</pre>



<figure class="wp-block-table is-style-stripes"><table><tbody><tr><th>Parameter</th><th>Description</th></tr><tr><td><em>sequence</em></td><td>&#8211; It is a mandatory parameter. <br>&#8211; Represents a sequence like a range of numbers, a list, a tuple, etc.</td></tr><tr><td><em>weights</em></td><td>&#8211; It is an optional parameter.<br>&#8211; Represents a list wherein the possibility for each value can be weighed. <br>&#8211; By default, it is None.</td></tr><tr><td><em>cum_weights</em></td><td>&#8211; It is an optional parameter.<br>&#8211; Represents a list where the possibility for each value can be weighed. However, the possibility, in this case, is accumulated. For example: normal weights: <code>[2, 3, 5]</code> is equivalent to the cum_weights: <code>[2, 5, 10]</code>.<br>&#8211; By default, it is None.</td></tr><tr><td><em>k</em></td><td>&#8211; It is an optional parameter.<br>&#8211; Represents an integer that determines the length of the returned list.</td></tr></tbody></table></figure>



<p><strong>Approach: </strong>Call the <code>random.choices()</code> function and feed in the given list and the weights/probability distributions as parameters. </p>



<p><strong>Code:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import random
numbers = [10, 20, 30]
distributions = [0.3, 0.2, 0.5]
random_number = random.choices(numbers, distributions, k=5)
print(random_number)</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>&#091;10, 30, 30, 10, 20]</code></pre>



<p><strong>Caution:</strong></p>



<ul class="wp-block-list"><li>If the relative or cumulative weight is not specified, then the <code>random.choices()</code> function&nbsp;will automatically select elements with equal probability.</li><li>The specified weights should always be of the same length as the specified sequence.</li><li>If you specify relative weights as well as cumulative weight at the same time, you will get a TypeError (<code>TypeError: Cannot specify both weights and cumulative weights</code>). Hence, to avoid the error,  do not specify both at the same time.</li><li>The <code>cum_weights</code> or <code>weights</code> can only be integers, floats, and fractions. They cannot be decimals. Also, you must ensure that the weights are non-negative.</li></ul>



<h2 class="wp-block-heading"><strong>Method 2:</strong> Using numpy.random.choice</h2>



<p>Another way to sample a random number from a probability distribution is to use the <code>numpy.random.choice()</code> function. </p>



<p><code>choice()</code>&nbsp;is a method of the <code>numpy.random</code> module that allows you to generate a random value based on a numpy array. It accepts an array as a parameter and randomly returns one of the values from the array. </p>



<pre class="wp-block-preformatted"><strong>Syntax:</strong>
numpy.random.choice(arr, k, p)</pre>



<figure class="wp-block-table is-style-stripes"><table><tbody><tr><th>Parameter</th><th>Description</th></tr><tr><td><em>arr</em></td><td>&#8211; Represents the array containing the sequence of random numbers.</td></tr><tr><td><em>k</em></td><td>&#8211; Represents an integer that determines the length of the returned list.</td></tr><tr><td><em>p</em></td><td>&#8211; Represents a list where the possibility for each value can be weighed. In simple words, it is the probability distribution of each value of the given array.</td></tr></tbody></table></figure>



<p><strong>Approach: </strong>Use the&nbsp;<code>numpy.random.choice(li, size, replace, weights)</code>&nbsp;function such that&nbsp;<code>replace</code>&nbsp;is set to&nbsp;<code>True</code>&nbsp;to return a list of the required&nbsp;<code>size</code> from the list&nbsp;<code>li</code>&nbsp;with respect to a list of corresponding weight sequences&nbsp;<code>weights</code>.</p>



<p><strong>Code:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
numbers = [10, 20, 30]
distributions = [0.3, 0.2, 0.5]
random_number = np.random.choice(numbers, 5, True, distributions)
print(random_number)</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>&#091;30 20 30 10 30]</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity" />



<p><strong>Do you want to become a NumPy master?</strong> Check out our interactive puzzle book <a href="https://amzn.to/39dEykm" target="_blank" rel="noreferrer noopener" title="https://amzn.to/39dEykm"><strong>Coffee Break NumPy</strong></a> and boost your data science skills! <em>(Amazon link opens in new tab.)</em></p>



<div class="wp-block-image"><figure class="aligncenter size-medium"><a href="https://amzn.to/39dEykm" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" width="200" height="300" src="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg" alt="Coffee Break NumPy" class="wp-image-2766"/></a></figure></div>



<h2 class="wp-block-heading"><strong>Method 3: Using Scipy</strong></h2>



<p><code>Scipy</code> is another hand library to deal with random weighted distributions. </p>



<ul class="wp-block-list"><li><a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.html#scipy.stats.rv_discrete"><code>rv_discrete</code></a>&nbsp;is a base class that is used to construct specific distribution instances and classes for discrete random variables. It is also used to construct an arbitrary distribution defined by a list of support points and corresponding probabilities. [source: <a rel="noreferrer noopener" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.html#scipy.stats.rv_discrete" target="_blank">Official Documentation</a>]</li></ul>



<p><strong>Explanation: </strong>In the following code snippet <code>rv_discrete()</code> takes the sequence of integer values that are contained in the list <code>numbers</code> as the first argument and the probability distributions/weights as the second argument and returns random values from the list based on their relative weigths/probability ditributions. </p>



<p><strong>Code:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.stats import rv_discrete
numbers = [10, 20, 30]
distributions = [0.3, 0.2, 0.5]
d = rv_discrete(values=(numbers, distributions))
print(d.rvs(size=5))</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>&#091;30 10 30 30 20]</code></pre>



<h2 class="wp-block-heading"><strong>Method 4: Using <a href="https://pypi.org/project/lea/" target="_blank" rel="noreferrer noopener">Lea</a></strong></h2>



<p>Another effective Python library that helps us to work with probability distributions is <strong>Lea</strong>. It is specifically designed to facilitate you to model a wide range of random phenomenons, like coin tossing, gambling, It allows you to model a broad range of random phenomenons, like dice throwing, coin tossing, gambling results, weather forecast, finance, etc.&nbsp;</p>



<p><strong>#Note: </strong>Since <code>lea</code> is an external library, you must install it before using it. Here&#8217;s the command to install <code>lea</code> in your system: <code>pip install lea</code></p>



<p><strong>Code:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import lea

numbers = [10, 20, 30]
distributions = [0.3, 0.2, 0.5]
d = tuple(zip(numbers, distributions))
print(lea.pmf(d).random(5))</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>(30, 30, 30, 10, 20)</code></pre>



<h2 class="wp-block-heading"><strong>Exercises</strong></h2>



<p><strong>Question 1: </strong> Our friend Harry has eight coloured crayons: [&#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;, &#8220;yellow&#8221;, &#8220;black&#8221;, &#8220;white&#8221;, &#8220;pink&#8221;, &#8220;orange&#8221;]. Harry has the weighted preference for selecting each color as: [1/24, 1/6, 1/6, 1/12, 1/12, 1/24, 1/8, 7/24]. He is only allowed to select three colors at once. Find the various combinations he can select in 10 attempts.</p>



<p><strong>Solution:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import random
colors = ["red", "green", "blue", "yellow", "black", "white", "pink", "orange"]
distributions = [1/24, 1/6, 1/6, 1/12, 1/12, 1/24, 1/8, 7/24]
for i in range(10):
    choices = random.choices(colors, distributions, k=3)
    print(choices)</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>&#091;'orange', 'pink', 'green']
&#091;'blue', 'yellow', 'yellow']
&#091;'orange', 'green', 'black']
&#091;'blue', 'red', 'blue']
&#091;'orange', 'orange', 'red']
&#091;'orange', 'green', 'blue']
&#091;'orange', 'black', 'blue']
&#091;'black', 'yellow', 'green']
&#091;'pink', 'orange', 'orange']
&#091;'blue', 'blue', 'white']</code></pre>



<p><strong>Question 2:</strong> </p>



<pre class="wp-block-preformatted"><strong>Given:</strong>
cities = ["Frankfurt", "Stuttgart", "Freiburg", "München", "Zürich", "Hamburg"]
populations = [736000, 628000, 228000, 1450000, 409241, 1841179]

The probability of a particular city being chosen depends on its population. Thus, larger the population of a city, higher the probability of the city being chosen. Based on this condition, find the probability distribution of the cities and display the city that might be selected in 10 attempts. </pre>



<p>  <strong>Solution:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import random
cities = ["Frankfurt", "Stuttgart", "Freiburg", "München", "Zürich", "Hamburg"]
populations = [736000, 628000, 228000, 1450000, 409241, 1841179]
distributions = [round(pop / sum(populations), 2) for pop in populations]
print(distributions)
for i in range(10):
    print(random.choices(cities, distributions)[0])</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>&#091;0.14, 0.12, 0.04, 0.27, 0.08, 0.35]
Freiburg
Frankfurt
Zürich
Hamburg
Stuttgart
Frankfurt
München
Frankfurt
München
München</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity" />



<p>With that we come to the end of this tutorial. I hope it has helped you. Please <strong><a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener">subscribe</a></strong> and stay tuned for more interesting tutorials and solutions. Happy learning! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<p class="has-background" style="background-color:#b1fcac"><strong>Recommended Read: <a href="https://blog.finxter.com/python-random-module/" target="_blank" rel="noreferrer noopener">Python’s Random Module – Everything You Need to Know to Get Started</a></strong></p>
<p>The post <a href="https://blog.finxter.com/sample-a-random-number-from-a-probability-distribution-in-python/">Sample a Random Number from a Probability Distribution in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Factorials &#8211; NumPy, Scipy, Math, Python</title>
		<link>https://blog.finxter.com/the-factorial-function-competition-numpy-scipy-math-and-python/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Sun, 05 Jun 2022 10:07:00 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=5505</guid>

					<description><![CDATA[<p>Factorial Definition and Example 💡 The factorial function n! calculates the number of permutations in a set. Say you want to rank three soccer teams Manchester United, FC Barcelona, and FC Bayern München &#8212; how many possible rankings exist? The answer is 3! = 3 x 2 x 1 = 6. Practical Example: Say, there ... <a title="Factorials &#8211; NumPy, Scipy, Math, Python" class="read-more" href="https://blog.finxter.com/the-factorial-function-competition-numpy-scipy-math-and-python/" aria-label="Read more about Factorials &#8211; NumPy, Scipy, Math, Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/the-factorial-function-competition-numpy-scipy-math-and-python/">Factorials &#8211; NumPy, Scipy, Math, Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Factorial Definition and Example</h2>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The factorial function <code>n!</code> calculates the number of permutations in a set. </p>



<p>Say you want to rank three soccer teams <strong>Manchester United</strong>, <strong>FC Barcelona</strong>, and <strong>FC Bayern München</strong> &#8212; how many possible rankings exist? </p>



<p>The answer is <code>3! = 3 x 2 x 1 = 6</code>. </p>



<p><strong>Practical Example</strong>: Say, there are 20 football teams in England’s premier league. Each team can possibly reach any of the 20 ranks at the end of the season. <em>How many possible rankings exist in the premier league, given 20 fixed teams?</em></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="604" height="423" src="https://blog.finxter.com/wp-content/uploads/2019/06/image-6.png" alt="" class="wp-image-3679" srcset="https://blog.finxter.com/wp-content/uploads/2019/06/image-6.png 604w, https://blog.finxter.com/wp-content/uploads/2019/06/image-6-300x210.png 300w, https://blog.finxter.com/wp-content/uploads/2019/06/image-6-100x70.png 100w" sizes="auto, (max-width: 604px) 100vw, 604px" /></figure>



<p><em><strong>Figure: Example of three possible rankings of the football teams in England’s premier league.</strong></em></p>



<p></p>



<p>In general, to calculate the <a rel="noreferrer noopener" title="A Simple Python Factorial Program Using Recursion" href="https://blog.finxter.com/a-simple-python-factorial-program-using-recursion/" target="_blank">factorial </a><code>n!</code>, you need to multiply all positive integer numbers that are smaller or equal to <code>n</code>. </p>



<p>For example, if you have 5 soccer teams, there are <code>5! = 5 x 4 x 3 x 2 x 1 = 120</code> different pairings.</p>



<p>There are many different ways to calculate the factorial function in Python easily, see alternatives below. </p>



<p>Feel free to watch my explainer video as you go through the article:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="The Factorial Function Competition: NumPy, Scipy, Math, and Python" width="937" height="527" src="https://www.youtube.com/embed/kberqt5NzSg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">How to Calculate the Factorial in NumPy?</h2>



<p class="has-base-background-color has-background">NumPy&#8217;s <a href="https://docs.scipy.org/doc/numpy/reference/c-api.coremath.html?highlight=math#numpy-core-math-library">math module</a> contains efficient implementations of basic math functions such as the factorial function <code>numpy.math.factorial(n)</code>. </p>



<p>Here&#8217;s an example of how to calculate the factorial <code>3!</code> with <a href="https://blog.finxter.com/numpy-tutorial/" target="_blank" rel="noreferrer noopener" title="NumPy Tutorial – Everything You Need to Know to Get Started">NumPy</a>: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import numpy as np
>>> np.math.factorial(3)
6</pre>



<p>The factorial function in NumPy has only one integer argument <code>n</code>. If the argument is negative or not an integer, Python will raise a value error. </p>



<p>Here&#8217;s how you can calculate this in Python for 3 teams:</p>



<iframe loading="lazy" src="https://repl.it/@finxter/numpyfactorial?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="700px" frameborder="no"></iframe>



<p><em><strong>Exercise</strong>: Modify the code to calculate the number of rankings for 20 teams!</em></p>



<h2 class="wp-block-heading">How to Calculate the Factorial in Scipy?</h2>



<p>The popular <code><a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" data-type="post" data-id="22420" target="_blank" rel="noreferrer noopener">scipy</a></code> library is a collection of libraries and modules that help you with scientific computing. </p>



<p class="has-base-background-color has-background">Scipy contains a powerful collection of functionality&#8212;built upon the <a href="https://blog.finxter.com/numpy-tutorial/">NumPy library</a>. Thus, it doesn&#8217;t surprise that the SciPy factorial function <code>scipy.math.factorial()</code> is actually a reference to NumPy&#8217;s factorial function <code>numpy.math.factorial()</code>. </p>



<p>In fact, if you compare their memory addresses using the <a rel="noreferrer noopener" title="Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know" href="https://blog.finxter.com/python-cheat-sheet/" target="_blank">keyword </a><code>is</code>, it turns out that both refer to the same function <a rel="noreferrer noopener" title="[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming" href="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/" target="_blank">object</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import scipy, numpy
>>> scipy.math.factorial(3)
6
>>> numpy.math.factorial(3)
6
>>> scipy.math.factorial is numpy.math.factorial
True</pre>



<p>So you can use both <code>scipy.math.factorial(3)</code> and <code>numpy.math.factorial(3)</code> to compute the factorial function <code>3!</code>.</p>



<p>As both functions point to the same object, the performance characteristics are the same &#8212; one is not faster than the other one.</p>



<p>Let&#8217;s have a look at <code>math.factorial()</code> &#8212; the mother of all factorial functions. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Check out my new Python book <a title="https://amzn.to/2WAYeJE" href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener">Python One-Liners</a></strong> (Amazon Link).</p>



<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it&#8217;s also an <strong>introduction to computer science</strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!</em></strong></p>


<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" width="215" height="283" src="https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png" alt="" class="wp-image-5969"/></a></figure>
</div>


<p>The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>



<p><strong>Publisher Link</strong>: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>



<h2 class="wp-block-heading">How to Calculate the Factorial in Python&#8217;s Math Library?</h2>



<p>As it turns out, not only NumPy and Scipy come with a packaged &#8220;implementation&#8221; of the factorial function, but also Python&#8217;s powerful <a rel="noreferrer noopener" href="https://docs.python.org/3/library/math.html" target="_blank">math library</a>. </p>



<p class="has-global-color-8-background-color has-background">You can use the <code><a href="https://blog.finxter.com/python-math-factorial-2/" data-type="post" data-id="38038" target="_blank" rel="noreferrer noopener">math.factorial(n)</a></code> function to compute the factorial <code>n!</code>. </p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import math
>>> math.factorial(3)
6</pre>



<p><em>The factorial of 3 is 6 &#8212; nothing new here.</em></p>



<p>Let&#8217;s check whether this is actually the same implementation as NumPy&#8217;s and Scipy&#8217;s factorial functions:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import scipy, numpy, math
>>> scipy.math.factorial is math.factorial
True
>>> numpy.math.factorial is math.factorial
True</pre>



<p>Ha! Both libraries NumPy and Scipy rely on the same factorial function of the <a rel="noreferrer noopener" title="Python Math Module [Ultimate Guide]" href="https://blog.finxter.com/python-math-module/" target="_blank">math library</a>. </p>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Hence, to <a rel="noreferrer noopener" title="Python One Line X" href="https://blog.finxter.com/python-one-line-x/" target="_blank">save valuable space </a>in your code, use the <code>math</code> factorial function if you have already imported the <code>math</code> library. If not, just use the NumPy or Scipy factorial function aliases.</p>



<p>So up &#8217;till now we&#8217;ve seen the same old wine in three different bottles: NumPy, Scipy, and math libraries all refer to the same factorial function implementation.</p>



<h2 class="wp-block-heading">How to Calculate the Factorial in Python?</h2>



<p>It&#8217;s often a good idea to implement a function by yourself. This will help you understand the underlying details better and gives you confidence and expertise. </p>



<p>So let&#8217;s implement the factorial function in Python!</p>



<p>To calculate the number of permutations of a given set of <code>n</code> elements, you use the factorial function <code>n!</code>. The factorial is defined as follows:</p>



<p><em>n! = n × (n – 1) × ( n – 2) × . . . &nbsp;× 1</em></p>



<p>For example:</p>



<ul class="wp-block-list"><li><em>1! = 1</em></li><li><em>3! = 3 × 2 × 1 = 6</em></li><li><em>10! = 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 3,628,800</em></li><li><em>20! = 20 × 19 × 18 × . . . &nbsp;× 3 × 2 × 1 = 2,432,902,008,176,640,000</em></li></ul>



<p><a href="https://blog.finxter.com/recursion/" target="_blank" rel="noreferrer noopener" title="Recursion: A Helpful Guide in Python">Recursively</a>, the factorial function can also be defined as follows:</p>



<p><em>n! = n × (n – 1)!</em></p>



<p>The recursion base cases are defined as shown here:</p>



<p><em>1! = 0! = 1</em></p>



<p>The intuition behind these base cases is that a set with one element has one permutation, and a set with zero elements has one permutation (there is one way of assigning zero elements to zero buckets). </p>



<p>Now, we can use this recursive definition to calculate the factorial function in a <a href="https://blog.finxter.com/python-one-line-recursion/" target="_blank" rel="noreferrer noopener" title="Python One Line Recursion">recursive manner</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> factorial = lambda n: n * factorial(n-1) if n > 1 else 1
>>> factorial(3)
6</pre>



<p><strong>Try It Yourself</strong>: Run this one-liner in our interactive code shell:</p>



<iframe loading="lazy" src="https://repl.it/@finxter/factorial?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="400px" frameborder="no"></iframe>



<p><em><strong>Exercise</strong>: What&#8217;s the output? </em></p>



<p>The <code>lambda</code> keyword is used to define an anonymous function in a single line. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f30d.png" alt="🌍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Learning Resource</strong>: You can learn everything you need to know about the lambda function in <a href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/">this comprehensive tutorial</a> on the Finxter blog.</p>



<p><em>If you love one-liners like I do, check out my book <a href="https://pythononeliners.com/" data-type="URL" data-id="https://pythononeliners.com/" target="_blank" rel="noreferrer noopener">&#8220;Python One-Liners&#8221;</a> which will teach you everything there is to learn about a single line of Python code!</em></p>



<p>You create a lambda function with one argument <code>n</code> and assign the lambda function to the name <code>factorial</code>. Finally, you call the named function <code>factorial(n-1)</code> to calculate the result of the function call <code>factorial(n)</code>.</p>



<p>Roughly speaking, you can use the simpler solution for <code>factorial(n-1)</code> to construct the solution of the harder problem <code>factorial(n)</code> by multiplying the former with the input argument <code>n</code>. </p>



<p>As soon as you reach the recursion base case <code>n &lt;= 1</code>, you simply return the hard-coded solution <code>factorial(1) = factorial(0) = 1</code>.</p>



<p>An alternative is to use the iterative computation like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def factorial(n):
    fac = n
    for i in range(1, n):
        fac *= i
    return fac

print(factorial(3))
# 6

print(factorial(5))
# 120</pre>



<p>In the function <code>factorial(n)</code>, we initialize the variable <code>fac</code> to the value <code>n</code>. Then, we iterate over all values <code>i</code> between 1 and n-1 (inclusive) and multiply them with the value currently stored in the variable <code>fac</code>. The result is the factorial of the integer value <code>n</code>. </p>



<h2 class="wp-block-heading">Speed Comparison</h2>



<p>Let&#8217;s compare all three different ways to calculate the factorial function regarding <a rel="noreferrer noopener" title="Python cProfile – 7 Strategies to Speed Up Your App" href="https://blog.finxter.com/python-profilers-how-to-speed-up-your-python-app/" target="_blank">speed</a>. </p>



<p>Note that the NumPy, Scipy, and math factorial functions are referencing the same function object&#8212;they have the same speed properties. </p>



<p>Thus, we only compare the <code>math.factorial()</code> function with our two implementations in Python (recursive and iterative). </p>



<p><strong><em>Want to take a guess first?</em></strong></p>



<p>I used my own notebook computer (Quadcore, Intel Core i7, 8th Generation) with Python 3.7 to run 900 factorial computations for each method using the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time

num_runs = 900
speed = []


## SPEED TEST MATH.FACTORIAL ##
import math


start = time.time()
for i in range(num_runs):
    math.factorial(i)
stop = time.time()

speed.append(stop-start)

    
## SPEED TEST RECURSIVE ##
factorial = lambda n: n * factorial(n-1) if n > 1 else 1

start = time.time()
for i in range(num_runs):
    factorial(i)
stop = time.time()

speed.append(stop-start)

    
## SPEED TEST ITERATIVE ##
def factorial(n):
    fac = n
    for i in range(1, n):
        fac *= i
    return fac


start = time.time()
for i in range(num_runs):
    factorial(i)
stop = time.time()

speed.append(stop-start)


## RESULT
print(speed)
# [0.011027336120605469, 0.10074210166931152, 0.0559844970703125]
import matplotlib.pyplot as plt
plt.bar(["Math", "Recursive", "Iterative"], height=speed)
plt.show()</pre>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="640" height="480" src="https://blog.finxter.com/wp-content/uploads/2019/12/factorial.jpg" alt="" class="wp-image-5508" srcset="https://blog.finxter.com/wp-content/uploads/2019/12/factorial.jpg 640w, https://blog.finxter.com/wp-content/uploads/2019/12/factorial-300x225.jpg 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></figure>
</div>


<p>Wow&#8212;the clear winner is the <code>math</code> module! A clear sign that you should always prefer library code over your own implementations! </p>



<p>The <code>math</code> library&#8217;s implementation is almost 600% faster than the iterative one and 1000% faster than the recursive implementation. </p>



<figure class="wp-block-table is-style-stripes"><table><tbody><tr><td><strong>Method</strong></td><td><code>math.factorial</code></td><td>Recursive</td><td>Iterative</td></tr><tr><td><strong>Seconds</strong></td><td>0.01</td><td>0.10</td><td>0.05</td></tr></tbody></table></figure>



<p><strong>Try It Yourself</strong>: You can perform this speed comparison yourself in the interactive code shell:</p>



<iframe loading="lazy" src="https://repl.it/@finxter/speedFactorial?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="500px" frameborder="no"></iframe>



<p><em><strong>Exercise</strong>: Do you receive similar results in your browser? Run the shell to find out!</em></p>



<h2 class="wp-block-heading">Where to Go From Here</h2>



<p>The three library implementations <code>numpy.math.factorial()</code>, <code>scipy.math.factorial()</code>, and <code>math.factorial()</code> point to the same function object in memory&#8212;they are identical so use any of them. </p>



<p>One a higher-level, you&#8217;ve learned that library implementations of popular libraries such as NumPy are blazingly fast and efficient. Do yourself a favor and use library implementations wherever possible.</p>



<p>A good place to start is the NumPy library which is the basis of many more advanced data science and machine learning libraries in Python such as matplotlib, pandas, tensorflow, and scikit-learn. Learning NumPy will set the foundation on which you can build your Python career. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f30d.png" alt="🌍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Tutorial</strong>: <a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356">NumPy &#8212; Everything you need to know to get started</a></p>



<h2 class="wp-block-heading">Programmer Humor</h2>



<pre class="wp-block-preformatted has-global-color-8-background-color has-background"><code><strong>Q</strong>: How do you tell an introverted computer scientist from an extroverted computer scientist?

<strong>A</strong>: An extroverted computer scientist looks at <strong><em>your</em></strong> shoes when he talks to you.</code></pre>



<p>The post <a href="https://blog.finxter.com/the-factorial-function-competition-numpy-scipy-math-and-python/">Factorials &#8211; NumPy, Scipy, Math, Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Normal Distribution and Shapiro-Wilk Test in Python</title>
		<link>https://blog.finxter.com/normal-distribution-and-shapiro-wilk-test-in-python/</link>
		
		<dc:creator><![CDATA[Rebecca Nowack]]></dc:creator>
		<pubDate>Sat, 04 Jun 2022 07:05:42 +0000</pubDate>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<category><![CDATA[Seaborn]]></category>
		<category><![CDATA[Statistics]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=405419</guid>

					<description><![CDATA[<p>Normal distribution is a statistical prerequisite for parametric tests like Pearson’s correlation, t-tests, and regression. Testing for normal distribution can be done visually with sns.displot(x, kde=true). The Shapiro-Wilk test for normality can be done quickest with pingouin&#8216;s pg.normality(x). 💡 Note: Several publications note that normal distribution is the least important prerequisite for parametric tests and ... <a title="Normal Distribution and Shapiro-Wilk Test in Python" class="read-more" href="https://blog.finxter.com/normal-distribution-and-shapiro-wilk-test-in-python/" aria-label="Read more about Normal Distribution and Shapiro-Wilk Test in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/normal-distribution-and-shapiro-wilk-test-in-python/">Normal Distribution and Shapiro-Wilk Test in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Normal distribution is a statistical prerequisite for parametric tests like <a href="https://blog.finxter.com/pearson-correlation-in-python/" data-type="post" data-id="405390" target="_blank" rel="noreferrer noopener">Pearson’s correlation</a>, t-tests, and regression. </p>



<ul class="wp-block-list"><li>Testing for normal distribution can be done visually with <code>sns.displot(x, kde=true)</code>. </li><li>The Shapiro-Wilk test for normality can be done quickest with <code>pingouin</code>&#8216;s <code>pg.normality(x)</code>.</li></ul>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Several publications note that normal distribution is the least important prerequisite for parametric tests and with large sample sizes you can assume normal distribution. Check <a href="https://brieflands.com/articles/ijem-71904.html" data-type="URL" data-id="https://brieflands.com/articles/ijem-71904.html" target="_blank" rel="noreferrer noopener">this paper</a> for more details.</p>



<h2 class="wp-block-heading">Python Libraries for Normal Distribution and Shapiro-Wilk</h2>



<p>We import pingouin, seaborn and SciPy. <a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" data-type="post" data-id="22420" target="_blank" rel="noreferrer noopener">SciPy</a> is the standard package for statistical tests and <code>pingouin</code> is a package for quick one-line statistical tests.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
import pingouin as pg
import seaborn as sns
import scipy as scipy
</pre>



<h2 class="wp-block-heading">Method 1: Seaborn</h2>



<p>We load the dataset about different species and sizes of penguins from seaborn.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">penguins = sns.load_dataset('penguins')
penguins.head() </pre>



<p></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="709" height="198" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-45.png" alt="" class="wp-image-405427" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-45.png 709w, https://blog.finxter.com/wp-content/uploads/2022/06/image-45-300x84.png 300w" sizes="auto, (max-width: 709px) 100vw, 709px" /></figure>
</div>


<p>We’ll check out the bill length of the penguins more closely. With Seaborn, we can plot a distribution curve over our data. </p>



<p>A normal distribution will have the shape of the gaussian curve. That is why a distribution plot is a great way to determine normal distribution visually as it can be seen right away if it is a bell curve or not.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sns.displot(penguins["bill_length_mm"], kde=True)</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="590" height="587" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-44.png" alt="" class="wp-image-405426" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-44.png 590w, https://blog.finxter.com/wp-content/uploads/2022/06/image-44-300x298.png 300w, https://blog.finxter.com/wp-content/uploads/2022/06/image-44-150x150.png 150w" sizes="auto, (max-width: 590px) 100vw, 590px" /></figure>
</div>


<p>This curve does not look normally distributed, but close.</p>



<p>The <strong>Shapiro-Wilk test</strong> is a test for normal distribution and can confirm our assumption. </p>



<p>The hypothesis for the test are:</p>



<ul class="wp-block-list"><li><strong>H0</strong>: Our data is normally distributed.</li><li><strong>H1</strong>: Our data is not normally distributed.</li></ul>



<p>If the test is significant, we’ll have to reject H0, meaning that we assume H1 is true, and the data is not normally distributed.&nbsp;</p>



<h2 class="wp-block-heading">Method 2: Shapiro-Wilk Test with Pingouin</h2>



<p class="has-base-background-color has-background">With the package <code>pingouin</code>, we can have a quick test output. For instance, the function call <code>pg.normality(x)</code> will give us the results of the Shapiro-Wilk test while automatically dropping missing values.</p>



<p>Here&#8217;s an example for testing normality on the <code>penguins</code> dataset previously instantiated:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pg.normality(penguins["bill_length_mm"])</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="333" height="75" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-43.png" alt="" class="wp-image-405425" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-43.png 333w, https://blog.finxter.com/wp-content/uploads/2022/06/image-43-300x68.png 300w" sizes="auto, (max-width: 333px) 100vw, 333px" /></figure>
</div>


<p>The p-value is significant, so we will reject the H0 assumption that our data is normally distributed and confirm our visual assumption of non-normal distribution.</p>



<h2 class="wp-block-heading">Method 3: Shapiro-Wilk Test in SciPy</h2>



<p>The Shapiro-Wilk test can also be done with <code>scipy.stats.shapiro(x)</code>. However, SciPy does not automatically drop missing values so the test will be invalid. Therefore, we must drop them beforehand.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">bill_length = penguins["bill_length_mm"].dropna()
scipy.stats.shapiro(bill_length)</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="609" height="42" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-42.png" alt="" class="wp-image-405424" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-42.png 609w, https://blog.finxter.com/wp-content/uploads/2022/06/image-42-300x21.png 300w" sizes="auto, (max-width: 609px) 100vw, 609px" /></figure>
</div>


<p>This delivers the same results and confirms our assumption of a not normally distributed variable.</p>



<h2 class="wp-block-heading">Normal Distribution on the Iris Dataset</h2>



<p>A normal distributed variable would look more like the sepal width from the iris dataset:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">iris = sns.load_dataset('iris')
sns.displot(iris["sepal_width"], kde=True)</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="565" height="589" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-41.png" alt="" class="wp-image-405423" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-41.png 565w, https://blog.finxter.com/wp-content/uploads/2022/06/image-41-288x300.png 288w" sizes="auto, (max-width: 565px) 100vw, 565px" /></figure>
</div>


<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pg.normality(iris["sepal_width"])</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="316" height="68" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-40.png" alt="" class="wp-image-405422" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-40.png 316w, https://blog.finxter.com/wp-content/uploads/2022/06/image-40-300x65.png 300w" sizes="auto, (max-width: 316px) 100vw, 316px" /></figure>
</div>


<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">scipy.stats.shapiro(iris["sepal_width"])</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="580" height="46" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-39.png" alt="" class="wp-image-405421" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-39.png 580w, https://blog.finxter.com/wp-content/uploads/2022/06/image-39-300x24.png 300w" sizes="auto, (max-width: 580px) 100vw, 580px" /></figure>
</div>


<p>Here, the Shapiro-Wilk test is not significant, so we assume H0 is correct and the data normally distributed.</p>



<p>If you want to apply parametric tests to your data like a Pearson regression you mostly still can, as normal distribution is not a hard prerequisite and large datasets tend to be normally distributed.&nbsp;</p>



<p>You can also z-transform and normalize your data so the values have the same mean and standard deviation. This is especially useful for machine learning algorithms.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Programmer Humor</h2>



<pre class="wp-block-preformatted has-global-color-8-background-color has-background"><code><strong>Q</strong>: How do you tell an introverted computer scientist from an extroverted computer scientist?

<strong>A</strong>: An extroverted computer scientist looks at <strong><em>your</em></strong> shoes when he talks to you.</code></pre>



<p>The post <a href="https://blog.finxter.com/normal-distribution-and-shapiro-wilk-test-in-python/">Normal Distribution and Shapiro-Wilk Test in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Pearson Correlation in Python</title>
		<link>https://blog.finxter.com/pearson-correlation-in-python/</link>
		
		<dc:creator><![CDATA[Rebecca Nowack]]></dc:creator>
		<pubDate>Sat, 04 Jun 2022 06:36:21 +0000</pubDate>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Pandas Library]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<category><![CDATA[Statistics]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=405390</guid>

					<description><![CDATA[<p>A good solution to calculate Pearson’s r and the p-value, to report the significance of the correlation, in Python is scipy.stats.pearsonr(x, y). A nice overview of the results delivers pingouin’s pg.corr(x, y).  What is Pearson&#8217;s &#8220;r&#8221; Measure? A statistical correlation with Pearson’s r measures the linear relationship between two numerical variables. The correlation coefficient r ... <a title="Pearson Correlation in Python" class="read-more" href="https://blog.finxter.com/pearson-correlation-in-python/" aria-label="Read more about Pearson Correlation in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/pearson-correlation-in-python/">Pearson Correlation in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-global-color-8-background-color has-background">A good solution to calculate Pearson’s r and the p-value, to report the significance of the correlation, in Python is <code>scipy.stats.pearsonr(x, y)</code>. A nice overview of the results delivers <code>pingouin</code>’s <code>pg.corr(x, y)</code>. </p>



<h2 class="wp-block-heading">What is Pearson&#8217;s &#8220;r&#8221; Measure?</h2>



<p>A statistical correlation with Pearson’s <strong>r</strong> measures the linear relationship between two numerical variables. </p>



<p>The correlation coefficient <strong>r</strong> tells us how the values lie on a descending or ascending line. <strong>r</strong> can take on values between 1 (positive correlation) and -1 (negative correlation) and 0 would be no correlation. </p>



<p>The prerequisite for the Pearson correlation is the normal distribution and metric data (e.g., measurements of height, distance, income, or age). </p>



<p>For categorical data you should use the <a href="https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient" data-type="URL" data-id="https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient" target="_blank" rel="noreferrer noopener">Spearman Rho rank correlation</a>. </p>



<p>However, the normal distribution is the least important prerequisite, and for larger datasets, parametric tests are robust so they can still be used. Larger datasets tend to be normally distributed but normality tests are sensitive to minor changes and reject the notion of normality on large datasets.</p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Be aware not to mix <em>causality</em> and <em>correlation</em>. Two variables that correlate do not necessarily have a causal relationship. It could be a third variable missing that explains the correlation or it is just by chance. This is called a <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Spurious_relationship" data-type="URL" data-id="https://en.wikipedia.org/wiki/Spurious_relationship" target="_blank">spurious relationship</a>.</p>



<h2 class="wp-block-heading">Python Libraries to Calculate Correlation Coefficient &#8220;r&#8221;</h2>



<p>We will calculate the correlation coefficient <strong>r</strong> with several packages on the iris dataset. </p>



<p>First, we load the necessary packages.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
import numpy as np
import pingouin as pg
import seaborn as sns
import scipy as scipy
</pre>



<p></p>



<h2 class="wp-block-heading">Pearson Correlation in Seaborn</h2>



<p>Many packages have built-in datasets. You can import <code>iris</code> from <a href="https://blog.finxter.com/best-matplotlib-cheat-sheet/" data-type="post" data-id="22273" target="_blank" rel="noreferrer noopener">Seaborn</a>. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">iris = sns.load_dataset('iris')
iris.head()</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="484" height="201" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-32.png" alt="" class="wp-image-405391" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-32.png 484w, https://blog.finxter.com/wp-content/uploads/2022/06/image-32-300x125.png 300w" sizes="auto, (max-width: 484px) 100vw, 484px" /></figure>
</div>


<p>With seaborn’s <code>sns.heatmap()</code> we can get a quick correlation matrix if we pass <code>df.corr()</code> into the function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sns.heatmap(iris.corr())</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="492" height="396" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-33.png" alt="" class="wp-image-405392" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-33.png 492w, https://blog.finxter.com/wp-content/uploads/2022/06/image-33-300x241.png 300w" sizes="auto, (max-width: 492px) 100vw, 492px" /></figure>
</div>


<p>This tells us that we have a high correlation between petal length and petal width, so we will test these variables separately. </p>



<p>First, we inspect the two variables with a seaborn <code><a href="https://blog.finxter.com/how-to-change-the-figure-size-for-a-seaborn-plot/" data-type="post" data-id="33627" target="_blank" rel="noreferrer noopener">sns.scatterplot()</a></code> to visually determine a linear relationship.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sns.scatterplot(data=iris, x="petal_length", y="petal_width")</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="465" height="348" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-34.png" alt="" class="wp-image-405393" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-34.png 465w, https://blog.finxter.com/wp-content/uploads/2022/06/image-34-300x225.png 300w" sizes="auto, (max-width: 465px) 100vw, 465px" /></figure>
</div>


<p>There is a clear linear relationship so we go on calculating our correlation coefficient.</p>



<h2 class="wp-block-heading">Pearson Correlation in NumPy</h2>



<p>NumPy<strong> </strong>will deliver the correlation coefficient Pearson’s <strong>r</strong> with <code>np.corrcoef(x, y)</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">np.corrcoef(iris["petal_length"], iris["petal_width"])</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="279" height="53" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-35.png" alt="" class="wp-image-405394"/></figure>
</div>


<h2 class="wp-block-heading">Pearson Correlation in Pandas</h2>



<p>Pandas also has a correlation function. With <code>df.corr()</code> you can get a correlation matrix for the whole dataframe. Or you can test the correlation between two variables with <code>x.corr(y)</code> like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">iris["petal_length"].corr(iris["petal_width"])</pre>



<p>Output:&nbsp;</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="191" height="25" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-36.png" alt="" class="wp-image-405395"/></figure>
</div>


<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank">NumPy</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank">pandas</a> do not deliver p-values which is important if you want to report the findings. The following two solutions are better for this. </p>



<h2 class="wp-block-heading">Pearson Correlation in SciPy</h2>



<p>With <code>scipy.stats.pearsonsr(x, y)</code> we receive <strong>r</strong> just as quick and a p-value.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">scipy.stats.pearsonr(iris["petal_length"], iris["petal_width"])</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="357" height="37" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-37.png" alt="" class="wp-image-405396" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-37.png 357w, https://blog.finxter.com/wp-content/uploads/2022/06/image-37-300x31.png 300w" sizes="auto, (max-width: 357px) 100vw, 357px" /></figure>
</div>


<p>SciPy delivers just two values, but these are important: the first is the correlation coefficient <strong>r</strong> and the second is the <strong>p-value</strong> that determines significance.  </p>



<h2 class="wp-block-heading">Pearson Correlation in Pingouin</h2>



<p>My favorite solution is the statistical package <code>pingouin</code> because it delivers all values you would need for interpretation. </p>



<p>If you’re not familiar with <code>pingouin</code> check it out! It has great functions for complete test statistics.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pg.corr(iris["petal_length"], iris["petal_width"])</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="514" height="78" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-38.png" alt="" class="wp-image-405397" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-38.png 514w, https://blog.finxter.com/wp-content/uploads/2022/06/image-38-300x46.png 300w" sizes="auto, (max-width: 514px) 100vw, 514px" /></figure>
</div>


<p>The output tells us the number of cases <strong>n</strong>, the coefficient <strong>r</strong>, the confidence intervals, the <strong>p-value</strong>, the Bayes factor, and the power. </p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The power tells us the probability of detecting a true and strong relationship between variables. If the power is high, we are likely to detect a true effect. </p>



<p><strong>Interpretation:</strong></p>



<p>The most important values are the correlation coefficient <strong>r</strong> and the <strong>p-value</strong>. Pingouin also delivers some more useful test statistics.</p>



<p>If <strong>p &lt; 0.05</strong> we assume a significant test result. </p>



<p><strong>r</strong> is 0.96 which is a highly positive correlation, when 1 is the maximum and a perfect correlation. </p>



<p>Based on <strong>r</strong>, we can determine the effect size which tells us the strength of the relationship by interpreting <strong>r</strong> after Cohen’s effect size interpretation. There are also other interpretations for the effect size but Cohen’s is widely used.</p>



<p>After Cohen, a value of <strong>r</strong> around 0.1 to 0.3 shows a weak relationship, from 0.3 on would be an average effect and from 0.5 upwards will be a strong effect. With <strong>r</strong> = 0.96 we interpret a strong relationship. </p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Programmer Humor</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="925" height="507" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-9.png" alt="" class="wp-image-401703" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-9.png 925w, https://blog.finxter.com/wp-content/uploads/2022/06/image-9-300x164.png 300w, https://blog.finxter.com/wp-content/uploads/2022/06/image-9-768x421.png 768w" sizes="auto, (max-width: 925px) 100vw, 925px" /><figcaption><em>&#8220;Real programmers set the universal constants at the start such that the universe evolves to contain the disk with the data they want.&#8221;</em> &#8212; <a rel="noreferrer noopener" href="https://imgs.xkcd.com/comics/real_programmers.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/real_programmers.png" target="_blank">xkcd</a></figcaption></figure>
</div>


<p>The post <a href="https://blog.finxter.com/pearson-correlation-in-python/">Pearson Correlation in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Calculate z-scores in Python?</title>
		<link>https://blog.finxter.com/how-to-calculate-z-scores-in-python/</link>
		
		<dc:creator><![CDATA[Rebecca Nowack]]></dc:creator>
		<pubDate>Sat, 28 May 2022 14:11:32 +0000</pubDate>
				<category><![CDATA[Data Science]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Pandas Library]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<category><![CDATA[sklearn]]></category>
		<category><![CDATA[Statistics]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=394877</guid>

					<description><![CDATA[<p>The z-scores can be used to compare data with different measurements and for normalization of data for machine learning algorithms and comparisons. 💡 Note: There are different methods to calculate the z-score. The quickest and easiest one is: scipy.stats.zscore(). What is the z-score? The z-score is used for normalization or standardization to make differently scaled ... <a title="How to Calculate z-scores in Python?" class="read-more" href="https://blog.finxter.com/how-to-calculate-z-scores-in-python/" aria-label="Read more about How to Calculate z-scores in Python?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-calculate-z-scores-in-python/">How to Calculate z-scores in Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The z-scores can be used to compare data with different measurements and for normalization of data for <a href="https://blog.finxter.com/cheat-sheet-6-pillar-machine-learning-algorithms/" data-type="post" data-id="2613" target="_blank" rel="noreferrer noopener">machine learning algorithms</a> and comparisons. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: There are different methods to calculate the z-score. The quickest and easiest one is: <code>scipy.stats.zscore()</code>.</p>



<h2 class="wp-block-heading">What is the z-score?</h2>



<p>The z-score is used for normalization or standardization to make differently scaled variables with different means and categories comparable.</p>



<p>The formula for the z score is easy, so it is not a complicated transformation:</p>



<pre class="wp-block-preformatted"><code>z-score = (datapoint – mean)/standard deviation</code></pre>



<p>The statistical expression is&nbsp;</p>



<pre class="wp-block-preformatted"><code>z = (X – μ) / σ</code></pre>



<p>The z-score then tells us how far away the normalized value is from the standardized mean. The mean for the z-score will always be 0 and the <a href="https://blog.finxter.com/how-to-get-the-variance-of-a-list-in-python/" data-type="post" data-id="7489" target="_blank" rel="noreferrer noopener">variance</a> and <a href="https://blog.finxter.com/how-to-calculate-column-standard-deviation-2d-numpy-array/" data-type="post" data-id="456" target="_blank" rel="noreferrer noopener">standard deviation</a> will be 1. This way, the means of two differently scaled data points are comparable.</p>



<p>This is useful for different measurements of the same item for example comparing measurements like <em><strong>mm</strong></em> and <strong><em>inch</em></strong> or comparing test results with different max scores.</p>



<p>So we’ll actually try this on an example.</p>



<h2 class="wp-block-heading">Example z-score</h2>



<p>This term, Frank has reached 48, 33 and 41 points on the tests in math and 82, 98 and 75 points on the tests in English. </p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ac.png" alt="💬" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: Is Frank better in English than in math? </p>



<p>We don’t know because the max points in the math tests are 50 points and 100 for the English tests so we cannot directly compare these results.&nbsp;</p>



<p>But we can test our question with the z-score by normalizing and comparing the means.</p>



<p>First, we load our packages and create a data frame with the test results.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
import NumPy as np
import scipy.stats as stats

test_scores = pd.DataFrame(
    {"math":[48, 33, 41],
     "english":[82, 98, 75]},
    index=[1, 2, 3])
</pre>



<p>The data frame with the test results look like this:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="148" height="141" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-323.png" alt="" class="wp-image-394891"/></figure>
</div>


<h2 class="wp-block-heading">How to Calculate z-scores with Pandas?</h2>



<p>To calculate the z-scores in <a href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank" rel="noreferrer noopener">pandas</a> we just apply the formula to our data. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">z_test_scores = (test_scores-test_scores.mean())/(test_scores.std())</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="192" height="135" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-324.png" alt="" class="wp-image-394892"/></figure>
</div>


<p>We now normalized over each column and can tell for each test result how much it differs from the standardized mean.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">z_test_scores.apply(stats.zscore)</pre>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Important</strong>: Pandas calculates the standard deviation per default with an <em><a href="https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation" data-type="URL" data-id="https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation" target="_blank" rel="noreferrer noopener">unbiased standard estimator</a></em> and NumPy does not. This can be adapted with the degree of freedom <code>ddof=0</code> in pandas to equalize it to NumPy or <code>ddof=1</code> in NumPy to use the <em>unbiased estimator</em>.</p>



<p>In pandas the default setting is the normalization by N-1 for the calculation of the standard deviation. </p>



<p>For NumPy and <code>scipy.stats.zscore</code>, which is based on NumPy, the default is 0, so N is the estimator. </p>



<p>Just be aware of where this difference comes from.</p>



<h2 class="wp-block-heading">How to z-transform in Python with SciPy.Stats?</h2>



<p><a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" data-type="post" data-id="22420" target="_blank" rel="noreferrer noopener">SciPy</a> has the quickest function available in stats <code>scipy.stats.zscore(data)</code>. We’ll use this on our test scores.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">stats.zscore(test_scores)</pre>



<p>This will standardize each column. The output shows slightly different values than in pandas.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="193" height="134" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-325.png" alt="" class="wp-image-394896"/></figure>
</div>


<p>Applying the <code>zscore()</code> function to a pandas data frame will deliver the same results.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">z_test_scores.apply(stats.zscore)</pre>



<p>If we adapt the delta degrees of freedom to N-1 equal to pandas, we receive the same results as above.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">stats.zscore(test_scores, ddof=1)</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="200" height="136" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-326.png" alt="" class="wp-image-394897"/></figure>
</div>


<p>To answer the question (<em><strong>in what subject Frank is better this term?</strong></em>) we use the mean of the scores and pass it into the same function.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">stats.zscore(test_scores.mean())</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="141" height="75" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-327.png" alt="" class="wp-image-394902"/></figure>
</div>


<p>This tells us that Frank was better in English than in math!</p>



<h2 class="wp-block-heading">How to Calculate z-scores with NumPy?</h2>



<p>The z-transformation in <a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">NumPy</a> works similar to pandas. </p>



<p>First, we turn our data frame into a NumPy array and apply the same formula. We have to pass <code>axis = 0</code> to receive the same results as with <code>stats.zscores()</code>, as the default direction in NumPy is different.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">test_scores_np = test_scores.to_numpy()
z_test_scores_np = (test_scores_np - np.mean(test_scores_np, axis=0)) / np.std(test_scores_np, axis=0)
</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="304" height="71" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-328.png" alt="" class="wp-image-394903" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-328.png 304w, https://blog.finxter.com/wp-content/uploads/2022/05/image-328-300x70.png 300w" sizes="auto, (max-width: 304px) 100vw, 304px" /></figure>
</div>


<h2 class="wp-block-heading">How to Calculate z-scores with sklearn Standard Scaler?</h2>



<p>For normalization and standardization in machine learning algorithms, <a href="https://blog.finxter.com/scikit-learn-cheat-sheets/" data-type="post" data-id="20549" target="_blank" rel="noreferrer noopener">Scikit-learn</a> also has a <a href="https://blog.finxter.com/sklearn-fit-vs-transform-vs-fit_transform-whats-the-difference/" data-type="post" data-id="30381" target="_blank" rel="noreferrer noopener">z-transform</a> function called <code>StandardScaler()</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()  
scaler.fit_transform(test_scores)
</pre>



<p>Output:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="316" height="82" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-329.png" alt="" class="wp-image-394905" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-329.png 316w, https://blog.finxter.com/wp-content/uploads/2022/05/image-329-300x78.png 300w" sizes="auto, (max-width: 316px) 100vw, 316px" /></figure>
</div>


<p>This will also return an array with the same values.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>We now looked at four different ways to normalize data in Python with the z-score and one of them will surely work for you.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>The post <a href="https://blog.finxter.com/how-to-calculate-z-scores-in-python/">How to Calculate z-scores in Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Calculate a Logistic Sigmoid Function in Python?</title>
		<link>https://blog.finxter.com/how-to-calculate-a-logistic-sigmoid-function-in-python/</link>
		
		<dc:creator><![CDATA[Shubham Sayon]]></dc:creator>
		<pubDate>Tue, 24 May 2022 20:25:40 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=386415</guid>

					<description><![CDATA[<p>Summary: You can caculate the logistic sigmoid function in Python using: Problem: Given a logistic sigmoid function: If the value of x is given, how will you calculate F(x) in Python? Let&#8217;s say x=0.458. 💡 Note: Logistic sigmoid function is defined as (1/(1 + e^-x)) where x is the input variable and represents any real ... <a title="How to Calculate a Logistic Sigmoid Function in Python?" class="read-more" href="https://blog.finxter.com/how-to-calculate-a-logistic-sigmoid-function-in-python/" aria-label="Read more about How to Calculate a Logistic Sigmoid Function in Python?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-calculate-a-logistic-sigmoid-function-in-python/">How to Calculate a Logistic Sigmoid Function in Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong>Summary:</strong> You can caculate the logistic sigmoid function in Python using:</p>



<ul class="has-global-color-8-background-color has-background wp-block-list">
<li>The Math Module: <code>1 / (1 + math.exp(-x))</code></li>



<li>The Numpy Library: <code>1 / (1 + np.exp(-x))</code></li>



<li>The Scipy Library: <code>scipy.special.expit(x)</code></li>
</ul>



<hr class="wp-block-separator has-css-opacity"/>



<p><strong>Problem: </strong>Given a logistic sigmoid function:</p>



<figure class="wp-block-image is-style-default"><img decoding="async" src="https://i.stack.imgur.com/SUuRi.png" alt="enter image description here"/></figure>



<p>If the value of <strong><code>x</code></strong> is given, how will you calculate <strong><code>F(x)</code></strong> in Python? Let&#8217;s say <code>x=0.458</code>.</p>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note: </strong>Logistic sigmoid function is defined as <strong><code>(1/(1 + e^-x))</code></strong> where <code>x</code> is the input variable and represents any real number. The function returns a value that lies within the range -1 and 1. It forms an <strong>S-shaped </strong>curve when plotted on a graph.  </p>



<h2 class="wp-block-heading">Method 1: Sigmoid Function in Python Using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-math-module/" target="_blank">Math</a> Module</h2>



<p><strong><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Approach: </strong>Define a function that accepts <code>x</code> as an input and returns <code>F(x)</code> as <code>1/(1 + math.exp(-x))</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math


def sigmoid(x):
    return 1 / (1 + math.exp(-x))


print(sigmoid(0.458))

# OUTPUT: 0.6125396134409151</pre>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9e8.png" alt="🧨" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Caution: </strong>The above solution is mainly intended as a simple one-to-one translation of the given sigmoid expression into Python code. It is <em>not</em> strictly tested or considered a perfect and numerically sound implementation.</p>



<p>If you need a more robust implementation, some of the solutions to follow might be more instrumental in solving your case.</p>



<p>Here&#8217;s a more stable implementation of the above solution:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math


def sigmoid(x):
    if x >= 0:
        k = math.exp(-x)
        res = 1 / (1 + k)
        return res
    else:
        k = math.exp(x)
        res = k / (1 + k)
        return res


print(sigmoid(0.458))</pre>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note:</strong> <code>exp()</code> is a method of the math module in Python that returns the value of <code>E</code> raised to the power of <code>x</code>. Here, <code>x</code> is the input value passed to the <code>exp()</code> function, while <code>E</code> represents the base of the natural system of the logarithm (approximately 2.718282).</p>



<h2 class="wp-block-heading">Method 2: Sigmoid Function in Python Using <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" target="_blank">Numpy</a></h2>



<p>The sigmoid function can also be implemented using the <code>exp()</code> method of the Numpy module. <code>numpy.exp()</code> works just like the <code>math.exp()</code> method, with the additional advantage of being able to handle arrays along with integers and float values. </p>



<p><strong>Example 1</strong>: Let&#8217;s have a look at an example to visualize how to implement the sigmoid function using <code>numpy.exp()</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


print(sigmoid(0.458))

# OUTPUT: 0.6125396134409151</pre>



<p>Probably a more numerically stable version of the above implementation is as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np


def sigmoid(x):
    return np.where(x &lt; 0, np.exp(x) / (1 + np.exp(x)), 1 / (1 + np.exp(-x)))


print(sigmoid(0.458))

# OUTPUT: 0.6125396134409151</pre>



<p><strong>Example 2:</strong> Let&#8217;s look at an implementation of the sigmoid function upon an array of evenly spaced values with the help of a graph.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt


def sigmoid(x):
    return np.where(x &lt; 0, np.exp(x) / (1 + np.exp(x)), 1 / (1 + np.exp(-x)))


val = np.linspace(start=-10, stop=10, num=200)
sigmoid_values = sigmoid(val)
plt.plot(val, sigmoid_values)
plt.xlabel("x")
plt.ylabel("sigmoid(X)")
plt.show()</pre>



<p><strong>Output:</strong></p>



<figure class="wp-block-image size-full is-style-default"><img loading="lazy" decoding="async" width="568" height="406" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-310.png" alt="" class="wp-image-387625" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-310.png 568w, https://blog.finxter.com/wp-content/uploads/2022/05/image-310-300x214.png 300w" sizes="auto, (max-width: 568px) 100vw, 568px" /></figure>



<p><strong>Explanation:</strong></p>



<ul class="wp-block-list">
<li>Initially, we created an array of evenly spaced values within the range of -10 and 10 with the help of the <code>linspace</code> method of the Numpy module, i.e., <strong>val</strong>.</li>



<li>We then used the sigmoid function on these values. If you print them out, you will find that they are either extremely close to 0 or very close to 1. This can also be visualized once the graph is plotted.</li>



<li>Finally, we plotted the sigmoid function graph that we previously computed with the help of the function. The <em>x-axis </em>maps the values contained in <strong>val, </strong>while the <em>y-axis </em>maps the values returned by the sigmoid function.</li>
</ul>



<hr class="wp-block-separator has-css-opacity"/>



<p><strong>Do you want to become a NumPy master?</strong> Check out our interactive puzzle book <a href="https://amzn.to/39dEykm" target="_blank" rel="noreferrer noopener" title="https://amzn.to/39dEykm"><strong>Coffee Break NumPy</strong></a> and boost your data science skills! <em>(Amazon link opens in new tab.)</em></p>



<div class="wp-block-image"><figure class="aligncenter size-medium"><a href="https://amzn.to/39dEykm" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" width="200" height="300" src="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg" alt="Coffee Break NumPy" class="wp-image-2766"/></a></figure></div>



<h2 class="wp-block-heading">Method 3: Sigmoid Function in Python Using the <a rel="noreferrer noopener" href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" target="_blank">Scipy</a> Library</h2>



<p>Another efficient way to calculate the sigmoid function in Python is to use the <strong>Scipy</strong> libraries <code>expit</code> function. </p>



<p><strong>Example 1: Calculating logistic sigmoid for a given value</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.special import expit
print(expit(0.458))

# OUTPUT: 0.6125396134409151</pre>



<p><strong>Example 2: Calculating logistic sigmoid for multiple values</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.special import expit
x = [-2, -1, 0, 1, 2]
for value in expit(x):
    print(value)</pre>



<p><strong>Output:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0.11920292202211755
0.2689414213699951
0.5
0.7310585786300049
0.8807970779778823</pre>



<p class="has-background" style="background-color:#edc6f6"><strong>Recommended Read: <a href="https://blog.finxter.com/logistic-regression-in-one-line-python/" target="_blank" rel="noreferrer noopener">Logistic Regression in Python Scikit-Learn</a></strong></p>



<h2 class="wp-block-heading">Method 4: Transform the tanh() Function  </h2>



<p>Another workaround to compute the sigmoid function is to transform the <code>tanh</code> function of the <code>math</code> module as shown below:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import math

sigmoid = lambda x: .5 * (math.tanh(.5 * x) + 1)
print(sigmoid(0.458))

# OUTPUT: 0.6125396134409151</pre>



<p>Since, mathematically <code>sigmoid(x) == (1 + tanh(x/2))/2</code>. Hence, the above implementation should work and is a valid solution. However, the methods mentioned earlier are undoubtedly more stable numerically and superior to this solution. </p>



<h2 class="wp-block-heading">How to Calculate the Sigmoid for Arrays with Size Bigger Than 1 in Python?</h2>



<p class="has-global-color-8-background-color has-background">You can calculate the sigmoid function for 2D arrays (and even higher dimensional arrays) using NumPy. The NumPy library applies operations element-wise, so the shape of the array does not affect the ability to apply the sigmoid function.</p>



<p>Here&#8217;s an example of a 2D array:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

# Define the sigmoid function
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# Create a 2D array
arr = np.array([[0, 1, 2], [3, 4, 5]])

sigmoid_arr = sigmoid(arr)

print(sigmoid_arr)
</pre>



<p>In this example, <code>sigmoid_arr</code> will be a 2D array with the same shape as <code>arr</code>, but with the sigmoid function applied to each element.</p>



<p>The output will be:</p>



<pre class="wp-block-preformatted"><code>[[0.5        0.73105858 0.88079708]
 [0.95257413 0.98201379 0.99330715]]</code></pre>



<p>These numbers are the sigmoid values of the corresponding elements in the input array. Each number is between 0 and 1, inclusive, as is the property of the sigmoid function.</p>



<h2 class="wp-block-heading"><strong>Conclusion</strong></h2>



<p>Well, that&#8217;s it for this tutorial. We have discussed as many as four ways of calculating the logistic sigmoid function in Python. Feel free to use the one that suits your requirements. </p>



<p>I hope this article has helped you. Please <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">subscribe</a></strong> and stay tuned for more interesting solutions and tutorials. Happy learning!</p>



<hr class="wp-block-separator has-css-opacity"/>



<p><strong><a href="https://academy.finxter.com/university/tensorflow/" target="_blank" rel="noreferrer noopener" title="https://academy.finxter.com/university/tensorflow/">TensorFlow &#8211; A Hands-On Introduction to Deep Learning and Neural Networks for Beginners</a></strong></p>



<p>This course gives you a charming introduction into deep learning and neural networks using Google&#8217;s TensorFlow library for Python beginners.</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/tensorflow/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-37.png" alt="" class="wp-image-35113" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-37.png 363w, https://blog.finxter.com/wp-content/uploads/2021/09/image-37-168x300.png 168w" sizes="auto, (max-width: 363px) 100vw, 363px" /></a></figure></div>
<p>The post <a href="https://blog.finxter.com/how-to-calculate-a-logistic-sigmoid-function-in-python/">How to Calculate a Logistic Sigmoid Function in Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Install SciPy on PyCharm?</title>
		<link>https://blog.finxter.com/how-to-install-scipy-on-pycharm/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Mon, 13 Sep 2021 08:27:22 +0000</pubDate>
				<category><![CDATA[Dependency Management]]></category>
		<category><![CDATA[PyCharm]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=35209</guid>

					<description><![CDATA[<p>SciPy is an open-source Python library for math, science, and engineering. It includes the wildly popular NumPy and Matplotlib libraries. Problem Formulation: Given a PyCharm project. How to install the SciPy library in your project within a virtual environment or globally? Here&#8217;s a solution that always works: Open File > Settings > Project from the ... <a title="How to Install SciPy on PyCharm?" class="read-more" href="https://blog.finxter.com/how-to-install-scipy-on-pycharm/" aria-label="Read more about How to Install SciPy on PyCharm?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-install-scipy-on-pycharm/">How to Install SciPy on PyCharm?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><a href="https://www.scipy.org/" target="_blank" rel="noreferrer noopener" title="https://www.scipy.org/">SciPy </a>is an open-source Python library for math, science, and engineering. It includes the wildly popular <a href="https://blog.finxter.com/numpy-tutorial/" target="_blank" rel="noreferrer noopener" title="NumPy Tutorial – Everything You Need to Know to Get Started">NumPy </a>and <a href="https://blog.finxter.com/matplotlib-full-guide/" target="_blank" rel="noreferrer noopener" title="Matplotlib — A Simple Guide with Videos">Matplotlib </a>libraries.</p>



<p class="has-pale-cyan-blue-background-color has-background"><strong>Problem Formulation:</strong> Given a <a href="https://blog.finxter.com/pycharm-a-simple-illustrated-guide/" target="_blank" rel="noreferrer noopener" title="PyCharm – A Simple Illustrated Guide">PyCharm </a>project. How to install the SciPy library in your project within a <a href="https://blog.finxter.com/python-virtual-environments-with-venv-a-step-by-step-guide/" target="_blank" rel="noreferrer noopener" title="Python Virtual Environments with “venv” — A Step-By-Step Guide">virtual environment</a> or globally?</p>



<p>Here&#8217;s a <a href="https://blog.finxter.com/how-to-install-a-library-on-pycharm/" target="_blank" rel="noreferrer noopener" title="How to Install a Library on PyCharm?">solution </a>that always works: </p>



<ul class="wp-block-list"><li>Open <code><strong>File > Settings > Project</strong></code> from the PyCharm menu.</li><li>Select your current project.</li><li>Click the <code><strong>Python Interpreter</strong></code> tab within your project tab.</li><li>Click the small <code><strong>+</strong></code> symbol to add a new library to the project. </li><li>Now type in the library to be installed, in your example <code>"scipy"</code> without quotes, and click <code><strong>Install Package</strong></code>. </li><li>Wait for the installation to terminate and close all popup windows.</li></ul>



<p>Here&#8217;s the installation process as a short animated video&#8212;it works analogously for SciPy, just type in <em>&#8220;scipy&#8221;</em> in the search field instead:</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://media.giphy.com/media/VQoZIvOyP23tVvQlW0/source.gif" alt=""/></figure>



<p>Make sure to select only &#8220;scipy&#8221; because there are many other packages that are not required but also contain the term &#8220;scipy&#8221; (False positives):</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="831" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-50-1024x831.png" alt="SciPy installation PyCharm" class="wp-image-35210" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-50-1024x831.png 1024w, https://blog.finxter.com/wp-content/uploads/2021/09/image-50-300x244.png 300w, https://blog.finxter.com/wp-content/uploads/2021/09/image-50-768x624.png 768w, https://blog.finxter.com/wp-content/uploads/2021/09/image-50.png 1143w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>Alternatively, you can run the <code><strong><a href="https://blog.finxter.com/how-to-install-pip-on-windows/" title="How To Install pip On Windows?" target="_blank" rel="noreferrer noopener">pip install</a> scipy</strong></code> command in your PyCharm &#8220;<strong>Terminal</strong>&#8221; view:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ pip install scipy</pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="592" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-51-1024x592.png" alt="&quot;pip install scipy&quot; in PyCharm Terminal" class="wp-image-35211" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-51-1024x592.png 1024w, https://blog.finxter.com/wp-content/uploads/2021/09/image-51-300x174.png 300w, https://blog.finxter.com/wp-content/uploads/2021/09/image-51-768x444.png 768w, https://blog.finxter.com/wp-content/uploads/2021/09/image-51.png 1224w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<hr class="wp-block-separator"/>



<p>Feel free to check out the following free email academy with Python cheat sheets to boost your coding skills!</p>






<p>To become a PyCharm master, check out our <a href="https://academy.finxter.com/university/pycharm/" title="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noreferrer noopener">full course</a> on the Finxter Computer Science Academy available for free for all <a href="https://blog.finxter.com/finxter-premium-membership/" target="_blank" rel="noreferrer noopener" title="Finxter Premium Membership">Finxter Premium Members</a>:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png" alt="" class="wp-image-34968" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png 363w, https://blog.finxter.com/wp-content/uploads/2021/09/image-10-168x300.png 168w" sizes="auto, (max-width: 363px) 100vw, 363px" /></a></figure></div>
<p>The post <a href="https://blog.finxter.com/how-to-install-scipy-on-pycharm/">How to Install SciPy on PyCharm?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python &#8211; Inverse of Normal Cumulative Distribution Function (CDF)</title>
		<link>https://blog.finxter.com/python-inverse-of-normal-cumulative-distribution-function-cdf/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Tue, 03 Aug 2021 19:22:31 +0000</pubDate>
				<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=33510</guid>

					<description><![CDATA[<p>Problem Formulation How to calculate the inverse of the normal cumulative distribution function (CDF) in Python? Method 1: scipy.stats.norm.ppf() In Excel, NORMSINV is the inverse of the CDF of the standard normal distribution. In Python&#8217;s SciPy library, the ppf() method of the scipy.stats.norm object is the percent point function, which is another name for the ... <a title="Python &#8211; Inverse of Normal Cumulative Distribution Function (CDF)" class="read-more" href="https://blog.finxter.com/python-inverse-of-normal-cumulative-distribution-function-cdf/" aria-label="Read more about Python &#8211; Inverse of Normal Cumulative Distribution Function (CDF)">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-inverse-of-normal-cumulative-distribution-function-cdf/">Python &#8211; Inverse of Normal Cumulative Distribution Function (CDF)</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Problem Formulation</h2>



<p>How to calculate the inverse of the normal cumulative distribution function (CDF) in Python?</p>



<h2 class="wp-block-heading">Method 1: scipy.stats.norm.ppf()</h2>



<p>In Excel, <a href="http://support.microsoft.com/kb/826772" target="_blank" rel="noreferrer noopener">NORMSINV</a> is the inverse of the CDF of the standard normal distribution. </p>



<p class="has-pale-cyan-blue-background-color has-background">In Python&#8217;s <a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" target="_blank" rel="noreferrer noopener" title="Best 10 Scipy Cheat Sheets">SciPy </a>library, the <code>ppf()</code> method of the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html" target="_blank" rel="noreferrer noopener"><code>scipy.stats.norm</code></a> object is the <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#PPF"><em>percent point function</em></a>, which is another name for the <a href="https://en.wikipedia.org/wiki/Quantile_function"><em>quantile function</em></a>. This <code>ppf()</code> method is the inverse of the <code>cdf()</code> function in SciPy.</p>



<ul class="wp-block-list"><li><code>norm.cdf()</code> is the inverse function of <code>norm.ppf()</code></li><li><code>norm.ppf()</code> is the inverse function of <code>norm.cdf()</code></li></ul>



<p>You can see this in the following code snippet:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from scipy.stats import norm

print(norm.cdf(norm.ppf(0.5)))
print(norm.ppf(norm.cdf(0.5)))</pre>



<p>The output is as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0.5
0.5000000000000001</pre>



<p>An alternative is given next:</p>



<h2 class="wp-block-heading">Method 2: statistics.NormalDist.inv_cdf()</h2>



<p class="has-pale-cyan-blue-background-color has-background">Python 3.8 provides the <a href="https://docs.python.org/3.8/library/statistics.html?highlight=normaldist#statistics.NormalDist" target="_blank" rel="noreferrer noopener"><code>NormalDist</code></a> object as part of the <a href="https://docs.python.org/3.8/library/statistics.html" target="_blank" rel="noreferrer noopener"><code>statistics</code></a> module that is included in the standard library. It includes the inverse cumulative distribution function <strong><a href="https://docs.python.org/3.8/library/statistics.html#statistics.NormalDist.inv_cdf"><code>inv_cdf()</code></a></strong>. To use it, pass the <em>mean </em>(<code>mu</code>) and <em>standard deviation</em> (<code>sigma</code>) into the <code>NormalDist()</code> constructor to adapt it to the concrete normal distribution at hand.</p>



<p>Have a look at the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from statistics import NormalDist

res = NormalDist(mu=1, sigma=0.5).inv_cdf(0.5)
print(res)
# 1.0</pre>



<p>A great resource on the topic is given next.</p>



<p><strong>References:</strong></p>



<ul class="wp-block-list"><li><a href="https://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p">https://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p</a></li></ul>



<p><strong>Do you want to become a NumPy master?</strong> Check out our interactive puzzle book <a href="https://amzn.to/39dEykm" target="_blank" rel="noreferrer noopener" title="https://amzn.to/39dEykm"><strong>Coffee Break NumPy</strong></a> and boost your data science skills! <em>(Amazon link opens in new tab.)</em></p>



<div class="wp-block-image"><figure class="aligncenter size-medium"><a href="https://amzn.to/39dEykm" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" width="200" height="300" src="https://blog.finxter.com/wp-content/uploads/2019/04/Cover_Coffee_Break_NumPy-200x300.jpg" alt="Coffee Break NumPy" class="wp-image-2766"/></a></figure></div>
<p>The post <a href="https://blog.finxter.com/python-inverse-of-normal-cumulative-distribution-function-cdf/">Python &#8211; Inverse of Normal Cumulative Distribution Function (CDF)</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Best 10 Scipy Cheat Sheets</title>
		<link>https://blog.finxter.com/best-10-scipy-cheat-sheets/</link>
		
		<dc:creator><![CDATA[Amber Mercado]]></dc:creator>
		<pubDate>Fri, 29 Jan 2021 15:48:34 +0000</pubDate>
				<category><![CDATA[Cheat Sheets]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SciPy]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=22420</guid>

					<description><![CDATA[<p>Hey Finxters! Another 10 of the best cheat sheets is here for you to peruse and hang on your wall with your other Python cheat sheets on the wall! Today, we are going to browse cheat sheets for Scipy!! For a quick explanation, SciPy is a scientific computation library that uses NumPy underneath. SciPy stands ... <a title="Best 10 Scipy Cheat Sheets" class="read-more" href="https://blog.finxter.com/best-10-scipy-cheat-sheets/" aria-label="Read more about Best 10 Scipy Cheat Sheets">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/">Best 10 Scipy Cheat Sheets</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hey Finxters! Another 10 of the best cheat sheets is here for you to peruse and hang on your wall with your other Python cheat sheets on the wall! Today, we are going to browse cheat sheets for Scipy!! For a quick explanation, <strong>SciPy</strong> is a scientific computation library that uses NumPy underneath. <strong>SciPy</strong> stands for Scientific <strong>Python</strong>. It provides more utility functions for optimization, stats and signal processing. Now that we have a brief explanation on what it is, let us dive right into these cheat sheets that can be kept handy when learning to implement Scipy in Python!</p>



<h2 class="wp-block-heading"><a href="https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf" target="_blank" rel="noreferrer noopener" title="https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf">Cheat Sheet 1: DataCamp</a></h2>



<figure class="wp-block-image size-large"><a href="https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="724" src="https://blog.finxter.com/wp-content/uploads/2021/01/image-182-1024x724.png" alt="" class="wp-image-22432" srcset="https://blog.finxter.com/wp-content/uploads/2021/01/image-182-1024x724.png 1024w, https://blog.finxter.com/wp-content/uploads/2021/01/image-182-300x212.png 300w, https://blog.finxter.com/wp-content/uploads/2021/01/image-182-768x543.png 768w, https://blog.finxter.com/wp-content/uploads/2021/01/image-182-150x106.png 150w, https://blog.finxter.com/wp-content/uploads/2021/01/image-182.png 1210w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>The first cheat sheet is from DataCamp.com and is chock full of information for you to consume. You will learn to interact with Numpy and know which functions and methods to use for linear algebra and of course a help section. This is one I would hang behind my monitor behind the wall!</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> None that I can see.</p>



<h2 class="wp-block-heading"><a href="https://data.templateroller.com/pdf_docs/160/1606/160651/main_numpy-or-scipy-pandas-plotting-quandl-cheat-sheet-python.pdf?c3e9234de51375ea17a17e2e60a6ea7b" target="_blank" rel="noreferrer noopener" title="https://data.templateroller.com/pdf_docs/160/1606/160651/main_numpy-or-scipy-pandas-plotting-quandl-cheat-sheet-python.pdf?c3e9234de51375ea17a17e2e60a6ea7b">Cheat Sheet 2: Quandl</a></h2>



<p>This cheat sheet covers the three main data science libraries used in Python: Pandas, Numpy, and Scipy. It goes over the functions call but has explanations on each one. Near the end it shows how to import data sets for you to use! Great for a beginner project!</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone. Bonus Python project included!</p>



<p><strong>Cons:</strong> None that I can see.</p>



<h2 class="wp-block-heading"><a href="https://elitedatascience.com/python-cheat-sheet" target="_blank" rel="noreferrer noopener" title="https://elitedatascience.com/python-cheat-sheet">Cheat Sheet 3: Elite Data Science</a></h2>



<p>This cheat sheet will walk you through some of the most common and useful functionality from these libraries. From importing data to a taste of Machine learning you can get a feel of what Python can do of the code examples.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> None that I can see.</p>



<h2 class="wp-block-heading"><a href="https://cheatography.com/sasha2411/cheat-sheets/hypothesis-testing-with-scipy" target="_blank" rel="noreferrer noopener" title="https://cheatography.com/sasha2411/cheat-sheets/hypothesis-testing-with-scipy">Cheat Sheet 4: Cheatography</a></h2>



<p>If you ever needed help understanding how to test a hypothesis in Scipy using code examples and clear explanations on what is happening when you write the code.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> None that I can see.</p>



<h2 class="wp-block-heading"><a href="https://intellipaat.com/blog/tutorial/python-tutorial/python-scipy" target="_blank" rel="noreferrer noopener" title="https://intellipaat.com/blog/tutorial/python-tutorial/python-scipy">Cheat Sheet 5: Intellipaat</a></h2>



<p>This cheat sheet is more a tutorial from Intellipaat.com It has full explanations with code examples to work. It has sufficient information about the scientific and technical library in Python, that is, Scipy. Nonetheless, it is more than worth your time to investigate and learn Scipy.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> It is more a tutorial than a cheat sheet.</p>



<h2 class="wp-block-heading"><a href="http://www.genautica.com/cheatsheets/scipy.jpg?epik=dj0yJnU9bHpxalo2Q3lGbFJ0ZDBIT2h3d3ZiVzY5dHVmTU5ocDUmcD0wJm49RFpSaUJjX1N2TjBRYkhSWHlmdzExdyZ0PUFBQUFBR0FURFRJ" target="_blank" rel="noreferrer noopener" title="http://www.genautica.com/cheatsheets/scipy.jpg?epik=dj0yJnU9bHpxalo2Q3lGbFJ0ZDBIT2h3d3ZiVzY5dHVmTU5ocDUmcD0wJm49RFpSaUJjX1N2TjBRYkhSWHlmdzExdyZ0PUFBQUFBR0FURFRJ">Cheat Sheet 6: Scipy.org</a></h2>



<figure class="wp-block-image is-resized"><a href="http://www.genautica.com/cheatsheets/scipy.jpg?epik=dj0yJnU9bHpxalo2Q3lGbFJ0ZDBIT2h3d3ZiVzY5dHVmTU5ocDUmcD0wJm49RFpSaUJjX1N2TjBRYkhSWHlmdzExdyZ0PUFBQUFBR0FURFRJ" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="http://www.genautica.com/cheatsheets/scipy.jpg?epik=dj0yJnU9bHpxalo2Q3lGbFJ0ZDBIT2h3d3ZiVzY5dHVmTU5ocDUmcD0wJm49RFpSaUJjX1N2TjBRYkhSWHlmdzExdyZ0PUFBQUFBR0FURFRJ" alt="Scipy Cheat Sheet: 
http://www.genautica.com/cheatsheets/scipy.jpg?epik=dj0yJnU9bHpxalo2Q3lGbFJ0ZDBIT2h3d3ZiVzY5dHVmTU5ocDUmcD0wJm49RFpSaUJjX1N2TjBRYkhSWHlmdzExdyZ0PUFBQUFBR0FURFRJ" width="1099" height="2649"/></a></figure>



<p>From the mouth of Scipy, this cheat sheet will show you all of the methods needed to perform different functions in Scipy and Python with explanations. This Comprehensive list has everything sorted neatly into the different functions to make it easy to look up as you are working in Scipy. This is one you will want in your notebook on the desk as an easy reference guide.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone. Recommended for the wall or notebook for daily use!</p>



<p><strong>Cons:</strong> None that I can see.</p>



<h2 class="wp-block-heading"><a href="https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781783984749" target="_blank" rel="noreferrer noopener" title="https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781783984749">Cheat Sheet 7: Packt&gt;</a></h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781783984749" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="187" height="231" src="https://blog.finxter.com/wp-content/uploads/2021/01/image-183.png" alt="" class="wp-image-22438" srcset="https://blog.finxter.com/wp-content/uploads/2021/01/image-183.png 187w, https://blog.finxter.com/wp-content/uploads/2021/01/image-183-150x185.png 150w" sizes="auto, (max-width: 187px) 100vw, 187px" /></a></figure>
</div>


<p>This is more a book than it is a cheat sheet. It focuses hard on mastering scipy giving you a project to work through so you can really get a grasp on Scipy and how it is implemented in Python. I recommend subscribing to the website for all of the information you will receive.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> It is an ebook not a cheat sheet, but worth your time.</p>



<h2 class="wp-block-heading"><a href="https://scipy-lectures.org/_downloads/ScipyLectures-simple.pdf" target="_blank" rel="noreferrer noopener" title="https://scipy-lectures.org/_downloads/ScipyLectures-simple.pdf">Cheat Sheet 8: Scipy.org</a></h2>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><a href="https://scipy-lectures.org/_downloads/ScipyLectures-simple.pdf" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2021/01/image-185.png" alt="" class="wp-image-22441" width="298" height="421" srcset="https://blog.finxter.com/wp-content/uploads/2021/01/image-185.png 595w, https://blog.finxter.com/wp-content/uploads/2021/01/image-185-212x300.png 212w, https://blog.finxter.com/wp-content/uploads/2021/01/image-185-150x212.png 150w" sizes="auto, (max-width: 298px) 100vw, 298px" /></a></figure>
</div>


<p>This is another ebook that I recommend keeping on hand to learn Scipy from beginner levels to advanced. This book contains code for you to work on in order to learn scipy in python building your skills. This is important for you to learn the skill you need for your data science career. I suggest reading the book, highlight the parts you don’t understand and print the code example to pin to the wall for help and minimize searching.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons:</strong> This is an ebook, but one of the best ways to learn.</p>



<h2 class="wp-block-heading"><a href="https://www.packtpub.com/product/learning-scipy-for-numerical-and-scientific-computing-second-edition/9781783987702" target="_blank" rel="noreferrer noopener" title="https://www.packtpub.com/product/learning-scipy-for-numerical-and-scientific-computing-second-edition/9781783987702">Cheat Sheet 9: Packt&gt;</a></h2>



<p>This one is also a ebook from packt&gt;. This ebook will teach you numerical and scientific computing in Python. You will also learn how to use Scipy in signal processing and how applications of Scipy can be used to collect, organize, analyze,a dn interpret data. By the end of the book, you will have fast, accurate, and easy-to-code solutions for numerical and scientific computing applications.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone.</p>



<p><strong>Cons: </strong>This is an ebook so you will be spending time reading and coding.</p>



<h2 class="wp-block-heading"><a href="https://www.packtpub.com/product/scipy-recipes/9781788291460" target="_blank" rel="noreferrer noopener" title="https://www.packtpub.com/product/scipy-recipes/9781788291460">Cheat Sheet 10: Packt&gt;</a></h2>



<p>Recipes are great in that you can find the exact one you are looking for without having to wade through all the other code snippets you do not need. In this ebook, you can play around with each one of these codes and gain a hands-on understanding of Scipy and its real-world problem applications.</p>



<p><strong>Pros:</strong> Rated ‘E’ for everyone. The independent nature of the recipes allows you to hop around from each example making this book very versatile.</p>



<p><strong>Cons:</strong> It is an ebook but a great one if you want to practice the different stacks of Scipy in Python.</p>






<h2 class="wp-block-heading">Programmer Humor &#8211; Blockchain</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="280" height="394" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png" alt="" class="wp-image-457795" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png 280w, https://blog.finxter.com/wp-content/uploads/2022/07/image-31-213x300.png 213w" sizes="auto, (max-width: 280px) 100vw, 280px" /><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;</em> <strong>source </strong>&#8211; <a href="https://imgs.xkcd.com/comics/blockchain.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/blockchain.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div>


<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<p><strong>Related Articles:</strong></p>



<ul class="wp-block-list"><li><a href="https://blog.finxter.com/collection-5-cheat-sheets-every-python-coder-must-own/" target="_blank" rel="noreferrer noopener" title="[Collection] 11 Python Cheat Sheets Every Python Coder Must Own">[Collection] 11 Python Cheat Sheets Every Python Coder Must Own</a></li><li><a href="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/" target="_blank" rel="noreferrer noopener" title="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/">[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming</a></li><li><a href="https://blog.finxter.com/machine-learning-cheat-sheets/" title="[Collection] 15 Mind-Blowing Machine Learning Cheat Sheets to Pin to Your Toilet Wall" target="_blank" rel="noreferrer noopener">[Collection] 15 Mind-Blowing Machine Learning Cheat Sheets to Pin to Your Toilet Wall</a></li><li><a href="https://blog.finxter.com/python-cheat-sheets/" title="https://blog.finxter.com/python-cheat-sheets/" target="_blank" rel="noreferrer noopener">Your 8+ Free Python Cheat Sheet [Course]</a></li><li><a href="https://blog.finxter.com/python-cheat-sheet/" target="_blank" rel="noreferrer noopener" title="Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know">Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know</a></li><li><a href="https://blog.finxter.com/python-cheat-sheet-functions-and-tricks/" title="Python Functions and Tricks Cheat Sheet" target="_blank" rel="noreferrer noopener">Python Functions and Tricks Cheat Sheet</a></li><li><a href="https://blog.finxter.com/python-interview-questions/" target="_blank" rel="noreferrer noopener" title="https://blog.finxter.com/python-interview-questions/">Python Cheat Sheet: 14 Interview Questions</a></li><li><a href="https://blog.finxter.com/pandas-cheat-sheets/" title="[PDF Collection] 7 Beautiful Pandas Cheat Sheets — Post Them to Your Wall" target="_blank" rel="noreferrer noopener">Beautiful Pandas Cheat Sheets</a></li><li><a href="https://blog.finxter.com/collection-10-best-numpy-cheat-sheets-every-python-coder-must-own/" title="[Collection] 10 Best NumPy Cheat Sheets Every Python Coder Must Own" target="_blank" rel="noreferrer noopener">10 Best NumPy Cheat Sheets</a></li><li><a href="https://blog.finxter.com/python-list-methods-cheat-sheet-instant-pdf-download/" title="Python List Methods Cheat Sheet [Instant PDF Download]" target="_blank" rel="noreferrer noopener">Python List Methods Cheat Sheet [Instant PDF Download]</a></li><li><a href="https://blog.finxter.com/cheat-sheet-6-pillar-machine-learning-algorithms/" target="_blank" rel="noreferrer noopener" title="[Cheat Sheet] 6 Pillar Machine Learning Algorithms">[Cheat Sheet] 6 Pillar Machine Learning Algorithms</a></li></ul>
</div></div>
<p>The post <a href="https://blog.finxter.com/best-10-scipy-cheat-sheets/">Best 10 Scipy Cheat Sheets</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-04-19 03:00:24 by W3 Total Cache
-->