Closed keafas closed 2 years ago
Hi @keafas,
It seems that you have an error on your code, so this is not an issue related to Spyder. I will recommend to you to review the shapes of all the variables in your code to be certain that you can do the operations you need. Hope this helps you, and happy spydering!
I can't make an animation(gif file). Please help me. I use a spider (anaconda3).
Refer to the Python code inserted below.
It succeeded in creating a graph, but an error occurred while creating an animation that visualizes learning at learning rate. An error occurs as follows. Please tell me how to solve this problem.
'pandas as pd' imported but unused (pyflakes E)
redefinition of unused 'plt' from line 10 (pyflakes E)
Refer to the Python code inserted below.
`# -- coding: utf-8 -- """ Spyder Editor
This is a temporary script file. """
import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib import cm from celluloid import Camera from matplotlib import gridspec
Training set
X = np.array([10,15,20,25,30,35,40,45,50,55,60]) #training set x Y = np.array([20,25,45,40,55,60,75,90,85,110,120]) #training set y
Variables
t1 = 0 t0 = 0 Ermean = 0 Erxmean = 0 N=len(X) # Training example lr = 0.000001 # learning rate cost = 0 # cost
epoch = 50000
index = np.array([]) w = np.array([]) b = np.array([]) c = np.array([]) Ermean_arr = np.array([]) Erxmean_arr = np.array([])
for i in range (epoch): 50000
import numpy as np x = [10,15,20,25,30,35,40,45,50,55,60] y = [20,25,45,40,55,60,75,90,85,110,120] m = len(y)
X = np.ones(shape=(m, 2)) X[:, 1] = x
def computeCost(x, y, theta): m = len(y) h_x = x.dot(theta) j = np.sum(np.square(h_x - y))(1/(2m)) return j
def gradientDescent(x, y, theta, alpha, iteration): print('Running Gradient Descent...') j_hist = [] m = len(y) for i in range(iteration): j_hist.append(computeCost(x, y, theta)) h_x = x.dot(theta) theta = theta - ((alpha/m) *((np.dot(x.T, (h_x-y) )))) return theta, j_hist
theta = np.zeros((2,1)) iteration = 50000 alpha = 0.000001
theta, cost = gradientDescent(X, y, theta, alpha, iteration)
print ("Epoch:", i+1, " theta1: ", t1, " theta0: ", t0, " cost: ", cost, " Ermean: ", Ermean, " Erxmean: ", Erxmean)
print('Theta 1:',theta[1][-1]) print('Theta 0:',theta[0][-1]) print('Cost:',cost[-1])
lr = [0.000001, 0.0005,0.001 ] t1 = [2.822,0.0412,0.000596] t0 = [1.079,118.26,119.97] cost = [4887.59,1.042,0.0002183]
import matplotlib.pyplot as plt plt.plot(lr, t1) plt.xlabel('Learning Rate') plt.ylabel('Theta 1') plt.show()
plt.plot(lr, t0) plt.xlabel('Learning Rate') plt.ylabel('Theta 0') plt.show()
plt.plot(lr, cost) plt.xlabel('Learning Rate') plt.ylabel('Cost') plt.show()
lr = [0.000001, 0.0005, 0.001] t1 = [2.822,0.0412,0.000596] t0 = [1.079,118.26,119.97] cost = [4887.59,1.042,0.0002183]
fig = plt.figure(figsize=(10,15)) # create figure gs = gridspec.GridSpec(2, 1, hspace = 0.3, height_ratios=[1, 1.2]) # set height ratios of subplots
label_font_size = 28 tick_label_size= 18
Customize subplots:
ax0=fig.add_subplot(gs[0]) ax0.set_title("Logistic regression curve", fontsize=22) ax0.tick_params(axis='both', which='major', labelsize=tick_label_size) ax0.set_xlabel("x", fontsize=label_font_size, labelpad=5) ax0.set_ylabel("y", fontsize=label_font_size, labelpad=5)
ax0.set_ylim([0,140]) ax0.set_yticks([0,20,40,60,80, 100,120,140]) ax0.set_yticklabels(["0","20","40","60","80", "100","120","140"], fontsize=tick_label_size)
ax1=fig.add_subplot(gs[1], projection="3d") ax1.set_title("Cost funtion and trajectory", fontsize=22) ax1.view_init(elev=20, azim=60) ax1.set_xlabel("theta1", fontsize=label_font_size, labelpad=25) ax1.set_ylabel("theta0", fontsize=label_font_size, labelpad=25) ax1.set_zlabel("costs", fontsize=label_font_size, labelpad=25)
ax1.tick_params(axis='both', which='major', labelsize=tick_label_size, pad=10)
ax1.tick_params(axis='z', which='major', labelsize=tick_label_size)
ax1.set_xticks([0,0.5,1,1.5,2,2.5,3]) ax1.set_xticklabels(["0", "0.5","1.0", "1.5", "2.0","2.5","3.0"], fontsize=tick_label_size) ax1.set_yticks([-10,-5,0,5,10]) ax1.set_yticklabels(["-10","-5","0","5","10"], fontsize=tick_label_size)
ax1.set_zticks([0,500,1000,1500,2000,2500,3000]) ax1.set_zticklabels(["0","500", "1000","1500","2000","2500","3000"], fontsize=tick_label_size)
a1=np.arange(0,20,1).tolist() a2=np.arange(20,200,20).tolist() a3=np.arange(200,2000,200).tolist() a4=np.arange(2000,50001,2000).tolist() points_ = a1+a2+a3+a4
camera = Camera(fig)
x = np.linspace(0,70,70)
ws = np.linspace(0,3, 50) bs = np.linspace(-10,10, 50)
M, B = np.meshgrid(ws, bs) Z = np.zeros ([len(ws),len(bs)])
for i in range (len(X)): H_temp = MX[i]+B E_temp = Y[i]-H_temp R_sq_temp = E_tempE_temp / 2 / N Z = Z+R_sq_temp
Graphicpoints = np.intersect1d (points, np.arange(epoch)) for i in Graphic_points:
animation = camera.animate(interval = 300, blit=True,# set delay between frames in milliseconds. repeat = False, repeat_delay = 0)
animation.save('C:\python\LogReg_1.gif') `