Question

In: Computer Science

Q.1: Use the NumPy’s random number generation to create an array of five random integers that...

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.

Solutions

Expert Solution

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.


Related Solutions

Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Create an application containing an array that stores 5 integers. The application should call five methods...
Create an application containing an array that stores 5 integers. The application should call five methods from Array2 class that in turn (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the integers, (4) display all values less than a limiting argument, and (5) display all values that are higher than the calculated average value. Save the file as ArrayTest.java.
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested...
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested for loops over the elements of the matrix to find the number of prime elements , as well as the sum of the prime elements in the matrix. You can use MATLAB's built-in function isprime to check if an element is a prime number. You should have these variable: cnt (counts the number of prime elements) sm (sum of prime elements) In the command...
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT