<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Liz_decoder</title>
    <description>The latest articles on Forem by Liz_decoder (@liz_decoder).</description>
    <link>https://forem.com/liz_decoder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1675731%2F44f7a5c0-fb4d-44fa-a74c-73cb0e858edd.png</url>
      <title>Forem: Liz_decoder</title>
      <link>https://forem.com/liz_decoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/liz_decoder"/>
    <language>en</language>
    <item>
      <title>NumPy: Understanding NumPy python library</title>
      <dc:creator>Liz_decoder</dc:creator>
      <pubDate>Mon, 24 Mar 2025 15:44:40 +0000</pubDate>
      <link>https://forem.com/liz_decoder/numpy-understanding-numpy-python-library-4pl6</link>
      <guid>https://forem.com/liz_decoder/numpy-understanding-numpy-python-library-4pl6</guid>
      <description>&lt;h1&gt;
  
  
  NUMPY
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;NumPy&lt;/em&gt; is the core library for scientific computing in Python. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foundational Python libraries such as pandas, SciPy, and Matplotlib are built on top of NumPy's API. So are machine learning libraries such as TensorFlow and scikit-learn, which use NumPy arrays as inputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  NumPy arrays
&lt;/h2&gt;

&lt;p&gt;The array is the main object in NumPy; it's a grid-like structure that holds data. An array can have any number of dimensions, and each dimension can be any length.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating 1D arrays from lists
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can create arrays from Python lists by passing a list as an argument to the np.array function. &lt;/li&gt;
&lt;li&gt;The data type of this array is a NumPy nd.array, or n-dimensional array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python_list =[3,2,5,9,7,1,4,3,6]
array = np.array(python_list)

array

array([3,2,5,9,7,1,4,3,6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating 2D arrays from lists
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To create a 2-dimensional array, pass np.array a list of lists.  A list of lists of lists would generate a 3-dimensional array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python_list_of_lists = ([[3,2,5],
                        [9,7,1],
                        [4,3,6]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrays vs. Python lists
&lt;/h2&gt;

&lt;p&gt;*Python lists can include many different data types: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;python_list = ['beep', False, 56, .924, [3,2,5]]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NumPy array must be the same data type. &lt;/li&gt;
&lt;li&gt;This makes NumPy very efficient: there's no need for NumPy to check the data type of each element in an array since they must all be the same.&lt;/li&gt;
&lt;li&gt;Having only a single data type also means that a NumPy array takes up less space in memory than the same information would if stored as a Python list.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numpy_boolean_array = [[True, False], [True, True], [False, True]]
numpy_float_array = [1.9, 2.4, 3.1, 6.4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating arrays from scratch
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There are many Numpy functions used to create arrays.
Using functions such as 

&lt;ul&gt;
&lt;li&gt;np.zeros()&lt;/li&gt;
&lt;li&gt;np.random.random()&lt;/li&gt;
&lt;li&gt;np.arange()&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating arrays: np.zeros()
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;np.zeros() creates an array full of zeros. We can use the zeros array just as we might use an empty Python list, filling it with data later on. We tell np.zeros() the shape of the desired array using a tuple of integers, each representing the length of a given dimension. &lt;/li&gt;
&lt;li&gt;A tuple is a Python data type used to store collections of data, similar to a list. Tuples are created with parentheses() &lt;/li&gt;
&lt;li&gt;Here, when np.zeros() is given a tuple of five and three, it creates an array with five rows and three columns.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.zeros((5,3))

array([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.].
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating arrays: np.random.random()
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;np.random.random() also accepts a tuple with the desired array's shape. The array will be made up of random floats between 0 - 1
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; np.random.random((2,4))

 array([[0.8852461, 0.08564132, 0.33463107, 0.53337117],
        [0.69933362, 0.09295327, 0.93617865, 0.04532187]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;np.random.random() is called that, because np.random.random() is a function within NumPy's random module, which contains useful functions for random sampling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;np.random. {NumPy module}&lt;/p&gt;

&lt;p&gt;random() {function name}&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating arrays: np.arange()
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;np.arange() creates an evenly-spaced array of numbers based on given start and stop values. 
By default, np.arange() creates an array of sequential integers. The start value is included in the output array, but the stop value is not. The start value can be omitted if the range begins with zero. If a third argument is passed, it is interpreted as the step value. Now, the desired distance between elements is three. 
*np.arange() is particularly useful for plotting!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;np.arange(-3,4)
array([-3, -2, -2, 0, 1, 2, 3])

np.arange(4)
array([0, 1, 2, 3])

np.arange(-3, 4, 3)
array([-3, 0, 3])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>datascience</category>
      <category>learning</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Understanding Tuple Unpacking and Iteration in Python: A Beginner's Guide</title>
      <dc:creator>Liz_decoder</dc:creator>
      <pubDate>Fri, 17 Jan 2025 11:34:53 +0000</pubDate>
      <link>https://forem.com/liz_decoder/understanding-tuple-unpacking-and-iteration-in-python-a-beginners-guide-42ee</link>
      <guid>https://forem.com/liz_decoder/understanding-tuple-unpacking-and-iteration-in-python-a-beginners-guide-42ee</guid>
      <description>&lt;p&gt;Ever tried carrying multiple items at once? That's exactly what Python tuples do! The concept of tuple unpacking in Python and how it relates to swapping elements in a list is important as it allows you to assign multiple variables from a tuple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's a Tuple?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Think of a tuple as a sturdy(unchangeable) container that keeps your items safe. Once you put something in, it stays exactly where you put it – no shuffling around allowed! &lt;br&gt;
Technically, A tuple in Python is a collection data type just like a list in Python, that is immutable, meaning once it is created its contents cannot be changed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tuples are defined using parentheses (), and they can hold multiple items.&lt;br&gt;
For example:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;my_tuple = (1, 2, 3)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Magic of Tuple Unpacking&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tuple unpacking is like having a smart unboxing system, where you can unpack everything at once.
Technically, it's a feature that lets you assign multiple values to multiple variables in a single line.
For example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;a, b = (1, 2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;a will be 1 and b will be 2.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Swapping elements with tuple unpacking&lt;/strong&gt; &lt;br&gt;
Tuple unpacking is commonly used for swapping values. You can swap values without needing a temporary variable. Here's how it works:&lt;br&gt;
You have a list&lt;br&gt;
a = [65, 90, 80, 100]&lt;/p&gt;

&lt;p&gt;If you want to swap the elements at index 1 and index 3, &lt;/p&gt;

&lt;p&gt;a[1], a[3] = a[3], a[1]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The right side a[3], a[1] creates a tuple (100, 90) from the values at index 3 and 1.&lt;/li&gt;
&lt;li&gt;The left side a[1], a[3] unpacks that tuple, assigning 100 to a[1] and 90 to a[3].&lt;/li&gt;
&lt;li&gt;This operation effectively swaps the values in one line.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Why Can't We Modify Tuples?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember, tuples are like locked boxes. Once you create a tuple, you can't change what's inside.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Technically, Tuples are immutable, you cannot change their contents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;scores = (95, 87, 92)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;scores[0] = 95 # TypeError! Can't modify a tuple&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This happens because tuples do not allow modification of individual elements. If you need to change values, you would need to create a new tuple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iterating over Tuples&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even though tuples are immutable that doesn't mean we can't look at everything inside! 
Iteration allows you to access and process each element in the tuple, one at a time.  We can easily walk through each item:
For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Student's grades
grades = ('A', 'B+', 'A-')
for grade in grades:
    print(f"Got a {grade}!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;if grade A takes the first value from the tuple # prints: Got a A!&lt;/code&gt;&lt;br&gt;
&lt;code&gt;if grade B takes the second value # prints: Got a B+!&lt;/code&gt;&lt;br&gt;
&lt;code&gt;if grade A- takes the third value # prints: Got a A-!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use tuples when you want to store data safe from changes&lt;/li&gt;
&lt;li&gt;Remember that tuple unpacking allows you to assign multiple variables from a tuple or sequence in one line and is your friend for multiple assignments.&lt;/li&gt;
&lt;li&gt;When swapping values, tuple unpacking is cleaner than using temporary variables.&lt;/li&gt;
&lt;li&gt;Iteration: You can iterate over tuples to access each value, but again, the tuple itself remains the same. 

&lt;ul&gt;
&lt;li&gt;If you need to modify values often, consider using a list.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
