{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy\n", "For each question below, use NumPy functionality to execute the logic of your answer. (**Do not use ```For```...

1 answer below »
Hello! I have one question here that I need some help with answering. I am just starting to learn numPy and could use some assistance


{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy\n", "For each question below, use NumPy functionality to execute the logic of your answer. (**Do not use ```For``` loops!**)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Generating Random Data and Filtering That Data\n", "\n", "Please run the two cells below to set your random seed. (This makes the 'randomness' of the array the same for everyone)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(25)\n", "ar = np.random.randn(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Do NOT change this code!\n", "ar = ar * 100\n", "ar = ar.astype('int8')\n", "ar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A. Write code in the function reshape that takes an array of size 1000 and reshapes the array into a set of five columns with 200 rows. Return this new array and replace the original ar array with it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def reshape(my_array):\n", " # YOUR CODE HERE\n", " \n", "#Check\n", "ar = reshape(ar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "B. Now that ar is a matrix, create a function named row_min_max to get the maximum and minimum values from the matrix, row wise. This function returns a tuple of a list of row max values and a list of row min values (in that order). Be sure to remember how many total values you should be outputting (one for every row).\n", "\n", "For example: if my_matrix is:\n", "\n", "4 1 5\n", "5 6 7\n", "4 5 6\n", "The returned value should be:\n", "\n", "([5,7,6], [1,5,4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def row_min_max(my_matrix):\n", " # YOUR CODE HERE\n", "\n", "#Check\n", "row_min_max(ar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "C. Write a function named less_than_mean that returns the total number of values in the ar matrix that are less than the mean of all values in the matrix. This should be a single number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def less_than_mean(my_matrix):\n", " # YOUR CODE HERE\n", "\n", " \n", "#Check\n", "less_than_mean(ar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "D. Write a function called max_position that returns the position (as a tuple) of the max value in the passed matrix.\n", "\n", "For example: if my_matrix is:\n", "\n", "4 1 5\n", "5 6 7\n", "4 5 6\n", "The returned value should be:\n", "\n", "(1, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def max_position(my_matrix):\n", " # YOUR CODE HERE\n", "\n", " \n", "#Check\n", "max_position(ar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "E. Lastly, write a function named select_sum that returns the sum of all the values of a boolean array indexed ar matrix. Use the boolean array index of the ar matrix by:\n", "\n", "values greater than 100 -boolean or-\n", "values less than 5 -boolean and- greater than or equal to -20" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def select_sum(my_matrix):\n", " # YOUR CODE HERE\n", "\n", "#Check\n", "select_sum(ar)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }
Answered Same DayMar 30, 2021

Answer To: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy\n", "For each question...

Devanshu answered on Mar 31 2021
136 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy\n",
"For each question below, use NumPy functionality to execute the logic of your answer. (**Do not use ```For``` loops!**)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Generating Random Data and Filtering That Data\n",
"\n",
"Please run the two cells below to set your random seed. (This makes the 'randomness' of the array
the same for everyone)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"np.random.seed(25)\n",
"ar = np.random.randn(1000)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 22, 102, -83, -59, -95, -22, -61, -73, 51, 86, -92,\n",
" -23, -41, 123, 7, -124, 120, -104, 105, -41, -27, -3,\n",
" 26, 68, 99, 59, 53, -29, -51, -58, 22, 73, -96,\n",
" 38, 39, 40, 21, -63, 24, 63, 11, 68, 24, -70,\n",
" 36, -50, -22, -56, 10, -55, 109, -90, -62, -94, 117,\n",
" -37, -54, 28, -20, 118, 72, -22, 94, 31, -116, 31,\n",
" 47, 57, 50, -62, -34, 50, 76, 112, 16, -96, 49,\n",
" -55, -8, -60, -6, -85, 42, -27, 119, -9, 83, -78,\n",
" 104, -64, 67, -9, 121, 49, -114, -53, -91, -21, 19,\n",
" -32, 84, 76, 44, 43, 102, -58, 4, 56, -114, 0,\n",
" -29, 23, 67, 122, -60, -16, -65, -19, 17, 3, -126,\n",
" -53, 52, 38, -76, -5, -24, 20, -30, 15, 3, -32,\n",
" -77, -33, 54, -115, -74, 101, 96, -18, -54, -109, 43,\n",
" -73, 16, -67, -16, -54, -35, -88, 9, 112, -45, 87,\n",
" -114, -85, 93, -17, 41, -28, -90, 48, -4, 64, 1,\n",
" -97, 59, -126, -100, -48, 39, 62, 53, 37, -52, 30,\n",
" -66, -19, 92, -84, -77, -120, -87, -128, 63, 50, -39,\n",
" -64, -50, 101, -118, 40, -126, 90, 73, -126, 120, -36,\n",
" 87, 84, -121, 111, -103, -44, 5, -56, 0, 17, 97,\n",
" -70, 11, 64, 1, -43, -6, -6, -39, 40, -74, -65,\n",
" -16, -115, 25, -98, 98, -53, -26, -115, -14, 106, 54,\n",
" -118, -28, -99, 96, 80, -56, -92, -41, 0, 47, 71,\n",
" -96, 45, -11, -118, -108, -108, 59, 8, -88, 30, 59,\n",
" -17, 73, 14, -15, 24, -26, -37, 34, -102, 57, -126,\n",
" 8, -82, 68, -103, -111, 9, 15, -79, -59, -55, 65,\n",
" -20, 120, -83, -40, 69, -57, 61, 19, 117, 11, 94,\n",
" 15, -125, -117, 4, -110, -37, -50, 102, 27, -64, -17,\n",
" -17, 28, 2, -124, -25, -89, -88, 2, 41, 31, 93,\n",
" 77, 40, -116, 66, -6, -113, 64, 1, 58, 57, 7,\n",
" -44, 3, -109, 74, 111, -23, -87, 23, -80, 21, 83,\n",
" -60, 6, 126, 27, -60, -118, -29, -119, -120, 17, 73,\n",
" -1, -114, -7, 111, 8, 28, 35, -54, 111, -110, -12,\n",
" -1, 46, -54, 2, 70, 14, -37, 46, 88, -61, 121,\n",
" 74, 88, -30, 89, -50, 4, 124, -59, -65, 89, 23,\n",
" 82, -70, 98, -9, -67, -31, -6, 43, 11, -12, 0,\n",
" -18, 47, 58, 72, 98, -20, 94, 6, -10, 106, 43,\n",
" 66, -77, 58, 94, 100, -97, -103, -104, 12, 78, 55,\n",
" -40, 78, -65, -117, 47, -98, 78, 8, 45, 20, -110,\n",
" 16, 21, 64, 71, -51, -52, -97, -100, 22, -109, -45,\n",
" 53, -6, -27, -127, 6, -51, -51, 45, -2, 24, 86,\n",
" -103, 57, -18, -93, -127, -117, 55, -108, 119, -23, 35,\n",
" 125, 68, -39, 54, -74, -46, -121, -7, 37, -33, -20,\n",
" -86, -117, 74, 22, 72, 61, 60, -46, 105, 27, -12,\n",
" -28, -31, 61, 40, -125, 4, 6, -77, -30, 116, -35,\n",
" 89, 63, 80, -103, -28, -112, 43, -97, -70, -41, 123,\n",
" -25, -92, -59, 41, -45, -77, -24, -21, 117, 68, 0,\n",
" 69, 17, 62, 50, 42, -5, 71, 5, 56, ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here