Powered by Cognitive K.i.
Example code of a basic Artificial Neural Network (ANN)
Example code of a basic Artificial Neural Network (ANN) in Python that uses machine learning to predict Bitcoin prices against multiple currencies in real-time. Please note that this code can be improved and optimized further based on your specific requirements:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam
# Load the dataset
data = pd.read_csv('bitcoin_prices.csv')
# Split the dataset into input features (X) and target variable (y) X = data.drop(['BTC/USD', 'Date'], axis=1).values y = data['BTC/USD'].values
# Scale the features to a range of 0 to 1 scaler = MinMaxScaler() X_scaled = scaler.fit_transform(X)
# Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# Build the ANN model
model = Sequential()
model.add(Dense(32, input_dim=X_train.shape[1], activation='relu')) model.add(Dense(16, activation='relu')) model.add(Dense(1, activation='linear'))
# Compile the model
optimizer = Adam(learning_rate=0.001)
model.compile(loss='mean_squared_error', optimizer=optimizer)
# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=1)
# Evaluate the model
loss = model.evaluate(X_test, y_test, verbose=0) print(f'Test loss: {loss}')
# Load real-time data
real_time_data = pd.read_csv('real_time_bitcoin_prices.csv')
# Scale the real-time data
real_time_data_scaled = scaler.transform(real_time_data)
# Make predictions
predictions = model.predict(real_time_data_scaled)
# Print the predictions
for i, currency in enumerate(['USD', 'GBP', 'EUR', 'CHF', 'JPY', 'AUD', 'NZD', 'CAD']):
print(f'Predicted BTC/{currency} price: {predictions[i]}') ```
In this example, we assume that you have the historical Bitcoin prices dataset stored in a file named 'bitcoin_prices.csv' and the real-time Bitcoin prices dataset stored in a file named 'real_time_bitcoin_prices.csv'. You will need to replace these filenames with the actual file paths for your datasets.
Additionally, you may need to install the required dependencies such as `pandas`, `scikit-learn`, and `tensorflow` if they are not already installed. You can use `pip install pandas scikit-learn tensorflow` to install them.
Please note that predicting real-time Bitcoin prices accurately is a challenging task due to the volatility and various other factors affecting the cryptocurrency market. Hence, you may need to adjust the model architecture, data preprocessing techniques, and hyperparameters to achieve better results based on your specific needs.