import tensorflow as tf import numpy as np import matplotlib.pyplot as plt plt.style.use("ggplot") from matplotlib import colors, cm import os import pandas as pd from tensorflow import keras # plt.rcParams controls the appearance of your plots globally, # affecting all subsequent plots created in your session. plt.rcParams["axes.grid"] = False plt.rcParams.update({'font.size': 20}) plt.rcParams.update({'figure.figsize': (12,9)}) plt.rcParams['lines.markersize'] = 8 # Generate data points with gaussian smearing data = np.random.uniform(size=100) labels = 5.*data*data*data + 1 + np.random.normal(loc=0.0, scale=0.1, size=100) # show plot plt.scatter(data, labels, label="data") plt.legend() plt.show() # define chi2 like cost function def cost(params): W, b = params return np.mean((labels - (W*data*data*data + b))**2) # call minimizer # provides a collection of optimization algorithms for finding the minimum o # maximum of a given function. from scipy.optimize import minimize res = minimize(cost, [1., 1.]) # returns an OptimizeResult object # x :the solution (minimum) of the optimization problem, represented as an # array. # Results of the minimization W, b = res.x print ('function value at the minimum and fitted parameters',res.fun,' ',W,' ',b) points = np.linspace(0, 1, 100) prediction = W*points*points*points + b # plot fit model plt.scatter(data, labels, label="data") plt.plot(points, prediction, label="model", color="green") plt.legend() plt.show()