Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need your Help
#1
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
Reply
#2
The problem is because \ is a special character
to get around it you can change
"C:\Users\Bilel\Desktop\Nouveau dossier\data.xlsx"
to r in front to make it a raw string
r"C:\Users\Bilel\Desktop\Nouveau dossier\data.xlsx"
or swap \ with /
"C:/Users/Bilel/Desktop/Nouveau dossier/data.xlsx"
or change single \ to double \\
r"C:\\Users\\Bilel\\Desktop\Nouveau dossier\\data.xlsx"
Reply
#3
I tried to make every possibility but the problem persists
Error:
runfile('C:/Users/Bilel/Desktop/DEEP/example_1.py', wdir='C:/Users/Bilel/Desktop/DEEP') Traceback (most recent call last): File "<ipython-input-1-9f59fd1a3eba>", line 1, in <module> runfile('C:/Users/Bilel/Desktop/DEEP/example_1.py', wdir='C:/Users/Bilel/Desktop/DEEP') File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace) 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/DEEP/example_1.py", line 22, in <module> os.chdir ("C:\\Users\\Bilel\\Desktop\\DEEP\\data_svm.xlsx") NotADirectoryError: [WinError 267] Nom de répertoire non valide: 'C:\\Users\\Bilel\\Desktop\\DEEP\\data_svm.xlsx'
Reply
#4
Different error, change path to a folder not a file
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020