Objectives · Demonstrate use of map and filter commands · Practice list comprehensions and lambda functions · Understand different ways to manipulate and perform calculations on Numpy arrays...

1 answer below »


Objectives


· Demonstrate use of map and filter commands


· Practice list comprehensions and lambda functions


· Understand different ways to manipulate and perform calculations on Numpy arrays including:


· Changing datatypes, getting the max values in each column and row, calculating the mean of a matrix


· Matrix value comparisons and slicing of values


· Load a file into a pandas dataframe and demonstrate some filtering



General Guidelines:


· All calculations need to be done in the functions (that includes any formatting of the output)


· Name your functions exactly as written in the problem statement


· Please have your functions return the answer rather than printing it inside the function


· Do not make a separate input() statement. The functions will be passed the input as shown in the examples


· The examples given are samples of how we will test/grade your code. Please ensure your functions output the same information


· Answer format is graded - please match the examples


· Docstrings and comments in your code are strongly suggested but won't be graded


· This homework is mostly auto-graded. The blank code blocks are the auto-grading scripts - please do not delete these!


· Your code needs to be written in the #Your Code Here blocks or it wont be graded correctly.



1. List, Mapping, Filtering, and Reducing Comprehensions (30 points)


A. Write a function namedcomputebelow that:


· Returns the square of any number for integer or floating type values


· Returns the reverse of the content if it is a string


· Or returns thevalue None(not the string 'None') if it is another kind of value.


·
def
compute(value):


·
# YOUR CODE HERE


· B. Now apply thecomputefunction made in 1A. (above) usingmapto a list of values. Write thismapstatement in the new function namedmap_compute. Return the values as alist(not amap_object). You should be able to do this in ONE single line!


· Example function call:


·
map_compute(['cup', '321', 12, ['x'], True])


· Expected returned output:


·
['puc', '123', 144, None, None]


·
def
map_compute(my_list):

·
# the solution should be written in one line

·
# YOUR CODE HERE

C. Write a new function namedfilter_computeto remove anyintegervalues that are less than 5 in yourmapresult and return this result as alist. You should be able to this in ONE single line. Some pseudo-code below:


· Step 1: Usemapto applycomputefunction from Part 1A to your list (as you did above in 1B)


· Step 2: Usefilterto apply the above constraint to your output of Step 1. You might want to re-factor your filter logic in a separate function (it won't be counted against the single line constraint imposed above)


Example function call:filter_compute(['cup', '321', 2, ['x'], 4])


Expected returned output:


['puc', '123', None, 16]



def
filter_compute(my_list):


# the solution should be written in one line


# do not forget to return the computed value


# YOUR CODE HERE


D. Now that you are an expert in maps and filters, write the above logic from Part 1B & 1C in ONE line using 2lambdafunctions, 1 for themap, and 1 for thefilterpart in the function namedlambda_compute. This should be one line with one lambda for the map and one lambda for the filter. DoNOTuse the lambdas to call the functions you already made in the parts above, instead make the same logic in a lambda function.


Hint: It could be easier to start with separate lambda functions and then combine then into one.





def

lambda_compute(my_list):
# the solution should be written in one line with lambda calls
# do not forget to return the computed value
# YOUR CODE HERE








2. NumPy (40 points)


In this next section, we will work with NumPy. For each question below, use NumPy functionality to execute the logic of your answer. (Do not use

For

loops!)


Generating Random Data and Filtering That Data


Please run the two cells below to set your random seed. (This makes the 'randomness' of the array the same for everyone)





import

numpy
as
np
np.random.seed(25) ar = np.random.randn(1000)





# Do NOT change this code!

ar = ar * 100 ar = ar.astype('int8') ar


A. Write code in the functionreshapethat 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 originalararray with it.









def
reshape(my_array):


# YOUR CODE HERE


ar = reshape(ar)


B. Now thataris a matrix, create a function namedrow_min_maxto 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).


For example: if my_matrix is:



4 1 5


5 7 5


4 5 6

The returned value should be:



([5,7,6], [1,5,4])




def

row_min_max(my_matrix):
# YOUR CODE HERE
row_min_max(ar)



C. Write a function namedless_than_meanthat returns the totalnumberof values in thearmatrix that are less than the mean of all values in the matrix. This should be a single number.






def

less_than_mean(my_matrix):
# YOUR CODE HERE
less_than_mean(ar)



D. Write a function calledmax_positionthat returns the position (as a tuple) of the max value in the passed matrix.


For example: ifmy_matrixis:



4 1 5


5 6 7


4 5 6

The returned value should be:



(1, 2)



def
max_position(my_matrix):


# YOUR CODE HERE


max_position(ar)


E. Lastly, write a function namedselect_sumthat returns the sum of all the values of a slicedarmatrix. Slice thearmatrix by:


· values greater than 100 or


· values less than 5 and greater than or equal to -20




def
select_sum(my_matrix):


# YOUR CODE HERE


select_sum(ar)


3. Data loading and indexing using Pandas (30 points)


A. For this part of the homework, we will work with the airline safety dataset. As the first step, load the dataset fromairline-safety.csvby defining theload_datafunction below. Have this function return the following (in order):


1. The entire data frame


2. Total number of rows


3. Total number of columns



def
load_data():


# YOUR CODE HERE


df, num_rows, num_cols = load_data()

B. Write the functionselect_airlinesthat returns the ith to jth airline name as alist(both inclusive) from the dataframe.


For example: if i=2 and j=5, your code should return 4 airline names: airline name from the 3rd, 4th, 5th and 6th row (assuming that the first row starts from 0).



Note on loc and iloc

Selection Using Label:


To select a column of a DataFrame by column label, the safest and fastest way is to use the .loc method. General usage looks like frame.loc[rowname,colname]. (Reminder that the colon : means "everything"). For example, if we want the color column of a data frame, we would use : ex.loc[:, 'color']


You can also slice across columns. For example, frame.loc[:, 'Name':] would give select the columns Name and the columns after.



def
select_airlines(df, i, j):


# YOUR CODE HERE


sliced_df = select_airlines(df, 2, 5)

C. Filtering or slicing with boolean arrays


In your quest for cleaner data, you will undoubtedly filter your data at some point: whether it be for clearing up cases with missing values, culling out fishy outliers, or analyzing subgroups of your data set. Note: compound expressions have to be grouped with parentheses.



A filter example:

df[df[column name]



Write the functionfatalities_filterto return just the airline names with more than 100 and less than 300 fatalities, between the years 85 and 99. Your function needs to return alistofairline names only.




def
fatalities_filter(df):


# YOUR CODE HERE


fatalities_filter(df)


Answered Same DayNov 02, 2021

Answer To: Objectives · Demonstrate use of map and filter commands · Practice list comprehensions and lambda...

Sampad Swarup answered on Nov 03 2021
154 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. List, Mapping, Filtering, and Reducing Comprehensions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def compute(x): #maping logic\n",
" if type(x) == type(''): # checking type string\n",
" return x[::-1] # returning the reversed string\n",
" else:\n",
" if type(x) == type(1.0) or type(x) == type(1): # checking for int and float values\n",
" re
turn x**2\n",
" else:\n",
" return None\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['puc', '123', 144, None, None]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def map_compute(my_list):\n",
" return list(map(compute, my_list)) # maping the list. Then listing and returning the mapped value.\n",
"\n",
"map_compute(['cup', '321', 12, ['x'], True])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['puc', '123', 4.0, None, 16]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def filter_list(x): #filtering logic\n",
" if type(x) == type(1): #checking type int\n",
" if x < 5:\n",
" return False\n",
" else:\n",
" return True\n",
" else:\n",
" return True\n",
"\n",
"def filter_compute(my_list):\n",
" my_list = map_compute(my_list) # using the map_compute fuction\n",
" my_list = filter(filter_list,my_list) # filtering the list using filtering logic\n",
" return list(my_list)\n",
"\n",
"filter_compute(['cup', '321', 2, 2.0, ['x'], 4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"D:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(['puc', '123', 144, 4, None, None], ['puc', '123', 144, None, None])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def lambda_compute(my_list):\n",
" #using lambda to raplicate the map operation\n",
" my_list_map = list(map(lambda x : x[::-1] if type(x) == type('') else (x**2 if type(x) == type(1.0) or type(x) == type(1) else None), my_list))\n",
" #using lambda to raplicate the filter operation\n",
" #here I'm filtering the my_list_map for better demonstration\n",
" my_list_filter = list(filter(lambda x : False if type(x) == type(1) and x < 5 else True, my_list_map))\n",
" return my_list_map, my_list_filter\n",
"\n",
"lambda_compute(['cup', '321', 12, 2, ['x'], True])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Generating Random Data and Filtering That Data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape of ar::(1000,)\n"
]
}
],
"source": [
"import numpy as np\n",
"np.random.seed(25)\n",
"\n",
"ar = np.random.randn(1000)\n",
"ar = ar * 100 \n",
"ar = ar.astype('int8')\n",
"\n",
"print(f'Shape of ar::{ar.shape}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After reshape function shape of ar::(200, 5)\n"
]
}
],
"source": [
"def reshape(my_array):\n",
" my_array = np.array(my_array) #making sure that parsed array is a numpy array\n",
" if len(my_array) == 1000:\n",
" return my_array.reshape(200, 5)#reshapeing the array\n",
" else:\n",
" print('Enter array of len 1000.')\n",
"ar = reshape(ar)\n",
"print(f'After reshape function shape of ar::{ar.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([102,\n",
" 86,\n",
" 123,\n",
" 120,\n",
" 99,\n",
" 59,\n",
" 73,\n",
" 63,\n",
" 68,\n",
" 10,\n",
" 117,\n",
" 118,\n",
" 94,\n",
" 57,\n",
" 112,\n",
" 49,\n",
" 119,\n",
" 104,\n",
" 121,\n",
" 19,\n",
" 102,\n",
" 56,\n",
" 122,\n",
" 17,\n",
" 52,\n",
" 20,\n",
" 54,\n",
" 101,\n",
" 43,\n",
" -16,\n",
" 112,\n",
" 93,\n",
" 64,\n",
" 59,\n",
" 62,\n",
" 92,\n",
" 63,\n",
" 101,\n",
" 90,\n",
" 120,\n",
" 111,\n",
" 97,\n",
" 64,\n",
" 40,\n",
" 98,\n",
" 106,\n",
" 96,\n",
" 80,\n",
" 71,\n",
" 59,\n",
" 73,\n",
" 24,\n",
" 57,\n",
" 68,\n",
" 65,\n",
" 120,\n",
" 117,\n",
" 94,\n",
" 102,\n",
" 28,\n",
" 2,\n",
" 93,\n",
" 66,\n",
" 58,\n",
" 111,\n",
" 83,\n",
" 126,\n",
" 17,\n",
" 111,\n",
" 111,\n",
" 46,\n",
" 70,\n",
" 121,\n",
" 124,\n",
" 89,\n",
" 98,\n",
" 43,\n",
" 98,\n",
" 106,\n",
" 94,\n",
" 100,\n",
" 78,\n",
" 78,\n",
" 45,\n",
" ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here