Question

Write a function reshaped_by_col(nums, col) which takes a Python list nums, and an integer value for...

Write a function reshaped_by_col(nums, col) which takes a Python list nums, and an integer value for columns that reshapes the 1D nums into col number of columns. Finally, returns the reshaped numpy array. If the shape cannot be reshaped, return None (you can do this without catching exceptions).

For example:

Test Result
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
col = 2
print(reshaped_by_col(nums, col))
[[ 10  20]
 [ 30  40]
 [ 50  60]
 [ 70  80]
 [ 90 100]]

Homework Answers

Answer #1
import numpy as np
def reshaped_by_col(nums,col):
    nums=np.array(nums)
    # finding the length of nums array
    length=len(nums)

    # if length is dicisible by col than answer is possible else answer will be None
    if length%col==0:
        # finding no of rows for the reshaped array
        row_size=length//col

        # reshaping matrix with reshape function of numpy
        reshaped=nums.reshape(row_size,col)
        return reshaped
    else:
        return None

# array which needs to be reshaped
nums=[10,20,30,40,50,60,70,80,90,100]

# number of columns in reshaped 2 -D array
col=2

# calling hte reshaped funciton and printing the reshaped array
print(reshaped_by_col(nums, col))
Know the answer?
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
Similar Questions
Write a Python program prints out the unique elements of a list lst in a sorted...
Write a Python program prints out the unique elements of a list lst in a sorted manner (i.e it removes duplicates from the list). For example, if list is [10,20,30,20,10,50,60,40,80,50,40], the output should be [10, 20, 30, 40, 50, 60, 80].?
A doctor that works in the hospital thinks that based on her prior experience, the average...
A doctor that works in the hospital thinks that based on her prior experience, the average ease of intubation in obese patients is around 45. Conduct a randomization test for a mean to assess whether the doctor’s hypothesis is plausible.Report the p-value. Interpret the p-value in terms of rejecting the null hypothesis or failing to reject the null hypothesis. Data collected: ease 5 20 80 80 20 2 10 60 15 0 70 85 10 15 60 70 60 30...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter an number. Then, it asks for a second number. These will use the "input" command. Store these as two separate variables. This function takes NO arguments and is also a VOID function, there is no return statement. The function should print the result of these two numbers multiplied together. The program should be able to multiply floats. For example: Test Input Result calculateInput() 5...
1. Find the Nash Equilibria of the following games. (Some may have more than one!) Player...
1. Find the Nash Equilibria of the following games. (Some may have more than one!) Player 2 D E F Player 1 A 30 , 30 20 , 0 0 , 10 B - 60 , 60 50 , 50 10 , 10 C 10 , 80 25 , 30 60 , 90 Player 2 E F G H Player 1 A 20 , 20 -5 , -5 15 , 90 15 , 15 B 5 , 70 30 ,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Python: Write a program that prompts the user to enter a positive integer value, and compute...
Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message containing the word Error and exit the program....
You are given the following data for your firm, which sells the only commercially-available Wifi-enabled cheese...
You are given the following data for your firm, which sells the only commercially-available Wifi-enabled cheese grater. Q P TC 0 $100 $1,000 10 $95 $1,211 20 $90 $1,411 30 $85 $1,609 40 $80 $1,814 50 $75 $2,035 60 $70 $2,281 70 $65 $2,561 80 $60 $2,884 90 $55 $3,259 100 $50 $3,695 QUESTION: The profit-maximizing quantity occurs at _______ and the profit-maximizing price occurs at _________. (Since MC is in terms of Q2, solving with calculus and algebra can...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
In what follows use any of the following tests/procedures: Regression, confidence intervals, one-sided t-test, or two-sided...
In what follows use any of the following tests/procedures: Regression, confidence intervals, one-sided t-test, or two-sided t-test. All the procedures should be done with 5% P-value or 95% confidence interval. The empirical literature suggests that class absenteeism undermines students’ academic performance and that an enforced mandatory attendance policy may be beneficial. Open Attendance data. Using data on 90 second-year students enrolled in a Statistics course, test if students who are assiduous in attending lectures have higher final grades than students...