i done following steps to train my model is it right?: from sklearn.modelselection…
- User
i done following steps to train my model is it right?:
from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from tensorflow.keras.utils import to_categorical import numpy as np
import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA
from google.colab import drive import pandas as pd drive.mount('/content/drive')
Load the dataset using pandas
df = pd.read_csv('/content/drive/MyDrive/ML/encoded_combined_USDT_file.csv')
Assuming ‘df’ is your DataFrame and ‘Label’ is the target column
X = df.drop('Label', axis=1).values Y = df['Label'].values Y = to_categorical(Y) # Convert labels to one-hot encoding
Separating out the features
X = df.iloc[:, :df.shape[1]].values # Adjust this if your features are located differently
Normalizing the features
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
Normalizing the features
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
To perform PCA again keeping only enough components to explain 95% of the variance
pca_95 = PCA(n_components=295) principalComponents_95 = pca_95.fit_transform(X_scaled)
Creating a dataframe with the principal components that explain 95% of variance
principal_df_95 = pd.DataFrame(data = principalComponents_95, columns = [f'Principal Component {i}' for i in range(1, 295+1)])
X_train, X_temp, y_train, y_temp = train_test_split(principal_df_95.values, Y, train_size = 0.94, random_state = 42)
We will keep the shuffle = True since our dataset has not any time dependency.
X_dev, X_test, y_dev, y_test = train_test_split(X_temp, y_temp, test_size = 0.5, random_state = 42)
import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Dropout
def build_model(): model = Sequential([ Dense(512, input_shape=(principal_df_95.values.shape[1],), activation='relu'), Dropout(0.3), Dense(256, activation='relu'), Dropout(0.2), Dense(128, activation='relu'), Dropout(0.1), Dense(64, activation='relu'), Dropout(0.05), Dense(32, activation='relu'), Dense(Y.shape[1], activation='softmax') # Output layer ])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return modelmodel = build_model()
history = model.fit(X_train, y_train, epochs=200, batch_size=64, validation_data=(X_dev, y_dev))
- Assistant