In: Computer Science
(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).
# 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: