You are on page 1of 3

Supervised_Learning_flow_sample1

September 21, 2022

[1]: import pandas as pd


import warnings
warnings.filterwarnings("ignore")

[2]: data = pd.DataFrame({'Redness' : [10, 4, 3.1, 9.5, 5.0, 9.8, 2.3, 3.1, 2.8, 6.
,→3],

'Roundness' : [4, 10, 13.1, .95, 8.0, 3.9, 12.3, 13.1, 10.1, 16.
,→3],

'Price' : [20, 13, 12, 19, 10, 15, 9.3, 8.1, 4.2, 8.7],
'Fruit' : ['Apple', 'Orange', 'Orange', 'Apple', 'Orange',␣
,→'Apple', 'Orange', 'Orange', 'Orange', 'Apple']

})

data

[2]: Redness Roundness Price Fruit


0 10.0 4.00 20.0 Apple
1 4.0 10.00 13.0 Orange
2 3.1 13.10 12.0 Orange
3 9.5 0.95 19.0 Apple
4 5.0 8.00 10.0 Orange
5 9.8 3.90 15.0 Apple
6 2.3 12.30 9.3 Orange
7 3.1 13.10 8.1 Orange
8 2.8 10.10 4.2 Orange
9 6.3 16.30 8.7 Apple

Shuffle and Split


[4]: from sklearn.model_selection import train_test_split

[5]: features = data[['Redness', 'Roundness', 'Price']]


target = data['Fruit']

[6]: X_train, X_test, y_train, y_test = train_test_split(features, target,␣


,→train_size=.75, random_state = 40)

1
[9]: print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

(7, 3)
(3, 3)
(7,)
(3,)

Train the model


[10]: from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC

[11]: my_logreg_model = LogisticRegression()


my_DT_model = DecisionTreeClassifier()
my_SVM_model = SVC()

[12]: my_logreg_model.fit(X_train, y_train)


my_DT_model.fit(X_train, y_train)
my_SVM_model.fit(X_train, y_train)

[12]: SVC()

Prediction
[17]: my_predictions_logreg = my_logreg_model.predict(X_test)
my_predictions_DT = my_DT_model.predict(X_test)
my_predictions_SVM = my_SVM_model.predict(X_test)

Predict on The Train Data : Extra Step


[18]: my_predictions_logreg_train = my_logreg_model.predict(X_train)
my_predictions_DT_train = my_DT_model.predict(X_train)
my_predictions_SVM_train = my_SVM_model.predict(X_train)

Testing the model


[19]: from sklearn.metrics import accuracy_score

[20]: print("The test accuracy for Logistic Regression Model : "␣


,→,accuracy_score(y_test, my_predictions_logreg))

print("The test accuracy for Decision Tree Model : " ,accuracy_score(y_test,␣


,→my_predictions_DT))

2
print("The test accuracy for SVM Model : " ,accuracy_score(y_test,␣
,→my_predictions_SVM))

The test accuracy for Logistic Regression Model : 1.0


The test accuracy for Decision Tree Model : 1.0
The test accuracy for SVM Model : 1.0

[21]: print("The train accuracy for Logistic Regression Model : "␣


,→,accuracy_score(y_train, my_predictions_logreg_train))

print("The train accuracy for Decision Tree Model : " ,accuracy_score(y_train,␣


,→my_predictions_DT_train))

print("The train accuracy for SVM Model : " ,accuracy_score(y_train,␣


,→my_predictions_SVM_train ))

The train accuracy for Logistic Regression Model : 1.0


The train accuracy for Decision Tree Model : 1.0
The train accuracy for SVM Model : 0.8571428571428571

0.0.1 Inference

[15]: my_pred = my_logreg_model.predict([[7,10,10]])


my_pred

[15]: array(['Apple'], dtype=object)

[ ]:

You might also like