Supervised Learning Algorithms: Explanaition and Simple code | by Felipe Sanchez Garzón | Towards Data Science

A supervised learning algorithm takes a known set of input data (the learning set) and known responses to the data (the output), and forms a model to generate reasonable predictions for the response to the new input data. Use supervised learning if you have existing data for the output you are trying to predict.
They can be for Classification or Regression. In this post I will show some example of each one.
1) Import the Classifier  from sklearn.neighbors import   KNeighborsClassifier2) Create arrays for the features and the response variable      y = df['target'].values      X = df.drop('target', axis=1).values              3) Create a k-NN classifier with 6 neighbors       knn = KNeighborsClassifier(n_neighbors =   6)              4) Fit the classifier to the data       knn.fit(X,y)                    5) make predictionsnew_prediction = knn.predict(X_new)
1) Setup arrays to store train and test accuraciesneighbors = np.arange(1, 9)train_accuracy = np.empty(len(neighbors))test_accuracy = np.empty(len(neighbors))2) Loop over different values of kfor i, k in enumerate(neighbors):   3) Setup a k-NN Classifier with k neighbors   knn = KNeighborsClassifier(n_neighbors=k)   4) Fit the classifier to the training data   knn.fit(X_train, y_train)   5) Compute accuracy on the training set   train_accuracy[i] = knn.score(X_train, y_train)   6) Compute accuracy on the testing set   test_accuracy[i] = knn.score(X_test, y_test)7) Generate plotplt.title('k-NN: Varying Number of Neighbors')plt.plot(neighbors, test_accuracy, label = 'Testing Accuracy')plt.plot(neighbors, train_accuracy, label = 'Training Accuracy')plt.legend()plt.xlabel('Number of Neighbors')plt.ylabel('Accuracy')
1) Import the Classifierfrom sklearn.ensemble import RandomForestClassifier2) input data as np arrayy = df[‘target'].valuesX = df.drop(‘target', axis=1).values3) import model and fit(train) datarfc = RandomForestClassifier(n_estimators=100)rfc.fit(X, y)4) make predictionspred_rfc = rfc.predict(X_test)
1) Import the necessary modules       from sklearn.linear_model import LogisticRegression       from sklearn.metrics import confusion_matrix, classification_report              2) Create training and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42)              3) Create the classifier:    logreg = LogisticRegression()              4) Fit the classifier to the training data       logreg.fit(X_train,y_train)              5) Predict the labels of the test set       y_pred = logreg.predict(X_test)
THE ROC Curve for the Logisti Regression
ROC curve, is a graphical plot that illustrates the diagnostic ability of a binary classifier system as its discrimination threshold is varied. The ROC curve is created by plotting the true positive rate against the false positive rate at various threshold settings. The true-positive rate is also known as sensitivity, recall or probability of detection. The false-positive rate is also known as probability of false alarm
1) Import the necessary modules       from sklearn.metrics import roc_curve              2) Compute predicted probabilities    y_pred_prob = logreg.predict_proba(X_test)[:,1]              3) Generate ROC curve values     fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)  4) Plot ROC curve       plt.plot([0, 1], [0, 1], 'k--')       plt.plot(fpr, tpr)       plt.xlabel('False Positive Rate')       plt.ylabel('True Positive Rate')       plt.title('ROC Curve')       plt.show()
The larger the area under the ROC curve, the better the model.
1) Import LinearRegression       from sklearn import linear_model       from sklearn.linear_model import   LinearRegression2) Create the regressor     reg = linear_model.LinearRegression()3) Create the prediction space      prediction_space =  np.linspace(min(X_fertility),max(X_fertility)).reshape(-1,1)4) Fit the model to the data       reg.fit(X_fertility,y)5) Compute predictions  y_pred = reg.predict(prediction_space)6) Plot regression line       plt.plot(prediction_space, y_pred,  color='black', linewidth=3)       plt.show()
1) Import Decision Tree Regressorfrom sklearn.tree   import DecisionTreeRegressor2) Create a decision tree regression model     decision_tree = DecisionTreeRegressor()            3) Fit the model to the training features  and targets       decision_tree.fit(train_features,train_targets)              4) Check the score on train and test       print(decision_tree.score(train_features,   train_targets))       print(decision_tree.score(test_features,test_targets))
Finally, two more regression algorithms:
If you like to continue reading stories like this one you can suscribe here !
If you want to see how these algorithms performs in a dataset from the real world, I suggest you to read the next post:
towardsdatascience.com
Or to read this project, where I am working with the data from my reasearch: