Question
Click on New in the upper right-hand side of the screen, then select Python 3 (pykernel) to open up a new Jupyter Notebook file. Copy
Click on New in the upper right-hand side of the screen, then select Python 3 (pykernel) to open up a new Jupyter Notebook file. Copy the code snippets below into the In window and run the code. Copy the results below each code snippet and briefly explain the actions the code performed. Click on the plus sign, to open a new In window for each code snippet.
a) Creating and reshaping arrays
1)
import numpy as np
a_arr = np.array((1, 2, 3, 4))
a_arr
2)
b_arr = np.arange(0, 16)
b_arr
3)
c_arr = b_arr.reshape(4, 4)
c_arr
4)
d_arr = np.arange(10, 26).reshape(4, 4)
d_arr
b) Accessing and altering elements within an array,
1)
a_arr[2]
2)
a_arr[2] = 5
a_arr
3)
a_arr.dtype
a_arr.ndim
a_arr.shape
a_arr.size
c) Aggregate functions
1)
d_arr.sum()
d_arr.min()
d_arr.max()
d_arr.mean()
d_arr.median()
d_arr.std()
d) Arithmetic Operations, Matrix Operations, Broadcasting, and Comparisons
1) Arithmetic operations
a_arr + 5
e_arr = c_arr + d_arr
e_arr
e_arr = c_arr - d_arr
e_arr
e_arr = c_arr * d_arr
e_arr
e_arr = d_arr / c_arr
e_arr
Question:
• What is the problem with the statement above
e_arr = np.sqrt(d_arr)
e_arr
2) Matric operations
f_arr = np.dot(c_arr, d_arr)
f_arr
3) Broadcasting
g_arr = a_arr * c_arr
g_arr
4) Comparisons
c_arr< 6
c_arr = c_arr * 10
c_arr < f_arr
e) Joining, Splitting, and Splicing Arrays
1) Joining
w_arr = np.vstack((c_arr, f_arr))
w_arr
w_arr = np.hstack((c_arr, f_arr))
w_arr
2) Splitting
x_arr = np.hsplit(c_arr, 2)
x_arr
x_arr = np.vsplit(c_arr, 2)
x_arr
3) Slicing
One dimensional array
b_arr[2:10:3]
b_arr[:10:3]
b_arr[::3]
Two-dimensional array
c_arr[0:1, :]
c_arr[0:1, 1:3]
c_arr[1:3, 1:3]
f) Saving and Loading Arrays
1) Saving
np.save('c_arr.npy',c_arr)
2) Loading
np.load('c_arr.npy')
Step by Step Solution
3.36 Rating (152 Votes )
There are 3 Steps involved in it
Step: 1
a Creating and reshaping arrays 1 python import numpy as np aarr nparray1 2 3 4 aarr Result array1 2 3 4 Explanation Created a onedimensional NumPy array aarr with elements 1 2 3 and 4 2 python barr n...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started