Question

In: Computer Science

In this problem you will need to read about the functions numpy.load and numpy.bincount. Store all...

In this problem you will need to read about the functions numpy.load and numpy.bincount. Store all answers in appropriate variables. numbers = np.load('randomints.npy')

Use only numpy functionality to carry out this part.

(a) Load the file randomints.npy into a numpy array. There are 100000 integers in this file and they are between 0 and 1000.

(b) Get a bincount array for the array loaded in the first part.

(c) Let M be the largest number of times an element occurs in the array.

What is the smallest number that occurs M times?

(d) What is the number of integers that occurred 111 times?

Solutions

Expert Solution

Solution: It has been assumed that the 'randomints.npy' file already exist. In case it does not exist and you would like to create the same, please run the following Code 1 one time only. (Please do not run this one time code if the file already exists)

Hope that helps.

Code 1 (one time code)

import numpy as np
import random

#Empty list
lst_int = []
#Add 100000 random numbers to the list 
for i in range(100000):
        lst_int.append(random.randint(0,1000))

# converting list to array
arr = np.array(lst_int)

np.save('randomints.npy',arr)

Main Solution to the various points have been given below :

The code has been tested it on a file with 10 random integers. The output has been given for the same. The code will work for 100000 random integers without any amendments.

#import
import numpy as np

# Problem Part a
# Load the file contents to an arrary b
b = np.load('randomints.npy')

#Print the array 
print("Array from the file")
print (b)

#Get the corresponding bin_count array
print("Bin Count Array")
b_bit = np.bincount(b)

#Print the bin count array
print(b_bit)

#Maximum number of times an integer occurs M
#This can be found from the bincount array which stores occurances
M = np.max(b_bit)

#Print value of M for reference
print(f"Maximum Number of times: {M}")

#Enumerate the Bin Count array  
#Break on the first index 
#This will give the minimum number which occur
#Variable min_M will hold the value

for idx, x in np.ndenumerate(b_bit):
    if x == M:
        number = idx
        min_M = number[0]
        break

print(F"Minimum number which occurs Maximum times: {min_M}")

#Find number of integers which occured 111 times
#We will store this sum in OCC_111 variable

OCC_111 = 0

for idx, x in np.ndenumerate(b_bit):
    if x == 111:
        OCC_111 = OCC_111 + 1

print (f"Number of integers which occur 111 times : {OCC_111}")


Output

Array from the file
[10 3 10 3 6 2 5 3 5 5]
Bin Count Array
[0 0 1 3 0 3 1 0 0 0 2]
Maximum Number of times: 3
Minimum number which occurs Maximum times: 3
Number of integers which occur 111 times : 0


Related Solutions

The Problem Below are a series of problems you need to solve using recursive functions. You...
The Problem Below are a series of problems you need to solve using recursive functions. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. Implementation Each recursive function MUST have a wrapper function enclosing it where you will do input/output file processing as well...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to analyze the information below from BOTH the management AND the employee perspective. This information pertains to a labor union in a simulated/made up/not real firm in Glen Ellyn. The first part of your information relates to Management – the second part relates to the Labor Union employees. I have provided you with information from the last union negotiations at the plant in 2016. It...
The reference standard for all of our functions is 1 bar. This problem has you compute...
The reference standard for all of our functions is 1 bar. This problem has you compute values at the old standard of 1 atm in order to assess the difference when producing new standard tables. Consider the vaporization of water at 25 degrees Celsius. Treat the vapor as ideal. H2O(l) to H2O(g) Thermodynamic Values at 1 bar standard. H(l) = -285.83 kJ/mol H(g) = -241.82 kJ/mol S(l) = 69.91 J/mol-K S(g) = 188.83 J/mol-K a.)Calculate the equilibrium constant for this...
Read each problem carefully and clearly show all work. Indicate calculator functions used (ie T-Test, 2-SampTTest,...
Read each problem carefully and clearly show all work. Indicate calculator functions used (ie T-Test, 2-SampTTest, etc) as well as calculator entries State your conclusions in terms of the context of the problem. Does the time of day that a class meets affect exam grades? Exam grades for an 8am statistics class and a 3pm statistics class taught by the same professor were compared. The summary data are given in the table below. Assuming both populations are approximately normally distributed,...
​ Before you start to type an answer, read the articles provided and think about all...
​ Before you start to type an answer, read the articles provided and think about all of things you have heard and read since the recent tax reform legislation. You will be awarded points for grammar, punctuation, spelling, and overall neatness. In your response, you must reference at least one of the articles provided. Here are the links to the articles: https://www.journalofaccountancy.com/news/2017/dec/tax-reform-bill-changes-for-individuals-201718070.html https://www.journalofaccountancy.com/news/2017/dec/congress-passes-tax-reform-201718091.html https://www.journalofaccountancy.com/news/2017/dec/president-signs-tax-cuts-jobs-act-201718112.html https://tax.thomsonreuters.com/checkpoint/individual-tax-changes-tax-reform-2017/?utm_campaign=SR.TR.INDIV.EL_1217&utm_medium=email&utm_source=Eloqua&site_id=82769734&elqTrackId=48423f0738ac41938fd5563259cb2922&elq=9cd800d5c2604b50b31a0405eb6373eb&elqaid=16613&elqat=1&elqCampaignId=12437 You are not required to give specific details about your financial situation, but please ask...
C++ requirements All values must be read in as type double and all calculations need to...
C++ requirements All values must be read in as type double and all calculations need to be done using type double. For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview This program will convert a set...
C++ requirements All values must be read in as type double and all calculations need to...
C++ requirements All values must be read in as type double and all calculations need to be done using type double. For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview This program will convert a set...
Problem 1 Part A (11 Marks) Note: It is suggested that you read all both parts...
Problem 1 Part A Note: It is suggested that you read all both parts of Problem 1 before you start so that you structure your space requirements for the answer appropriately. Several friends grouped themselves as shareholders and Emerald Delivery Limited opened for business September 1 2018. The transactions for the month are listed. You are to create the journal entries in good form for these transactions. Sept 12015 - Shareholders started the company with 20,000 of their own cash...
If you needed to store facts about information that you were collecting, you would store these...
If you needed to store facts about information that you were collecting, you would store these facts in the ___. What is hardware? What replicates and spreads itself?
I have a C problem. I need to read the data of a txt file to...
I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort. The txt file and the struct like aa.txt 1 2 3 4 ***************************** struct aaa{ int num1,num2; }bbb[2]; num1 for 1,3, num2 for 2 4 I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT