In: Computer Science
Q.1:
Use the NumPy’s random number generation to create an array of five random integers that represent summertime temperatures in the range 60–100, then perform the following tasks:
a. Convert the array into the Series named temperatures and display it.
b. Determine the lowest, highest and average temperatures.
c. Produce descriptive statistics for the Series.
Q.2:
Given the following dictionary;
temps = {'Mon': [68, 89], 'Tue': [71, 93], 'Wed': [66, 82], 'Thu': [75, 97], 'Fri': [62, 79]}
perform the following tasks:
a. Convert the dictionary into the DataFrame named temperatures with Low and High as the indices, then display the DataFrame.
b. Use the column names to select only the columns for Mon through Wed.
c. Use the row index Low to select only the low temperatures for each day.
d. Set the floating-point precision to 2, then calculate the average temperature for each day.
e. Calculate the average low and high temperatures.
Q.3:
Given the following array:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
write statements to perform the following tasks:
a. Select the second row.
b. Select the first and third rows.
c. Select the middle three columns.
Q.4:
Use NumPy function arange to create an array of 20 even integers from 2 through 40, then reshape the result into a 4-by-5 array.
Q.5:
Use NumPy random-number generation to create an array of twelve random grades in the range 60 through 100, then reshape the result into a 3-by-4 array. Calculate the average of all the grades, the averages of the grades in each column and the averages of the grades in each row.
Question 1
import numpy as np #import numpy package
import pandas as pd #import pandas package
arr = np.random.randint(60,100, 5) #generate 5 random values between 60 to 100
temperatures = pd.Series(arr) #convert array to series
print(temperatures) #display series
print("Minimum Temperature: ", min(temperatures)) #minimum temperature
print("Maximum Temperature: ", max(temperatures)) #maximum temperature
print("Average Temperature: ", temperatures.mean()) #average temperature
print(temperatures.describe()) #descriptive statistics for the Series
Question 2
temps = {'Mon': [68, 89], 'Tue': [71, 93], 'Wed': [66, 82], 'Thu': [75, 97], 'Fri': [62, 79]}
temperatures = pd.DataFrame(temps, index=['Low', 'High']) #create a dataframe with Low and High as Indices
print(temperatures) #display dataframe
print(temperatures.loc[:, 'Mon':'Wed'])# select columns from Monday to Wednesday
print(temperatures.loc['Low']) #select low temperatures for each day
print(round(temperatures.mean(axis=0),2)) #average temperature of each day
print(temperatures.mean(axis=1)) # average low and high temperatures
Question 3
arr = np.array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
print(arr[1]) #select second row
print(arr[[0,2]]) #select first and third row
print(arr[:, 1:4]) #select middle three columns
Question 4
a = np.arange(2, 41, 2) # array of 20 even integers from 2 to 40
print(a) #print the array before reshape
a = a.reshape(4,5) #reshape the array into 4x5
print(a) # print the array after reshape
Question 5
grade = np.random.randint(60, 100, 12) #create array of random number between 60 to 100
grade = grade.reshape(3,4) #reshape the array in 3x4
print(grade.mean()) #average of all grades
print(grade.mean(axis=0)) #column wise mean
print(grade.mean(axis=1)) #row wise mean
Screenshot with output
Question 1
Question 2
Question 3
Question 4
Question 5
Comments has been given for each line of code. Screenshot of the code as well as the output has been attached for the reference.