Question

In: Computer Science

Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1...

  1. Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1 to 3 lines.

(a) Create an 8 × 8 array with ones on all the edges and zeros everywhere else.

(b) Create an 8 × 8 array of integers with a checkerboard pattern of ones and zeros.

(c) Given the array c = np.arange(2, 50, 5), make all the numbers not divisible by 3 negative.

(d) Find the size, shape, mean, and standard deviation of the arrays you created in parts (a)–(c).

Solutions

Expert Solution

# do comment if any problem arises

# Code

a)

import numpy

#create numpy array of 1's

array_2d=numpy.ones([8,8],dtype='int')

#set all non edge elements to 0

array_2d[1:-1,1:-1]=0

#print 2d array

print(array_2d)

Output:

b)

import numpy

#create numpy array of 1's

array_2d=numpy.zeros([8,8],dtype='int')

#set every second block ie alternate block to 1

array_2d[::2,::2]=1

array_2d[1::2,1::2]=1

#print 2d array

print(array_2d)

Output:

3)

import numpy as np

c = np.arange(2, 50, 5)

#loop through all numbers

for i in range(len(c)):

    #if number is not divisible by 3 make it -ve

    if c[i]%3!=0:

        c[i] = -c[i]

print(c)

Output:

d)

for 1)

import numpy

#create numpy array of 1's

array_2d = numpy.ones([8, 8], dtype='int')

#set all non edge elements to 0

array_2d[1:-1, 1:-1] = 0

row,column=array_2d.shape

print(f'Size: {row*column}')

print(f'Shape: {row}x{column}')

print(f'Mean: {numpy.mean(array_2d)}')

print(f'Standard daviation: {round(numpy.std(array_2d),4)}')

Output:

2)

import numpy

#create numpy array of 1's

array_2d = numpy.zeros([8, 8], dtype='int')

#set every second block ie alternate block to 1

array_2d[::2, ::2] = 1

array_2d[1::2, 1::2] = 1

row,column=array_2d.shape

print(f'Size: {row*column}')

print(f'Shape: {row}x{column}')

print(f'Mean: {numpy.mean(array_2d)}')

print(f'Standard daviation: {round(numpy.std(array_2d),4)}')

Output:

3)

import numpy as np

c = np.arange(2, 50, 5)

#loop through all numbers

for i in range(len(c)):

    #if number is not divisible by 3 make it -ve

    if c[i] % 3 != 0:

        c[i] = -c[i]

print(c)

print(f'Size: {len(c)}')

print(f'Shape: {c.shape}')

print(f'Mean: {np.mean(c)}')

print(f'Standard daviation: {round(np.std(c),4)}')

Output:


Related Solutions

1.Employees often perform work tasks simply because their bosses tell them to do so. This is...
1.Employees often perform work tasks simply because their bosses tell them to do so. This is an example of __________ power. expert legitimate referent reward 2. Brianna is trying to increase her chances of being promoted to vice president by working to build good work relationships with other managers outside her own department.  Brianna's behavior should be viewed as dysfunctional politics. functional politics. coercive power. functional influence. 3. Tara wants the Vice President of the Gingerbread Factory to know about the...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Describe the types of arrays and operations that can be performed of them. [10
Describe the types of arrays and operations that can be performed of them. [10
For this case study assignment, perform the following tasks: 1. Select an American company with a...
For this case study assignment, perform the following tasks: 1. Select an American company with a worldwide presence (examples: Starbucks, McDonalds, Walmart). 2. Do research on the worldwide economic crisis of 2008 and in particular, focus on the company selected. 3. Discuss how your chosen company faired in the economic crisis of 2008. 4. Discuss the microeconomic implications of the crisis on your company. 5. Discuss whether your company was immune or not immune to the crisis 6. Discuss the...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
1.All of the following can be done when the resources that provide capacity are fixed EXCEPT:a....
1. All of the following can be done when the resources that provide capacity are fixed EXCEPT:a. workforce schedulingb. production schedulingc. both a and b can be done when the resources that provide capacity are fixedd. none of the above2. Which of the following BEST describes process management?a. it increases capacity by removing constraint points (bottlenecks) in the systemb. it can increase capacity by redesigning workflowsc. techniques such as queuing theory rearrange resources to achieve more customers in the systemd....
Managers at all levels of an organisation need adequate information to perform their respective tasks. One...
Managers at all levels of an organisation need adequate information to perform their respective tasks. One study investigated the effect the source has on the dissemination of information. In this particular study the sources of information were a superior, a peer and a subordinate. In each case, a measure of dissemination was obtained, with higher values indicating greater dissemination of information. To verify whether the source of information significantly affects dissemination, you perform the ANOVA test. The following table partially...
Managers at all levels of an organisation need adequate information to perform their respective tasks. One...
Managers at all levels of an organisation need adequate information to perform their respective tasks. One study investigated the effect the source has on the dissemination of information. In this particular study the sources of information were a superior, a peer and a subordinate. In each case, a measure of dissemination was obtained, with higher values indicating greater dissemination of information. To verify whether the source of information significantly affects dissemination, you perform the ANOVA test. The following table partially...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name and program title on the output screen. 2. Display instructions for the user. 3. Prompt the user to enter two numbers. 4. Calculate the sum, difference, product, (integer) quotient and remainder of the numbers. 5. Display a terminating message. Requirements: 1. The main procedure must be divided into sections:  introduction  get the data  calculate the required values  display the results...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT