#๐Ÿ”’ numpy dot gives ValueError: setting an array element with a sequence

5 messages ยท Page 1 of 1 (latest)

static heron
#
import numpy as np

# Given parameters
m1 = 2
c1 = 3
x1 = 4
m2 = 1.5
c2 = 1
delta = 0.5

# Create a matrix 'm' (2x2) with custom coefficients
m = np.array([[m1, c1], [m2, c2]])
print("m = ",m)
# Calculate y1
y1 = np.dot(m, np.array([x1, 1]))  # [m11*x1 + m12, m21*x1 + m22]
print("y1 = ", y1)
# Calculate y2 using the sigmoid function
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

y2 = sigmoid(y1)
print("y2 = ", y2)

# Calculate y3
y3 = np.dot(m, np.array([y2, 1]).T)  # [m11*y2 + m12, m21*y2 + m22]

# Calculate y4 using the sigmoid function
y4 = sigmoid(y3)

# Check if y4 > delta
result = y4 > delta

print("Matrix m:\n", m)
print("y1:", y1)
print("y2:", y2)
print("y3:", y3)
print("y4:", y4)
print("y4 > delta:", result)

I am getting this error -

m =  [[2.  3. ]
 [1.5 1. ]]
y1 =  [11.  7.]
y2 =  [0.9999833  0.99908895]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-7919dd51c859> in <cell line: 25>()
     23 
     24 # Calculate y3
---> 25 y3 = np.dot(m, np.array([y2, 1]).T)  # [m11*y2 + m12, m21*y2 + m22]
     26 
     27 # Calculate y4 using the sigmoid function

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.
reef loomBOT
#

@static heron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

near canyon
#

based on the error it seems like the dimensions of the "m" matrix and the y2 array is different. thats why its unable to dot product

reef loomBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.