Mlp is not defined

hello i am tring to create the neural network to predict the bitcoin ...but i have these error?
could someone help me?


btc['predicted_signal']=mlp.predict(X)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [129], in <cell line: 1>()
----> 1 btc['predicted_signal']=mlp.predict(X)

NameError: name 'mlp' is not defined

Hello con-amore- coin,



I believe "mlp" is a "model" you want to use in order to predict with this model the prediction feature.



You have to fit first the model before you predict the prediction feature.



Can you please provide the code above the lines you shared?



Thanks,



José Carlos

hi jose Carlos…below you can find the code…

thank you for the help.

#80% on train e 20% on test
from sklearn.model_selection import train_test_split
btc_lenght=len(btc)
train_lenght=int(btc_lenght*0.8)
test_lenght=int(btc_lenght*0.2)
X_train=X[:train_lenght]
X_test=X[test_lenght:]
y_train=y[:train_lenght]
y_test=y[train_lenght:]

from sklearn.neural_network import MLPClassifier

mlp=MLPClassifier(activation='logistic',hidden_layer_sizes=(5),random_state=42,solver='sgd')
mlp.Fit(X_train,y_train)

Hello con-amore,



I tried to run your code with the S&P 500 index data. Here's the code:

 

#80% on train e 20% on test
import yfinance as yf
import numpy as np
from sklearn.neural_network import MLPClassifier

# Read data
data = yf.download('^GSPC','2018-01-01', '2022-03-21')

# Get returns and lead returns
data['Ret'] = data['Close'].pct_change()
data['Ret_lead'] = data['Ret'].shift(-1)

# Create prediction feature
data['prediction_feature'] = np.where(data['Ret_lead']>0,1,0)

# Drop NaN values to secure they don't appear in model input data
data.dropna(inplace=True)

# Set X and y
X = data['Ret'].dropna().values.reshape(-1,1)
y = data['prediction_feature'].values

# Length of data
btc_lenght=len(data['Ret'].index)

# Split the data into tran and test data
train_lenght=int(btc_lenght*0.8)
test_lenght=int(btc_lenght*0.2)
X_train=X[:train_lenght]
X_test=X[test_lenght:]
y_train=y[:train_lenght]
y_test=y[train_lenght:]

# Set the model object
mlp=MLPClassifier(activation='logistic',hidden_layer_sizes=(5),random_state=42,solver='sgd')

# Fit the model
mlp.fit(X_train,y_train)

# Predict the prediction feature with X
data['predicted_signal']=mlp.predict(X)

There were minor errors that I corrected. Let me explain them and add some comments:


  1. Don't forget always to import the necessary libraries 
  2. "mlp.Fit" is wrong. It is actually "mlp.fit" with lowercase. Please always check the documentation of each library to make sure you write well the code. 
  3. You had the error that mlp was not defined. In the code you sent you have already defined the variable. So now it can run.
  4. Don't forget set X and y with no NaN values. When you pass dataframe columns to numpy arrays to set both inputs, please don't forget to first drop NaN values from the dataframe. Check my code to see how to do it and also check in which part of the code I'm doing it.
  5. You can use the code I provided you. You just need to change the dataframe with your bitcoin data.



    I hope this helps,



    Thanks and Regards,



    José Carlos





    Thanks



    José Carlos

Grazie del supporto!