Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
2
Academic year
2023
anon
Views
9
Numpy, Statistical, Sorting, and SetOperations Several fundamental matrix operations, like addition, subtraction, and multiplication, were taught in earlier lectures. This time, though, we'll explore more intricateoperations like aggregates, sorting, and set operations that are frequently used indata analysis. Let's look at some of the features you'll frequently use. We'll start by making a two by four array and filling it with random values. The average value for the array'sitems will then be determined. Say you wish to calculate the average over all rows.Well, you might say "axis equals one" to do that. The average values for each roware included in the array of two components that we will get in return. Similarly, bysetting "axis = zero," we can figure out the averages for each column. For eachcolumn, the output will display the averages. Other techniques that can be helpful that work similarly to mean include "sum," for example. Let's imagine we want the medians broken down per row just to tieeverything together. The array must be passed to the function "median" in theNumPy library. To compute the medians, the axis is set to one. The median valuesfor each row are available. Let's now look at the sorting features that are built-in. We'll start by making a ten-item array with random elements. In some cases, we only need a duplicate of thesorted array and don't want to alter the array values itself. To accomplish that, wefirst make a copy of it and then sort it using the "sort" technique. The copy will besorted when we execute this, but the original won't have changed. Using the "sort"method would have allowed you to sort the original array in place if all you hadintended to do was that. Before moving on to set operations, I'd like to briefly introduce the function "unique". To choose only the unique values from an array of values, use the NumPy method"unique". The outcome will be a brand-new array with only distinct values. On ndarrays, set operations are also applicable. Let's construct two ndarrays that contain string elements. Desks, chairs, and bulbs are included in the initial array. Alamp, a lightbulb, and a chair are in the second array. The technique "intersect1d"will provide us those elements that are common to both arrays. Chair and a bulb arethe outcomes. The "union" method will return all of the distinct elements from botharrays, including the lamp, desk, chair, and bulb. Using "difference," we can look forcomponents that are present in one collection but not the other. The sole componentin set one that is not also present in set two is the desk. Similar to that, you can
request an array of Booleans that indicate whether or not each member in the arrayis present. We receive back false, true, and true because the desk is not true, but thechair and light are.
Numpy, Statistical, Sorting, and Set Operations
Please or to post comments