Arrays | NumPy | Python Methods and Functions

### Algorithm:

** **

** **** **

** Example — **

` ` |

** An example for a deeper understanding — **

Let`s assume that we have a large data set, each data item is a list of parameters. In Numpy, we have a two-dimensional array where each row represents a data item and the number of rows is — this is the size of the dataset. Suppose we want to apply some kind of scaling to all of this data, each parameter gets its own scaling factor or, say, each parameter is multiplied by some factor.

Just to be clear, let`s count the calories in foods using macronutrients. Roughly speaking, the caloric portions of food are composed of fat (9 calories per gram), protein (4 cpg), and carbohydrates (4 cpg). Thus, if we list some food items (our data) and for each food item we break down their macronutrients (parameters), then we can then multiply each nutrient by its calorie content (apply scaling) to calculate the calorie breakdown for each item. nutrition.

Thanks to this transformation, we can now compute all kinds of useful information. For example, what is the total number of calories present in any food, or given the breakdown of my lunch, find out how many calories I got from protein, and so on.

** Let`s see a naive way to create this calculation with Numpy: **

** exit:**

array ([[2.4, 8.7, 31.2], [157.2, 70.8, 292] , [165.6, 95.1, 191.2], [43.2, 33, 39.2]])

** Input data: ** array ** A with dimensions m and an array B with dimensions n**

p = max (m, n) if m

Broadcasting Rules:Broadcasting two arrays together follow these rules:

- If the arrays don`t have the same rank then prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The two arrays are compatible in a dimension if they have the same size in the dimension or if one of the arrays has size 1 in that dimension.
- The arrays can be broadcast together iff they are compatible with all dimensions.
- After broadcasting, each array behaves as if it had shape equal to the element-wise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension.

Example # 1:Single Dimension array

` ` |

` import ` ` numpy as np ` ` a ` ` = ` ` np.array ([` ` 17 ` `, ` ` 11 ` `, ` ` 19 ` `]) ` ` # 1x3 Dimension array ` ` print ` ` (a) ` ` b ` ` = ` ` 3 ` ` print ` ` ( b) ` ` # Broadcasting happened beacuse of ` ` # miss match in array Dimension ... ` ` c ` ` = ` ` a ` ` + ` ` b ` ` print ` ` (c) ` |

** Output: **

[17 11 19] 3 [20 14 22]

** Example 2: ** two-dimensional array

` ` |

** Exit: **

[[11 22 33] [10 20 30]] 4 [[15 26 37] [14 24 34]]

** Example 3: **

` ` |

** Exit:**

[[4 5] [8 10] [12 15]] [[2 4 6] [5 7 9]] [[5 6 7] [9 10 11]] [[5 6 7] [9 10 11]] [[2 4 6] [8 10 12]]

** Building a 2D function - **

Broadcast is also often used when displaying an image based on two-dimensional functions. If we want to define a function z = f (x, y).

** Example :**

` `

` ` ` import `

` numpy as np `

` import `

` matplotlib.pyplot as plt `

` # Calculates x and y coordinates for `

` # points on sine and cosine curves `

` x `

` = `

` np.arange (`

` 0 `

`, `

` 3 `

` * `

` np.pi, `

` 0.1 ``) `

` ` ` y_sin `

` = `

` np.sin (x) `

` `` y_cos `

=

` np.cos (x) `

` # Plot points using matplotlib `

` plt.plot (x, y_sin) `

` plt.plot (x, y_cos) `

` plt.xlabel (`

` `x axis label` `

`) `

` plt.ylabel (`

` `y axis label` `

`) `

` plt.title (`

` `Sine and Cosine` `

`) `

` plt.legend ([`

` `Sine` `

`, `

`` Cosine` `

`]) `

` plt.show () `

` `

` `

** Output: **

Black Hat Python, 2nd Edition: Python Programming for Hackers and Pentesters PDF, 2nd Edition. Fully updated for Python 3, the second edition of this worldwide bestseller (over 100,000 copies sold)...

22/08/2021

Big data is, admittedly, an overhyped buzzword used by software and hardware companies to boost their sales. Behind the hype, however, there is a real and extremely important technology trend with imp...

10/07/2020

This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ- ing a book has always been a bucket‐list item, and I was ver...

10/07/2020

Data and storage models are the basis for big data ecosystem stacks. While storage model captures the physical aspects and features for data storage, data model captures the logical representation and...

10/07/2020

X
# Submit new EBook