Question
Please help I am really confused I wrote a python code Listed below: Please help me to get it to work I am getting the
Please help I am really confused I wrote a python code Listed below: Please help me to get it to work I am getting the below error I also took a picture of the error and attached it. I am trying to utilize the Matrix Multiplication with the matmul numpy command and it is erroring out. Thank you so much for your help.
import re
import numpy as np
while True:
print("Do you want to play the Matrix Game?")
play = input("Enter Y for Yes or N for No: ")
if play == 'N':
print("*********** Thanks for playing Python Numpy ***************")
break
p_num = input("Enter your phone number (XXX-XXX-XXXX): ")
found = re.search('\d\d\d-\d\d\d-\d\d\d\d', p_num)
while not(found and len(p_num) == 12):
p_num = input(
"Your phone number is not in correct format. Please renter: ")
found = re.search('\d\d\d-\d\d\d-\d\d\d\d', p_num)
zip_code = input("Enter your zip code+4 (XXXXX-XXXX):")
z_found = re.search('\d\d\d\d\d-\d\d\d\d', zip_code)
while not(z_found and len(zip_code) == 10):
zip_code = input(
"Your zip code is not in correct format. Please renter: ")
z_found = re.search('\d\d\d\d\d-\d\d\d\d', zip_code)
first_matrix = []
print("Enter your first 3x3 matrix:")
# we need to split it with something so i added it as space
for _ in range(3):
row = input().split(' ')
row = [int(num) for num in row]
first_matrix.append(row)
print("Your first 3x3 matrix is:")
for row in first_matrix:
row = [str(num) for num in row]
print(" ".join(row))
second_matrix = []
print("Enter your second 3x3 matrix:")
# we need to split it with something so i added it as space
for _ in range(3):
row = input().split(' ')
row = [int(num) for num in row]
second_matrix.append(row)
print("Your second 3x3 matrix is:")
for row in second_matrix:
row = [str(num) for num in row]
print(" ".join(row))
# printing the both matrix for better understanding
print("First matrix:")
print(first_matrix)
print('Second matrix:')
print(second_matrix)
menu = "Select a Matrix Operation from the list below: \
a. Addition \
b. Subtraction \
c. Matrix Multiplication \
d. Element by element multiplication "
selection = input(menu)
# initialize the matrix with 0
matrix_result = [[0,0,0],
[0,0,0 ],
[0,0,0]]
if selection == 'a':
print("You selected Addition.")
matrix_result = np.add(first_matrix, second_matrix)
elif selection == 'b':
print("You selected Subtraction.")
matrix_result = np.subtract(first_matrix, second_matrix)
elif selection == 'c':
print("You selected Matrix Multiplication.")
matrix_result = np.matmul(first_matrix, second_matrix)
elif selection == 'd':
print("You selected Element by element multiplication.")
matrix_result = np.multiply(first_matrix, second_matrix)
print("The results are:", end=" ")
for row in matrix_result:
for num in row:
print(num, end=" ")
print()
transpose = matrix_result.transpose()
print("The Transpose is:", end=" ")
for row in transpose:
for num in row:
print(num, end=" ")
print()
print("The row and column mean values of the results are:")
row_means = np.round(matrix_result.mean(axis=1), 2)
print("Row:", end=" ")
for num in row_means:
print(num, end=" ")
print()
column_means = np.round(matrix_result.mean(axis=0), 2)
print("Column:", end=" ")
for num in column_means:
print(num, end=" ")
print()
This is the error I am getting:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last)input-2-5493972cb501> in <module> 113 print("You selected Matrix Multiplication.") 114 --> 115 matrix_result = np.matmul(first_matrix, second_matrix) 116 117 elif selection == 'd': ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 1)
Could you please fix for me
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started