Module 4-Part
I
Broadcasting:
Broadcasting is a mechanism in NumPy that allows arithmetic
operations on arrays of different shapes by automatically
expanding the smaller array so that the shapes are compatible—
without copying data.
Broadcasting allows an operator or a function to act on two or more arrays
even if these arrays do not have the same shape. Two arrays can be
subjected to broadcasting when all their dimensions are compatible, i.e.,
the length of each dimension must be equal or one of them must be equal
to 1.
Example:
Rules for broadcasting:
1. First you must add a 1 to each missing dimension. . If the
compatibility rules are now satisfied, you can apply broadcasting
and move to the second rule.
Example:
2. The rule of compatibility is met. Now apply the second rule of
broadcasting. This rule explains how to extend the size of the
smallest array so that it’s the size of the biggest array, so that the
element-wise function or operator is applicable.
The second rule assumes that the missing elements (size, length 1)
are filled with replicas of the values contained in extended sizes .
After applying the second broadcasting rule,
Reading and Writing Array Data
on Files
This is a very common data analysis operation, since the size of the
dataset to be analyzed is almost always huge.
NumPy provides a set of functions that allow data analysts to save
the results of their calculations in a text or binary file. Similarly,
NumPy allows you to read and convert written data in a file into an
array.
NumPy provides a pair of functions, called save() and load(), that
enable you to save and then later retrieve data stored in binary
format.
[Link] and Saving Data in Binary File
Once you have an array to save, simply call the save() function and
specify as arguments the name of the file and the array. The file will
automatically be given the .npy extension.
Example:
[Link] Files with Tabular Data
Tabular data means data arranged in rows and columns (e.g., CSV,
TSV, TXT, Excel). Take for example the case of a set of data in the
CSV (Comma-Separated Values) format, in which data are collected
in a tabular format and the values are separated by commas.
Example : file name: “ch3_data.csv”
To be able to read your data in a text file and insert values into an
array, NumPy provides a function called genfromtxt().
Normally, this function takes three arguments
[Link] name of the file containing the data,
[Link] character that separates the values from each other (in this
case, a comma),
[Link] the data contain column headers.
How Ndarray differs from list? Demonstrate
create NDarray, Intrinsic creation of an array
and dtype option.
How One Dimensional array is converted into amatrix?
Explain with example.