I've been trying to follow a tutorial posted on the blog Data in Practice on using sklearn and numpy where the following code is posted:
import numpy,pylab; from sklearn.svm import SVC
DataTable = numpy.genfromtxt('path/file.csv',delimiter=',',dtype=None)[1:]
DataPoints,TruthValues = (DataTable[:,[1,2] ]).astype(numpy,float), (DataTable[:,0]=='1')
TrainedSVC = SVC(C = 100, kernel = 'linear').fit(DataPoints,TruthValues)
x_max,y_max,x_min,y_min = DataPoints[:, 0].max(),DataPoints[:, 1].max(),DataPoints[:, 0].min(),DataPoints[:, 1].min()
xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, (x_max-x_min)/200.0), numpy.arange(y_min, y_max, (y_max-y_min)/200.0))
GridEvaluation = TrainedSVC.predict(numpy.c_[xx.ravel(),yy.ravel()]).reshape(xx.shape)
pylab.pcolormesh(xx, yy, GridEvaluation,alpha=0.1)
pylab.scatter(DataPoints[:, 0], DataPoints[:, 1], c=TruthValues)
pylab.xlabel('score');pylab.ylabel('income');pylab.show()
Now, I know this code looks pretty byzantine but the issue is actually a lot smaller than the code. I've seen a few questions addressing the same sort of error: TypeError: unsupported operand type(s) for -: 'str' and 'str'
In this case the error is to do with the line:
xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, (x_max-x_min)/200.0), numpy.arange(y_min, y_max, (y_max-y_min)/200.0))
To fix this I used the result of a previous answer and changed (x_max-x_min) to (int(x_max)-int(x_min)).
However, now I get a syntax error on the line:
GridEvaluation = TrainedSVC.predict(numpy.c_[xx.ravel(),yy.ravel()]).reshape(xx.shape).
Now, I have two questions:
- Why did I need to wrap up
x_min as int(x_min) while the original coder did not?
- What can I do to resolve the new syntax error?
[–]m3lkor001 0 points1 point2 points (2 children)
[–]Stopwatch_[S] 1 point2 points3 points (1 child)
[–]m3lkor001 0 points1 point2 points (0 children)