can you use a sigmoid function here to get the output in a signal 0,1 pip install…
- User
can you use a sigmoid function here to get the output in a signal 0,1 pip install keras_tuner
import pandas as pd #We use Pandas to preprocess the data import numpy as np import matplotlib.pyplot as plt #We used Matplotlib library to visualize the data. It helped us know the data and find important trends. import keras #We use Keras to implement the machine learning models. import math import tensorflow as tf from numpy import array from tensorflow.keras.models import Sequential,Model,save_model from tensorflow.keras.layers import LSTM, Dense, Dropout, Flatten, Input from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score import seaborn as sns # Visualization sns.set_style('white', { 'axes.spines.right': False, 'axes.spines.top': False}) import io from google.colab import files
from keras_tuner import RandomSearch from keras_tuner import Objective
#uploaded = files.upload() #df = pd.read_csv(io.BytesIO(uploaded['VSIN1-a.csv'])) df = pd.read_excel("/content/BSAP1-a.xls").dropna()#.reset_index().drop(columns=['index']) #df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d') df[''] = pd.to_datetime(df[''], format='%Y%m%d') #df=df[::-1].reset_index(drop=True)
df
#df = df[~(df == 0).any(axis=1)] #df=df.reset_index(drop=True)
dataset = df[['']].dropna().values dataset = dataset.astype('float64')
''' df['dif'] = (df['']-df['']).dropna() df['dif1'] = (df[''].diff()).dropna() dataset = df[['','dif', "dif1"]].dropna().values
dataset = dataset.astype('float64') '''
''' df.drop(df.tail(1).index,inplace=True) dataset = df[['']].dropna().values dataset = dataset.astype('float64') '''
dataset
plt.plot(df[''],df['']) plt.grid() plt.show()
lookback -> timestep
def create_dataset(dataset,look_back): data_x, data_y = [],[] #data_x is data and data_y is label for i in range(len(dataset)-look_back): #we want if data be beyond len(sequendatasetce), the command will not continue # اگر داده بیشتر از #len(sequendatasetce) #باشد، فرمان ادامه نمییابد. data_x.append(dataset[i:(i+look_back),:]) data_y.append(dataset[i+look_back,:]) return np.array(data_x) , np.array(data_y)
train_size = int(len(dataset) * 0.80) train , test = dataset[:train_size,:] , dataset[train_size:,:]
scaler = StandardScaler() train = scaler.fit_transform(train) test = scaler.fit_transform(test)
n_steps = 5 #timestep or look_up
train_x , train_y = create_dataset(train, n_steps) test_x , test_y = create_dataset(test, n_steps)
print(train_x.shape , train_y.shape) print(test_x.shape , test_y.shape)
trainxr = np.reshape(train_x,(train_x.shape[0],train_x.shape[1],1)) testxr = np.reshape(test_x,(test_x.shape[0],test_x.shape[1],1)) train_x = trainxr test_x = testxr print(trainxr.shape) print(testxr.shape)
- Assistant
Yes, you can use a sigmoid function to get the output in a signal of 0 and 1. However, in the provided code, the model is using a linear activation function for the output layer, which outputs a continuous value rather than a binary signal. If you want to convert the output to a binary signal, you can apply a threshold to the predicted values. For example, if the predicted value is greater than 0.5, you can consider it as 1, and if it is less than or equal to 0.5, you can consider it as 0.
- User
write the code of the sigmoid function
- Assistant
Sure! Here's the code for the sigmoid function in Python:
pythonimport numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x))You can use this function to apply the sigmoid activation to your model's output.