avatarNatalie Frank, Ph.D.

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4014

Abstract

w-do-you-know-the-shape-and-size-of-an-array"> shape and size</a> attributes of a NumPy array can be used to determine its size and shape. To show you how to do this, consider the following sample code:</p><figure id="d80b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Zb80GcDJvLlcJHxdJ5OARg.png"><figcaption>Image source: <a href="https://www.pythontutorial.net/python-numpy/what-is-numpy/">https://www.pythontutorial.net/python-numpy/what-is-numpy/</a></figcaption></figure><div id="be46"><pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># Create a NumPy array</span> arr = np.array([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]])

<span class="hljs-comment"># Get the shape of the array</span> shape = arr.shape

<span class="hljs-comment"># Get the size (number of elements) of the array</span> size = arr.size

<span class="hljs-comment"># Print the shape and size</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"Shape of the array:"</span>, shape) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Size of the array:"</span>, size)</pre></div><p id="5678"><b>The output will be:</b></p><div id="2b07"><pre>Shape of the array: (2, 3) Size of the array: 6</pre></div><p id="4e3e">The numpy library is first imported in this example as <b><i>np</i></b>. Then, a NumPy array called <b><i>arr </i></b>is created, containing two rows and three columns of integers. We use the <b><i>shape </i></b>attribute to determine the array’s shape, and the <b><i>size </i></b>attribute to determine the size of the array.</p><h1 id="f123">Numpy Data Types</h1><p id="4c21">Various data types, such as integers, floats, and others, can be contained in NumPy arrays. When establishing an array, the data type can be specified. Following are a few typical data types:-</p><figure id="c617"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ekCE4G_H2oHE3qfaS6LKqQ.png"><figcaption>Image source: <a href="https://lifewithdata.com/2022/04/02/numpy-array-data-types-with-examples/">https://lifewithdata.com/2022/04/02/numpy-array-data-types-with-examples/</a></figcaption></figure><ul><li><code><b>int</b></code><b> </b>for integers</li><li><code><b>float</b></code><b> </b>for floating point numbers</li><li><code><b>complex</b></code><b> </b>for complex numbers</li><li><code><b>bool</b></code><b> </b>for boolean numbers</li><li><code><b>str</b></code><b> </b>for strings</li></ul><p id="7c82">Let's see some examples of each data type:</p><div id="9a51"><pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># Creating arrays with specified data types</span> int_array = np.array([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], dtype=<span class="hljs-string">'int'</span>) float_array = np.array([<span class="hljs-number">1.0</span>, <span class="hljs-number">2.0</span>, <span class="hljs-number">3.0</span>], dtype=<span class="hljs-string">'float'</span>) complex_array = np.array([<span class="hljs-number">1</span>+<span class="hljs-number">2j</span>, <span class="hljs-number">2</span>+<span class="hljs-number">3j</span>], dtype=<span class="hljs-string">'complex'</span>) bool_array = np.array([<span class="hljs-literal">True</span>, <span class="hljs-literal">False</span>, <span class="hljs-literal">True</span>], dtype=<span class="hljs-string">'bool'</span>) str_array = np.array([<span class="hljs-string">'apple'</span>, <span class="hljs-string">'banana'</span>, <span class="hljs-string">'cherry'</span>], dtype=<span class="hljs-string">'str'</span>)

<span class="hljs-built_in">print</span>(<span class="hljs-string">"Int Array:"</span>, int_array) <sp

Options

an class="hljs-built_in">print</span>(<span class="hljs-string">"Float Array:"</span>, float_array) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Complex Array:"</span>, complex_array) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Bool Array:"</span>, bool_array) <span class="hljs-built_in">print</span>(<span class="hljs-string">"String Array:"</span>, str_array)</pre></div><p id="ac64"><b>The output will be:</b></p><div id="37dd"><pre>Int Array: [1 2 3] Float Array: [1. 2. 3.] Complex Array: [1.+2.j 2.+3.j] Bool Array: [ True False True] String Array: [<span class="hljs-string">'apple'</span> <span class="hljs-string">'banana'</span> <span class="hljs-string">'cherry'</span>]</pre></div><h1 id="f659">Array Indexing in Numpy</h1><p id="74a8"><a href="https://numpy.org/devdocs/user/absolute_beginners.html#indexing-and-slicing"><b>Indexing and slicing</b></a> are supported by NumPy arrays, making it simple to access and modify elements. In NumPy, indexing begins at 0, just like in Python lists. Square brackets and the index can be used to access specific array elements:</p><figure id="cad6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*SDsmoUcgwAiGZXUqO6vQ_g.png"><figcaption>Image source: <a href="https://www.programiz.com/python-programming/numpy/array-indexing">https://www.programiz.com/python-programming/numpy/array-indexing</a></figcaption></figure><div id="dd55"><pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

my_array = np.array([<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>])

<span class="hljs-comment"># Access the first element</span> first_element = my_array[<span class="hljs-number">0</span>]

<span class="hljs-comment"># Access the last element</span> last_element = my_array[-<span class="hljs-number">1</span>]

<span class="hljs-built_in">print</span>(<span class="hljs-string">"First Element:"</span>, first_element) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Last Element:"</span>, last_element)</pre></div><p id="bbff"><b>The output will be:</b></p><div id="0c33"><pre>First Element: 10 Last Element: 50</pre></div><h1 id="dded">Array Slicing in Numpy</h1><p id="7707">By choosing a range of elements from an existing array, you can build a new array using slicing. The basic syntax for slicing is array_name<b>[start: stop: step]</b>. Let's see one example:</p><div id="43c8"><pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

my_array = np.array([<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>])

<span class="hljs-comment"># Slice the array from index 1 to 3 (exclusive)</span> sliced_array = my_array[<span class="hljs-number">1</span>:<span class="hljs-number">3</span>]

<span class="hljs-built_in">print</span>(<span class="hljs-string">"Sliced Array:"</span>, sliced_array)</pre></div><p id="2ca1"><b>The output will be:</b></p><div id="8f1c"><pre>Sliced Array: [20 30]</pre></div><p id="21cb"><b>Note: </b>By using slicing you can create new numpy arrays from the existing array.</p><h1 id="f7fa">Conclusion</h1><p id="c9b5">As a whole, we’ve studied NumPy arrays, learning about crucial parts including array creation, NumPy data types, and the techniques of array indexing and slicing. In the next article, we will cover array manipulation, broadcasting, and many more so keep studying and keep exploring.</p><p id="dfd7">If you learn new things from this article don't forget to give some claps, because it helps me a lot to write more articles for you. Thank You ❤🖤.</p><p id="9164"><a href="https://readmedium.com/how-to-install-numpy-on-your-system-640c230716ff">Previous>></a></p></article></body>

Use External Links to Increase Your Audience and Earnings

Learn the basics of using incoming and outgoing external links to improve the response to your content.

Credit: Jean Beaufort on Publicdomainpictures

As writers on Medium get more comfortable publishing on the platform they look for new ways to increase engagement with their content and their fan base. One way of doing this is to use external links. External links can be a great way to improve the authority of an article, positively impact SEO, and provide more support for author content not published on the platform.

One way to show search engines that you are creating quality content for your visitors. Linking to relevant, authoritative sites in your niche is a good way to do this. External links are one of the most important ranking factors in the Google algorithm.

What Are External Links

External links are hyperlinks that direct the reader to any domain other than the one the link is included on. If your content has a link to something on another site this is an external link. If another site links to your content this is an external link to your site, also known as a backlink or incoming link.

These are different from internal links which link to something on the same domain or website. If you link your article on Medium to another article on Medium, yours or someone else’s, this is an internal link. On Medium, this can be hyperlink or a text box that looks like this:

While on Medium you can also use a text link like the one above for external links, since many times you are linking to resource material of some sort, it’s best to use hyperlinks. This lets the viewers know if they need more information on a topic they can get it by going to the link, but it’s also easy for those who don’t need the information to glide over the link without it interrupting their reading.

For example, if you look at the first paragraph, you’ll see that SEO is a hyperlink. Most freelance writers have at least some familiarity with the term so they won’t need to click on it. For some newer writers, the term may still be unclear to them so they may want to remind themselves about what the term means. External links External links can be used along with internal links to positively influence the response to your writing.

Creating External Links in Your Content

To construct external links make sure to use descriptive keywords in your anchor text that represent the same topic as the page you are targeting. These phrases should be worked into the content naturally so they read well and don’t seem awkward or forced.

An example of good anchor text would be like you see in this sentence.

An example of bad anchor text would be like you see in this sentence (see here for example!)

The bold words would be the hyperlinks although they’d be here I have just underlined them so you get the idea.

To create a hyperlink in Medium, highlight the text that will be the anchor. A small box will pop up. Paste the URL into the box and press enter. You anchor text will should now be underlined and if you hover over it the URL should appear.

Authority

An authoritative link is one that goes to a site that is trusted by those that use it and Google. The site publishes high quality, well-researched and verifiable content that attracts links from similarly highly reputable sites. This also increases reader engagement with the content.

When adding external links to your articles, it’s important to make sure they don’t go to low quality sites and resources. Low-quality links go to sites that aren’t trustworthy, and may even be trying to manipulate search rankings. Links to low quality sites can hurt your Google rankings.

Popularity

External links are also used by search engines to determine the popularity of a site by search engines. Traffic is difficult for search engines to measure because these numbers are buried in private server logs. External links are publicly displayed and easily stored.

Relevancy

Anchor text is assumed to reflect the content of the page that the link goes to. The source page, target page and domain sited in the link also are used as relevancy metrics by search engines. Popularity and relevance are combined to determine the best results for a search query.

Backlinks

Backlinks are external links from another website or page to your website or page. They are often referred to as “votes of confidence” by one site in favor of another. They are messages to search engines that other sites vouch for your content. It a number of high quality sites link to your site search engines assume this means the content is worth linking to. Search engines also assume this means that the content is worth coming up on a Search Engine Response Page for a particular query.

Earning backlinks is one of the most important components of SEO. Like outgoing external links, backlinks are only valuable when they come from high authority, trustworthy, popular sites. Backlinks from spammy sites can hurt the authority and ranking or your page.

You can also use external links to connect your Medium posts to your other blog posts or website. Just remember that the quality, relevance and popularity of each site will be affected by the others that are linked to it or both in terms of incoming and outgoing external links.

If you enjoyed reading this article, you might also like these:

You can find links to my other work on Medium and follow me here. Thanks for reading!

SEO
Links
Writing
Writing Tips
Blogging
Recommended from ReadMedium