Question

In Python import numpy as np Given the array a = np.array([[1, 2, 3], [10, 20,...

In Python

import numpy as np

Given the array a = np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]]), compute and print

  1. the sums over all rows (should give [6, 60, 600])
  2. the sums over all columns (the sum of he first column is 111)
  3. the maximum of the array
  4. the maxima over all rows
  5. the mean of the sub-array formed by omitting the first row and column
  6. the products over the first two columns (hint: look for an appropriate ndarray method

In Python

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

Here is a code to do so along with the output:

The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.

Code:

import numpy as np
a = np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]])

# the axis = 1 refers to the row and axis = 0 refers to columns
a.sum(axis=1)
print(f"The sum over all rows is: {a.sum(axis=1)}" )


print(f"the sums over all columns is: {a.sum(axis=0)}" )

# max without any axis value means max of entire array

print(f"The max of the array is: {a.max()}" )

# max with axis means max along all those axes

print(f"The maxima over all rows is: {a.max(axis=1)}" )

# slice the 2-d array to get a matrix with the first row
# column ommited, then call mean to find the average
subarray = a[1:3,1:3]

print(f"The mean of the sub-array formed by omitting the first row and column is: {subarray.mean()}" )

# multiply after slicing the array
product = np.multiply(a[:,0], a[:,1])
print(f"The products over the first two columns is: {product}" )

Add a comment
Know the answer?
Add Answer to:
In Python import numpy as np Given the array a = np.array([[1, 2, 3], [10, 20,...
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 with this python progam using numpy Create a 4x4 two dimensional array with numbers...

    need help with this python progam using numpy Create a 4x4 two dimensional array with numbers from 1 thru 16. show this then: Change the last element by dividing it in half. Show that the original array has changed. show this then: Set all values in row 2 to zero. Show the original array contains this change show this then: Set all values in column 1 to one. Shoe the original array contains this change. show this then: Display the...

  • Hi. It's a python and I got an error below comment import numpy as np arr=np.genfromtxt("/Volumes/Samsung...

    Hi. It's a python and I got an error below comment import numpy as np arr=np.genfromtxt("/Volumes/Samsung SSD 860 EVO 500GB Media/Download/primenumbers.txt", dtype=int) arr=arr.reshape(-1,1) arr.shape def find_cat(x): if x<= 300: return '<=300' elif x <= 600: return '<=600' else: return '<=1000' arr2 = np.apply_along_axis(find_cat, axis=1, arr=arr) arr2 = arr2.reshape(-1,1) arr3 = np.hstack((arr, arr2)) arr_300 = np.array((col[0] for col in arr3 if col[i]=='<=300'), dtype=int) arr_300 arr_300.shape count_300=len(arr_300) count_300 avg_300=round(np.mean(arr_300, 2) avg_300 print("Number of items in category \ "<=300\"= (one), and average of...

  • python Problem 3-6 points First, start with the following code: import numpy as np A np.random....

    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. savetxt ('exam2.txt', A, fmt-idelimiter B-# np. zeros ( (n, n) , dtypes, int 64, ) When run, it will produce a file named "exam2.txt" that has 5 rows each with 5 numbers separated by commas,. In addition, a 5 by 5 array of zeros named B is defined. Run this code, but do not change it Your job...

  • 7. i) Let n and k be some given positive integers, and x a 1-dimensional NumPy...

    7. i) Let n and k be some given positive integers, and x a 1-dimensional NumPy array of length n. Write a Python code that creates the 2-dimensional NumPy array which has k columns all identical to x. You may import numpy as np Perform the test case: ne5 k-3 x = np.arange(0,1,0.2) # the output should be [[. 0. 0.] [0.2 0.2 0.2] [0.4 0.4 0.4) (0.6 0.6 0.6) [0.8 0.8 0.8]] 7. ii) Let a, b, c be...

  • do it in python 1. Import the proper libraries: Pandas and NumPy and create aliases pd,...

    do it in python 1. Import the proper libraries: Pandas and NumPy and create aliases pd, np respectively. 2. Load sample data (car_loan.csv) into data frame: df 3. Export Pandas DataFrames to csv. Save file name as out.csv. hint: help(df.to_csv) 4. Run the command: df.info (). What do you see, how many columns? also what about number of entries for each column 5. It is often the case where you change your column names or remove unnecessary columns. a. Change...

  • Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a...

    Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a statement that assigns 145 to the first column of the first row of this array. 2. Write a statement that assigns 18 to the last column of the last row of this array. values is a two-dimensional array of floats, with 10 rows and 20 columns. Write code that sums all of the elements in the array and stores the sum in the variable...

  • PYTHON import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import...

    PYTHON import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split Our goal is to create a linear regression model to estimate values of ln_price using ln_carat as the only feature. We will now prepare the feature and label arrays. "carat"   "cut" "color"   "clarity"   "depth"   "table"   "price"   "x"   "y"   "z" "1" 0.23   "Ideal" "E" "SI2" 61.5 55 326   3.95   3.98   2.43 "2" 0.21   "Premium" "E" "SI1"...

  • 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):...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multi...

    D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multiple types (e.g., ints and strings) in its individual element positions. A NumPy array object can be instantiated using multiple types (e.g., ints and strings) in the list passed to its constructor O Memory freeing will require a double-nested loop. The number of bits used to store a particular NumPy array object is fixed. O The numpy.append(my.array, new_list) operation mutates...

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