Read the Numbers From Scores.text and Output the Average, Minimum, and Maximum Numbers Python
Often when faced with a large amount of data, a first step is to compute summary statistics for the data in question. Perhaps the most common summary statistics are the hateful and standard deviation, which allow you to summarize the "typical" values in a dataset, but other aggregates are useful likewise (the sum, production, median, minimum and maximum, quantiles, etc.).
NumPy has fast built-in assemblage functions for working on arrays; we'll discuss and demonstrate some of them hither.
Summing the Values in an Array¶
As a quick example, consider computing the sum of all values in an array. Python itself can do this using the built-in sum
function:
In [2]:
L = np . random . random ( 100 ) sum ( L )
The syntax is quite like to that of NumPy's sum
role, and the effect is the same in the simplest case:
Nonetheless, considering information technology executes the operation in compiled code, NumPy'due south version of the functioning is computed much more quickly:
In [4]:
big_array = np . random . rand ( 1000000 ) % timeit sum(big_array) % timeit np.sum(big_array)
ten loops, all-time of three: 104 ms per loop 1000 loops, best of 3: 442 µs per loop
Be conscientious, though: the sum
office and the np.sum
function are non identical, which can sometimes lead to confusion! In particular, their optional arguments have different meanings, and np.sum
is aware of multiple array dimensions, as nosotros will come across in the following section.
Minimum and Maximum¶
Similarly, Python has built-in min
and max
functions, used to find the minimum value and maximum value of any given array:
In [5]:
min ( big_array ), max ( big_array )
Out[5]:
NumPy's corresponding functions have similar syntax, and once more operate much more than quickly:
In [6]:
np . min ( big_array ), np . max ( big_array )
Out[6]:
In [7]:
% timeit min(big_array) % timeit np.min(big_array)
10 loops, best of 3: 82.iii ms per loop 1000 loops, best of 3: 497 µs per loop
For min
, max
, sum
, and several other NumPy aggregates, a shorter syntax is to use methods of the array object itself:
In [viii]:
impress ( big_array . min (), big_array . max (), big_array . sum ())
1.17171281366e-06 0.999997678497 499911.628197
Whenever possible, make sure that you are using the NumPy version of these aggregates when operating on NumPy arrays!
Multi dimensional aggregates¶
One mutual type of aggregation operation is an aggregate along a row or column. Say y'all take some information stored in a ii-dimensional array:
In [ix]:
1000 = np . random . random (( iii , 4 )) print ( Grand )
[[ 0.8967576 0.03783739 0.75952519 0.06682827] [ 0.8354065 0.99196818 0.19544769 0.43447084] [ 0.66859307 0.15038721 0.37911423 0.6687194 ]]
By default, each NumPy aggregation function volition return the aggregate over the entire array:
Assemblage functions take an additional argument specifying the axis forth which the aggregate is computed. For instance, we can find the minimum value within each column by specifying centrality=0
:
Out[eleven]:
The function returns four values, corresponding to the iv columns of numbers.
Similarly, we can detect the maximum value within each row:
Out[12]:
The manner the axis is specified hither tin can exist confusing to users coming from other languages. The axis
keyword specifies the dimension of the assortment that will be complanate, rather than the dimension that will be returned. And so specifying axis=0
ways that the outset centrality will exist collapsed: for 2-dimensional arrays, this means that values inside each column will be aggregated.
Other aggregation functions¶
NumPy provides many other aggregation functions, but we won't discuss them in detail here. Additionally, most aggregates have a NaN
-safe counterpart that computes the outcome while ignoring missing values, which are marked by the special IEEE floating-point NaN
value (for a fuller word of missing data, see Handling Missing Data). Some of these NaN
-safe functions were non added until NumPy i.8, so they will non be available in older NumPy versions.
The following table provides a list of useful aggregation functions available in NumPy:
Function Name | NaN-safety Version | Description |
---|---|---|
np.sum | np.nansum | Compute sum of elements |
np.prod | np.nanprod | Compute product of elements |
np.mean | np.nanmean | Compute mean of elements |
np.std | np.nanstd | Compute standard difference |
np.var | np.nanvar | Compute variance |
np.min | np.nanmin | Detect minimum value |
np.max | np.nanmax | Find maximum value |
np.argmin | np.nanargmin | Find index of minimum value |
np.argmax | np.nanargmax | Find index of maximum value |
np.median | np.nanmedian | Compute median of elements |
np.percentile | np.nanpercentile | Compute rank-based statistics of elements |
np.whatever | North/A | Evaluate whether whatsoever elements are true |
np.all | N/A | Evaluate whether all elements are truthful |
Nosotros volition run into these aggregates oftentimes throughout the rest of the book.
Case: What is the Average Elevation of US Presidents?¶
Aggregates available in NumPy can exist extremely useful for summarizing a set of values. As a unproblematic example, permit's consider the heights of all US presidents. This data is available in the file president_heights.csv, which is a elementary comma-separated list of labels and values:
In [thirteen]:
!caput -4 data/president_heights.csv
society,proper noun,elevation(cm) ane,George Washington,189 2,John Adams,170 3,Thomas Jefferson,189
We'll use the Pandas package, which we'll explore more fully in Chapter three, to read the file and extract this data (annotation that the heights are measured in centimeters).
In [14]:
import pandas every bit pd data = pd . read_csv ( 'data/president_heights.csv' ) heights = np . array ( data [ 'pinnacle(cm)' ]) print ( heights )
[189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183 177 185 188 188 182 185]
Now that we have this data array, we can compute a variety of summary statistics:
In [15]:
impress ( "Mean summit: " , heights . mean ()) print ( "Standard departure:" , heights . std ()) print ( "Minimum summit: " , heights . min ()) print ( "Maximum height: " , heights . max ())
Mean height: 179.738095238 Standard departure: vi.93184344275 Minimum height: 163 Maximum acme: 193
Note that in each case, the aggregation operation reduced the unabridged array to a single summarizing value, which gives us data virtually the distribution of values. We may also wish to compute quantiles:
In [sixteen]:
print ( "25th percentile: " , np . percentile ( heights , 25 )) print ( "Median: " , np . median ( heights )) impress ( "75th percentile: " , np . percentile ( heights , 75 ))
25th percentile: 174.25 Median: 182.0 75th percentile: 183.0
Nosotros run across that the median meridian of United states of america presidents is 182 cm, or just shy of six anxiety.
Of course, sometimes information technology's more useful to encounter a visual representation of this data, which we tin can achieve using tools in Matplotlib (we'll discuss Matplotlib more fully in Chapter 4). For example, this lawmaking generates the following nautical chart:
In [17]:
% matplotlib inline import matplotlib.pyplot every bit plt import seaborn ; seaborn . fix () # set plot manner
In [18]:
plt . hist ( heights ) plt . title ( 'Summit Distribution of Usa Presidents' ) plt . xlabel ( 'height (cm)' ) plt . ylabel ( 'number' );
These aggregates are some of the key pieces of exploratory data analysis that we'll explore in more than depth in later chapters of the book.
Source: https://jakevdp.github.io/PythonDataScienceHandbook/02.04-computation-on-arrays-aggregates.html
0 Response to "Read the Numbers From Scores.text and Output the Average, Minimum, and Maximum Numbers Python"
Post a Comment