<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Solving the Captcha problem with Random Forest and Suppor Vector
To be able to run this notebook you should have place in the folder Peter's program image_loader.py and the folder images used last week
Step1:  We define the function prep_datas (props to Alexandre), already used the previous week. However now we reshape the images from a 32x32 matrix (this value seems unnecessary, however the bigger the image the worst the classifiers will work) to a flat 1024 vector, a constraint given by the Random Forest classifier.
Step2:  Then we load the training and the test set
Step3:  We define the classifiers Random Forest Classifier and Support Machine Classifier and we train them throught the fit function. Taking a linear kernel for SVC gives the best results for this classifier.
Step4:  Let's test how good the system is doing
Step5:  Now we load the real set of images and test it. This part of the program has been taken from Alexandre's program from last week. First we load the 'real world' images
Step6:  Then we make the predictions with both classifiers
Step7:  Finally we plot the results
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import os
from skimage.transform import resize
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
import tools as im
from matplotlib import pyplot as plt
%matplotlib inline
path=os.getcwd()+'/' # finds the path of the folder in which the notebook is
path_train=path+'images/train/'
path_test=path+'images/test/'
path_real=path+'images/real_world/'
Explanation: Solving the Captcha problem with Random Forest and Suppor Vector
To be able to run this notebook you should have place in the folder Peter's program image_loader.py and the folder images used last week
End of explanation
def prep_datas(xset,xlabels):
    X=list(xset)
    for i in range(len(X)):
        X[i]=resize(X[i],(32,32,1)) # reduce the size of the image from 100X100 to 32X32. Also flattens the color levels
        X[i]=np.reshape(X[i],1024) # reshape from 32x32 to a flat 1024 vector
    X=np.array(X) # transforms it into an array
    Y=np.asarray(xlabels) # transforms from list to array
    return X,Y
Explanation: We define the function prep_datas (props to Alexandre), already used the previous week. However now we reshape the images from a 32x32 matrix (this value seems unnecessary, however the bigger the image the worst the classifiers will work) to a flat 1024 vector, a constraint given by the Random Forest classifier.
End of explanation
training_set, training_labels = im.load_images(path_train)
X_train, Y_train = prep_datas(training_set,training_labels)
test_set, test_labels = im.load_images(path_test)
X_test,Y_test=prep_datas(test_set,test_labels)
Explanation: Then we load the training and the test set:
End of explanation
classifierForest = RandomForestClassifier(n_estimators=1000)
classifierSVC=svm.SVC(kernel='linear')
classifierForest.fit(X_train, Y_train) 
classifierSVC.fit(X_train,Y_train) 
Explanation: We define the classifiers Random Forest Classifier and Support Machine Classifier and we train them throught the fit function. Taking a linear kernel for SVC gives the best results for this classifier.
End of explanation
expectedF = Y_test
predictedF = classifierForest.predict(X_test)
predictedS = classifierSVC.predict(X_test)
print(expectedF)
print(predictedF)
print(predictedS)
Explanation: Let's test how good the system is doing
End of explanation
real_world_set=[]
for i in np.arange(1,73):
    filename=path+'images/real_world/'+str(i)+'.png'
    real_world_set.append(im.deshear(filename))
fake_label=np.ones(len(real_world_set),dtype='int32')
X_real,Y_real=prep_datas(real_world_set,fake_label)
Explanation: Now we load the real set of images and test it. This part of the program has been taken from Alexandre's program from last week. First we load the 'real world' images
End of explanation
y_predF = classifierForest.predict(X_real)
y_predS = classifierSVC.predict(X_real)
Explanation: Then we make the predictions with both classifiers
End of explanation
f=open(path+'images/real_world/labels.txt',"r")
lines=f.readlines()
result=[]
for x in lines:
    result.append((x.split('	')[1]).replace('\n',''))
f.close()
result=np.array([int(x) for x in result])
result[result>1]=1
plt.plot(y_predF,'o')
plt.plot(1.2*y_predS,'o')
plt.plot(2*result,'o')
plt.ylim(-0.5,2.5);
Explanation: Finally we plot the results
End of explanation
<END_TASK>