<jupyter_start><jupyter_text>Selecting values from an array<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>We use arrays all the time, in data science.<jupyter_code>my_array = np.array([10, 11, 12])<jupyter_output><empty_output><jupyter_text>One of the most common tasks we have to do on arrays, is to select values.We do this with *array slicing*.We do array slicing when we follow the **array variable name** with `[` (opensquare brackets), followed by **something to specify which elements we want**,followed by `]` (close square brackets).For example:<jupyter_code>my_array[0]<jupyter_output><empty_output><jupyter_text>The example is an example of the simplest case is when we want a single elementfrom a one-dimensional array.  In that case the thing between the `[` and the`]` is the *index* of the value that we want.The *index* of the first value is 0, the index of the second value is 2, and soon.<jupyter_code>my_array[0]<jupyter_output><empty_output><jupyter_text>We start by loading data from a Comma Separated Value file (CSV file).This is summary data that [<NAME>](https://asrosen.com) downloaded from for an analysis he reported in [a 2018paper](https://asrosen.com/wp-content/uploads/2018/07/postprint_rmp-1.pdf). Thedata file here is from the [supplementarymaterial](https://www.tandfonline.com/doi/suppl/10.1080/02602938.2016.1276155);it has the average rating across academic discipline for all professors in aparticular discipline who have more than 20 ratings.See [the dataset page](../data/rate_my_professors) for moredetail.If you are running on your laptop, you should download the{download}`rate_my_course.csv ` file to the samedirectory as this notebook.Don't worry about the code next cell.  It loads the data we need from a datafile. We will come on the machinery to do this very soon.<jupyter_code># This code we have not covered yet.  We will cover it soon.
# It loads the data file, and makes some arrays.

# Load the library for reading data files.
import pandas as pd
# Safe settings for Pandas. We'll come on to this later.
pd.set_option('mode.chained_assignment', 'raise')
# Read the file.
courses = pd.read_csv('rate_my_course.csv')
# Sort the courses by number of rated teachers, most first.
courses_by_n = courses.sort_values(
    'Number of Professors', ascending=False)
# Select the top eight easiest courses.
big_courses = courses_by_n.head(8)
big_courses
# Again - don't worry about this code - we will cover it later.
# Put the columns into arrays
disciplines = np.array(big_courses['Discipline'])
easiness = np.array(big_courses['Easiness'])
quality = np.array(big_courses['Overall Quality'])<jupyter_output><empty_output><jupyter_text>We now have the names of the disciplines with the largest number of professors.<jupyter_code>disciplines<jupyter_output><empty_output><jupyter_text>Here we get the first value.  This value is at index 0.<jupyter_code># Get the first value (at index position 0)
disciplines[0]<jupyter_output><empty_output><jupyter_text>Notice that this is another way of writing:<jupyter_code>disciplines.item(0)
# Get the second value (at index position 1)
disciplines[1]
# Get the third value (at index position 2)
disciplines[2]<jupyter_output><empty_output><jupyter_text>At first this will take some time to get used to, that the first value is atindex position 0.  There are good reasons for this, and many programminglanguages have this convention, but it does a while to get this habit of mind.  Square brackets and indexingNotice too that we use square brackets for indexing.We have seen square brackets before, when we create lists.  For example, we cancreate a list with `my_list = [1, 2, 3]`. (We usually then create an array withsomething like `my_array = np.array(my_list)`.  This is *a different use ofsquare brackets*. When we create a list, the square brackets tell Python thatthe elements between the brackets should be the elements of the list.  This useis called a *list literal* expression.  The square brackets follow an equalsign, or some other operator, or start the line.When we use square brackets for indexing, the square brackets always follow an expression that returns an array.  For example, consider this line:<jupyter_code>disciplines[2]<jupyter_output><empty_output><jupyter_text>`disciplines` is an expression that returns an array.  Therefore the open square brackets follows this expression, and therefore, Python can tell that this use of square brackets means *select an element or elements from the array*.So we have seen different uses of square brackets:* Creating a list (a *list literal*); (often uses in making arrays).* Indexing into an array.Python can tell which of these two we mean, from the context.  Indexing with negative numbersIf we know how many elements the array has, then we can get the last element by using the number of elements, minus one (why?).Here the number of elements is:<jupyter_code>len(disciplines)<jupyter_output><empty_output><jupyter_text>So, the last element of the array is at index position 7:<jupyter_code>disciplines[7]<jupyter_output><empty_output><jupyter_text>In fact, there is a short cut for getting elements at the end of the array, andthat is to use an offset with a minus in front.  The number is then the offsetfrom one past the last item.  For example, here is another way to get the lastelement:<jupyter_code>disciplines[-1]<jupyter_output><empty_output><jupyter_text>The last but one element:<jupyter_code>disciplines[-2]<jupyter_output><empty_output><jupyter_text>Index with slicesSometimes we want more than one element from the array.  For example, we might want the first 4 elements from the array.  We can get these using an array *slice*.  It looks like this:<jupyter_code># All the elements from offset zero up to (not including) 4
disciplines[0:4]
# All the elements from offset 4 up to (not including) 8
disciplines[5:11]<jupyter_output><empty_output><jupyter_text>You can omit the first number, if you mean to start at offset 0:<jupyter_code>disciplines[:4]<jupyter_output><empty_output><jupyter_text>You can omit the last number if you mean to end at the last element of the array:<jupyter_code>disciplines[4:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07570168404170008, 'fraction_numerical': 0.012510024057738572, 'mean_word_length': 5.71259418729817, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 66, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '49882', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1806, 'n_words': 783, 'path': 'arrays/array_indexing.ipynb', 'repo_name': 'uob-ds/cfd2021', 'size': '6241'}