Question
python
Problem 3-6 points First, start with the following code: import numpy as np A np.random. randint (0, 10, sie n,n)) np. savetx
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

temp.ру х 1import numpy as np n=5 A np.random.randint(0 10, size (n, n)) np. savetxt(exam2.txt, A, i, delimiter np . zer

ètemp.py x LI 17 18 # trip is used to remove spaces and newlines here and # then split is used to split by comma separation B

Output:

e temp.py × 目exam2.txt 1:bash +曰 く TERMINAL import numpy as np n=5 A np.random.randint (0, 10, size-(n, n)) np.savetxt(exa

exam2.txt

temp.py exam2.txt x L2TERMINAL :bash 9,4,6,2 2 4,5,2,3,7 7,7,5,5,7 4 1.4,5,9,5 5 8,6,1,5,0 murky :load-and-match$ python temp

#Look out for the indentation and free feel to ask anything.

CODE:

import numpy as np
n = 5
A = np.random.randint(0, 10, size=(n, n))
np.savetxt('exam2.txt', A, '%i', delimiter=',')
B = np.zeros((n, n), dtype='int64')

################################################################################
# 1. Making A equal to B.

# readlines is used for reading the whole lines and put it into a list of lines
# where each element in the list corresponds to a line
f = open('exam2.txt').readlines()

# Enumerate allows us to loop over something and have an automatic counter.
for index, item in enumerate(f):
    # map yields a generator. So, we need to convert into list.
    # strip is used to remove spaces and newlines here and
    # then split is used to split by comma separation.
    B[index] = list(map(int, item.strip().split(',')))
print(B)

#################################################################################
# 2. Counting the number of elements greater than 5

# initialize count with 0
count = 0

# B is a numpy array. So it has shape property which returns the dimensions of the array
# shape[0] is for row and shape[1] is for column.
# print(B.shape)
for i in range(B.shape[0]):
    for j in range(B.shape[1]):
        if B[i][j] >= 5:
            count += 1
        else:
            pass
print("COUNT: ", count)

-------------------------------------------------------------

exam2.txt

3,9,4,6,2
4,5,2,3,7
7,7,5,5,7
1,4,5,9,5
8,6,1,5,0

Add a comment
Know the answer?
Add Answer to:
python Problem 3-6 points First, start with the following code: import numpy as np A np.random....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help completing code. (In python) Problem 2. Prepare the data for the lab In this...

    Need help completing code. (In python) Problem 2. Prepare the data for the lab In this problem you'll split the dataset into a training dataset and a test dataset and store them into separate arrays to work with: (a) Create a training set dataTR consisting of the first 500 and last 500 rows of the original dataset and a set dataTS with the remaining (middle) 372 rows. (b) Create 2D numpy arrays X-tr with rows [1.zi(1), Zi(2) , Zi(3) ,...

  • Python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt....

    python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt.gca().set_prop_cycle( ’ color ’ , [ ’ red ’ , ’ green ’ , ’ blue ’ , ’ black ’ ]) 6 7 class MyLine: 8 9 def __init__(self, * args, ** options): 10 #TO DO: IMPLEMENT FUNCTION 11 pass 12 13 def draw(self): 14 plt.plot(abscissa,self.line(abscissa)) 15 16 def get_line(self): 17 return "y = {0:.2f}x + {1:.2f}".format(self.slope, self.intercept) 18 19 def __str__(self):...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

  • Hi it's my code for python I almost finished my project but only one thing left which is most con...

    Hi it's my code for python I almost finished my project but only one thing left which is most confusing part please help me I have to find most occurring ending character from a to z For instance, output should be like this I have to find a to z. Words starting with ‘a’ end mostly with ‘O’ Words starting with ‘b’ end mostly with ‘O’ ...... No words start with ‘O’(If there's no word in the character from a...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

  • Hi it's python I imported a data which are so many words in txt and I arranged and reshaped with ...

    Hi it's python I imported a data which are so many words in txt and I arranged and reshaped with alphabetically both rows and columns I was successful with these steps but I am stuck with next step below is my code and screenshot import numpy as np import pandas as pd data=pd.read_csv("/Users/superman/Downloads/words_file2.txt",header=None) df_input=pd.DataFrame(data) df_output=pd.DataFrame(np.arange(676).reshape((26,26)), index = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'], columns = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) df_output.index.name="Start" df_output.columns.name="End" df_output This below screen shot is what I have to find I have to find each word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT