Question

Use Python 3 in Jupyter Write a function called countUnique that has one parameter which is...

Use Python 3 in Jupyter

Write a function called countUnique that has one parameter which is a NumPy array. The array will be a list of integers with many repeated values. The function should count how many times each unique integer appears and return the counts. For example, if it is given the array array([3, 3, 3, 1, 1, 3, 1, 1, 3, 2]), it should return the counts [(1, 4), (2, 1), (3, 5)] since 1 appears 4 times, 2 appears once, and 3 appears 5 times.

In a separate code cell, create an create a NumPy array of 20 random integers from 1 to 20. Use your countUnique to determine how many times each number appears.

In a separate code cell, create an create a NumPy array of 10,000 random integers from 1 to 5. Use your countUnique to determine how many times each number appears.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python 3 code

============================================================================================

import numpy as np
arr=np.random.randint(1,20,size=20)
print(arr)
unique_elements, counts_elements = np.unique(arr, return_counts=True)
for i in range(len(counts_elements)):
print("(",unique_elements[i],",",counts_elements[i],")",end=" ")

============================================================================================

Jupyter Output

for 1 to 5 10000 times

Add a comment
Know the answer?
Add Answer to:
Use Python 3 in Jupyter Write a function called countUnique that has one parameter which is...
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
  • In python, Write short programs (notebook cells) showing how to create & use the following data...

    In python, Write short programs (notebook cells) showing how to create & use the following data types: Tuples Ints Floats Dictionaries Sets Numpy array The programming details are provided in a separate Jupyter notebook file

  • Write a function in python, index(arr, value) to find indices of elements equal to some value...

    Write a function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Python code define a function car that takes 'swift' as a parameter function should count how...

    Python code define a function car that takes 'swift' as a parameter function should count how many letters in the string function should return a dictionary where key is lower case alpha caracter and value is the number of times that character appears in sentence. ex- input- that big car is so expensive output- 't':1, 'h':1, 'a':2, 'b':1, 'i':3,......

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords,...

    please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords, Y_coords) below, which should take two arrays of equal length containing the x-andy- coordinates for some number of particles. We'll use a very simple random walk algorithm • For each particle, choose a random angle between 0 and 2 • The particle moves by 1 unit of distance in the direction given by d.o. It is displaced by (Ax, Ay) (cos, sino). We'll do...

  • with python Write a function called linear_search which consumes a sorted list of integers and a...

    with python Write a function called linear_search which consumes a sorted list of integers and a number and linearly searches for the number in the list. Your function should return how many comparisons were made in order to find the element in the list. If the element is not in the list, your linear search should return -1 For example:

  • IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that...

    IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that is assumed to be greater than 1. The function will return a dictionary with keys the integers from 2 to n, inclusive, and with values the set of prime factors of each key. So, the command prime_factors.dict (8) will return the dictionary 2 123,3: 3),4 2),5 (53,6 2,3),7:7),8 {2)) Note that even though the prime number 2 appears twice in the prime fac- torization...

  • Create a program that counts how many times a number is included in an array. To...

    Create a program that counts how many times a number is included in an array. To create the array ask for 10 values (as integers) and then ask for the number that should be checked for. Then print the number of times the requested number appears in the array (see below). Please comment your code as you go along. Hint: You may need 2 for loops for this, one to create your array and one to check the values. Enter...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

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