Apr-26-2019, 08:52 AM
i tried to execute this code below but i got this error can someone help me
#function for scatter plot def myscatter(df,dfpos,dfneg): #plot a blank scatter plot for defining the right dimensions plt.scatter(df.iloc[:,0],df.iloc[:,1],color="white") #annotate - positive instances for i in dfpos.index: plt.annotate(i,xy=(df.loc[i,'x1'],df.loc[i,'x2']),xytext=(-3,-3),textcoords='offset points',color='red') #annotate - negative instances for i in dfneg.index: plt.annotate(i,xy=(df.loc[i,'x1'],df.loc[i,'x2']),xytext=(-3,-3),textcoords='offset points',color='blue') return None #end of the function #change the current directory import os os.chdir ( "C:\Users\Bilel\Desktop\Nouveau dossier\data.xlsx" ) #pandas package import pandas #load the dataset df = pandas.read_table("C:\Users\Bilel\Desktop\Nouveau dossier\data.xlsx",sep="\t",header=0,index_col=0) print(df.shape) #partitionning the data.frame according to the labels dfpos = df[df['y']=='p'] dfneg = df[df['y']=='n'] #graphical representation import matplotlib.pyplot as plt myscatter(df,dfpos,dfneg) plt.show() #scikit-learn - SVC class from sklearn.svm import SVC #object svm = SVC(kernel='linear') #fit on the dataset svm.fit(df.as_matrix()[:,0:2],df.as_matrix()[:,2]) #number of support vectors print(svm.support_.shape) #index of the support vectors print(df.index[svm.support_]) #values for these support vectors print(svm.support_vectors_) #weigth of the support vectors print(svm.dual_coef_) #coordinates c1 = svm.support_vectors_[:,0] c2 = svm.support_vectors_[:,1] #highlight the support vectors myscatter(df,dfpos,dfneg) plt.scatter(c1,c2,s=200,facecolors='none',edgecolors='black') plt.show() #SVC provides automatically the coefficients for linear kernel print(svm.coef_) #we get also the intercept print(svm.intercept_) #coordinates import numpy as np xf = np.array([3,12]) yf = -svm.coef_[0][0]/svm.coef_[0][1]*xf-svm.intercept_/svm.coef_[0][1] xb = np.array([4.5,12]) yb = -svm.coef_[0][0]/svm.coef_[0][1]*xb-(svm.intercept_-1.0)/svm.coef_[0][1] xh = np.array([2,11]) yh = -svm.coef_[0][0]/svm.coef_[0][1]*xh-(svm.intercept_+1.0)/svm.coef_[0][1] #graphical representation with the frontier myscatter(df,dfpos,dfneg) plt.scatter(c1,c2,s=200,facecolors='none',edgecolors='black') plt.plot(xf,yf,c='green') plt.plot(xb,yb,c='gray') plt.plot(xh,yh,c='gray') plt.show()
Error: File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Bilel/Desktop/Nouveau dossier/example_1.py", line 26
os.chdir ( "C:\Users\Bilel\Desktop\Nouveau dossier\data.xlsx" )
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape