# Help with Regression Model Training

**URL:** <https://discuss.pennylane.ai/t/help-with-regression-model-training/4615>\
**Category:** PennyLane Help\
**Created:** [May 17, 2024, 2:17am UTC](https://discuss.pennylane.ai/t/help-with-regression-model-training/4615 "2024-05-17T02:17:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkajsfbksdjf](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/fkajsfbksdjf/32/2576_2.png) [@fkajsfbksdjf](https://discuss.pennylane.ai/u/fkajsfbksdjf)\
**Post date:** [May 17, 2024, 2:17am UTC](https://discuss.pennylane.ai/t/help-with-regression-model-training/4615/1 "2024-05-17T02:17:46Z")

</div>

Hello,

I am very new to Pennylane and have very little Python experience.

I am doing this for a school project and I just need help correcting the issue:  
I am trying to do a regression of several Y variables vs one single X variable.

I am attempting to use Hybrid Quantum-Classical ML to a problem application.

Here is the code:

```auto
import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

# Load the dataset
data = pd.read_csv('file.csv')

X = data['X'].values
Y = data['Y'].values

# Normalize or scale your features
X_normalized = (X - np.min(X)) / (np.max(X) - np.min(X))
Y_normalized = (Y - np.min(Y)) / (np.max(Y) - np.min(Y))

# Split into training and testing datasets
X_train, X_test, Y_train, Y_test = train_test_split(X_normalized, Y_normalized, test_size=0.2, random_state=42)

# Create the quantum device
dev = qml.device('default.qubit', wires=1)

# Define the quantum circuit
@qml.qnode(dev)
def quantum_circuit(datapoint, params):
    qml.RX(datapoint, wires=0)
    qml.Rot(params[0], params[1], params[2], wires=0)
    return qml.expval(qml.PauliZ(wires=0))

# Classical pre/postprocessing and loss function
def loss_func(predictions, targets):
    return np.mean((predictions - targets) ** 2)

# Define the cost function
def cost_fn(params, X, Y):
    predictions = [quantum_circuit(x, params) for x in X]
    return loss_func(predictions, Y)

# Define the optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.3)

# Initialize parameters
params = np.array([0.1, 0.1, 0.1], requires_grad=True)

# Training loop
for i in range(100):
    print("Params shape:", params.shape) # Debug: Check params shape
    params, prev_cost = opt.step_and_cost(lambda p: cost_fn(p, X_train, Y_train), params)
    if i % 10 == 0:
        print(f'Step {i}, Cost {prev_cost}')

# Generate predictions for testing
test_predictions = [quantum_circuit(x, params) for x in X_test]

# Plotting results
plt.scatter(X_train, Y_train, color='blue', label='Training Data')
plt.scatter(X_test, Y_test, color='red', label='Test Data')
plt.scatter(X_test, test_predictions, color='green', label='Predictions', marker='x')
plt.xlabel("Normalized X")
plt.ylabel("Normalized Y")
plt.title("Quantum Regression Model Results")
plt.legend()
plt.show()

```

* * *

Error:  
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) TypeError: float() argument must be a string or a real number, not ‘ArrayBox’ The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) Cell In[36], line 49 47 for i in range(100): 48 print(“Params shape:”, params.shape) # Debug: Check params shape —\> 49 params, prev\_cost = opt.step\_and\_cost(lambda p: cost\_fn(p, X\_train, Y\_train), params) 50 if i % 10 == 0: 51 print(f’Step {i}, Cost {prev\_cost}') File /opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient\_descent.py:64, in GradientDescentOptimizer.step\_and\_cost(self, objective\_fn, grad\_fn, \*args, \*\*kwargs) [44](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:44) def step\_and\_cost(self, objective\_fn, \*args, grad\_fn=None, \*\*kwargs): [45](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:45) “”“Update trainable arguments with one step of the optimizer and return the corresponding [46](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:46) objective function value prior to the step. [47](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:47) (…) [61](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:61) If single arg is provided, list [array] is replaced by array. [62](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:62) “”” —\> [64](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:64) g, forward = self.compute\_grad(objective\_fn, args, kwargs, grad\_fn=grad\_fn) [65](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:65) new\_args = self.apply\_grad(g, args) [67](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:67) if forward is None:

…

→ [129](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/numpy/core/_methods.py:129) ret = ret.dtype.type(ret / rcount) [130](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/numpy/core/_methods.py:130) else: [131](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/HQML/lib/python3.12/site-packages/numpy/core/_methods.py:131) ret = ret / rcount ValueError: setting an array element with a sequence.

* * *

Any help is appreciated.

Thank you!

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [May 17, 2024, 9:19pm UTC](https://discuss.pennylane.ai/t/help-with-regression-model-training/4615/2 "2024-05-17T21:19:46Z")

</div>

Hi @fkajsfbksdjf ,

You were actually quite close! I didn’t have access to your data so I tested this with some random y=x data.  
It seems that the issue was being caused by getting the square of `(predictions-targets)`. In the code below I calculated the square for each pair `(predictions[i]-targets[i])` and you no longer see the error you had before. Moreover, you see some nice predictions.

I noticed a lot of similarity in your code with the example at the end of my blog post on [how to start learning quantum machine learning](https://pennylane.ai/blog/2021/10/how-to-start-learning-quantum-machine-learning/). Did you use it? Or did you find the base code somewhere else? I hope it helped you get started! Make sure to give it a quick mention or citation when using it. 😀

```auto
import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

# Load the dataset
'''data = pd.read_csv('file.csv')

X = data['X'].values
Y = data['Y'].values'''
# Example data
X = np.random.random(100, requires_grad=False)
Y = X + np.random.random(100, requires_grad=False)/10

# Normalize or scale your features
X_normalized = (X - np.min(X)) / (np.max(X) - np.min(X))
Y_normalized = (Y - np.min(Y)) / (np.max(Y) - np.min(Y))

# Split into training and testing datasets
X_train, X_test, Y_train, Y_test = train_test_split(X_normalized, Y_normalized, test_size=0.2, random_state=42)

# Create the quantum device
dev = qml.device('default.qubit', wires=1)

# Define the quantum circuit
@qml.qnode(dev)
def quantum_circuit(datapoint, params):
    qml.RX(datapoint, wires=0)
    qml.Rot(params[0], params[1], params[2], wires=0)
    return qml.expval(qml.PauliZ(wires=0))

# Classical pre/postprocessing and loss function
def loss_func(predictions, targets):
    return np.mean(np.array([(predictions[i] - targets[i]) **2 for i in range(len(targets))]))#np.mean((predictions - targets)** 2)

# Define the cost function
def cost_fn(params, X, Y):
    predictions = [quantum_circuit(x, params) for x in X]
    return loss_func(predictions, Y)

# Define the optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.3)

# Initialize parameters
params = np.array([0.1, 0.1, 0.1], requires_grad=True)

# Training loop
for i in range(100):
    #print("Params shape:", params.shape) # Debug: Check params shape
    params, prev_cost = opt.step_and_cost(lambda p: cost_fn(p, X_train, Y_train), params)
    if i % 10 == 0:
        print(f'Step {i}, Cost {prev_cost}')

# Generate predictions for testing
test_predictions = [quantum_circuit(x, params) for x in X_test]

# Plotting results
plt.scatter(X_train, Y_train, color='blue', label='Training Data')
plt.scatter(X_test, Y_test, color='red', label='Test Data')
plt.scatter(X_test, test_predictions, color='green', label='Predictions', marker='x')
plt.xlabel("Normalized X")
plt.ylabel("Normalized Y")
plt.title("Quantum Regression Model Results")
plt.legend()
plt.show()

```

![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/9/98e587574f8564b1c08802eaed589a69f665f246.png)

---

<div class="post-metadata">

**Author:** ![fkajsfbksdjf](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/fkajsfbksdjf/32/2576_2.png) [@fkajsfbksdjf](https://discuss.pennylane.ai/u/fkajsfbksdjf)\
**Post date:** [May 17, 2024, 9:36pm UTC](https://discuss.pennylane.ai/t/help-with-regression-model-training/4615/3 "2024-05-17T21:36:38Z")

</div>

Thank you. I will follow up when I test on real data. Yes I pattered this after your code and article. You are actually in my references and citations already ❤

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [May 17, 2024, 9:37pm UTC](https://discuss.pennylane.ai/t/help-with-regression-model-training/4615/4 "2024-05-17T21:37:49Z")

</div>

Oh that’s great to hear @fkajsfbksdjf ! Thank you.
