text_plain_output_1.png
from keras.layers import Dense, Activation

from keras.models import Sequential

from keras.optimizers import Adam

from sklearn.metrics import mean_squared_error

from sklearn.tree import DecisionTreeRegressor

import numpy as np

import numpy as np

import random

import random

import random
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
def perform_operation(a, b, op):
    if op == 0:
        return a + b
    elif op == 1:
        return a - b
    elif op == 2:
        return a * b
    elif op == 3:
        return a / b if b != 0 else 0
train_x = []
train_y = []
for i in range(50000000):
    a = random.randint(0, 10000)
    b = random.randint(0, 10000)
    op = random.randint(0, 3)
    result = perform_operation(a, b, op)
    train_x.append([a, b, op])
    train_y.append(result)
train_x = np.array(train_x)
train_y = np.array(train_y)
model = DecisionTreeRegressor()
model.fit(train_x, train_y)
test_x = np.array([[5, 10, 0], [15, 20, 1], [30, 40, 2], [50, 60, 3]])
predictions = model.predict(test_x)
mse = mean_squared_error(train_y, model.predict(train_x))

import numpy as np
import random
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import Adam
def generate_random_polynomial_and_derivative(degree, max_coeff=100):
    coeffs = [random.randint(-max_coeff, max_coeff) for _ in range(degree + 1)]
    derivative_coeffs = [coeffs[i] * i for i in range(1, len(coeffs))]
    return (coeffs, derivative_coeffs)
n = 7
train_x = []
train_y = []
for i in range(50000):
    coeffs, derivative_coeffs = generate_random_polynomial_and_derivative(n)
    train_x.append(coeffs)
    train_y.append(derivative_coeffs)
train_x = np.array(train_x)
train_y = np.array(train_y)
model = Sequential()
model.add(Dense(128, input_dim=n + 1))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(n))
optimizer = Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss='mse')
model.fit(train_x, train_y, epochs=100, batch_size=32)
def test_model_on_polynomial(coeffs):
    test_x = np.array([coeffs])
    predictions = model.predict(test_x)
    return predictions
custom_polynomial = [-9, 1, 5, 2, 8, 5, 4, 1]
predicted_derivative_coeffs = test_model_on_polynomial(custom_polynomial)
print(predicted_derivative_coeffs)