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
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...
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...
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 ,...
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...
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...
Write a function, grade(score), which is given score, and it returns a letter grade — the...
Write a function, grade(score), which is given score, and it returns a letter grade — the grade for that mark — according to this scheme: A: >= 90 B: [80, 90) C: [70, 80) D: [60, 70) F: < 60 The square and round brackets denote closed and open intervals. A closed interval includes the number, and an open interval excludes it. So 79.99999 gets grade C, but 80 gets grade B. Define the main function as follows: In main,...
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...
Consider the supply and demand schedules for calzones at a local pizzeria. Use the information in...
Consider the supply and demand schedules for calzones at a local pizzeria. Use the information in the schedules to answer the five questions. Demand Price (P) $10 $9 $8 $7 $6 $5 $4 $3 $2 $1 Quantity (Q) 20 40 60 80 100 120 140 160 180 200 Supply Price (P) $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 Quantity (Q) 20 30 40 50 60 70 80 90 100 110 Consider the supply and demand schedules for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT