<jupyter_start><jupyter_text>CLASSIFICATION using train_test_split function that comes with scikit learn<jupyter_code>from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(iris['data'],iris['target'],random_state=0)
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>KNearestNeighbor (KNN algorithm)<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>TAKING UNKOWN DATA TO PREDICT<jupyter_code>import numpy as np
X_new=np.array([[5,2.9,1,0.2]])
X_new.shape<jupyter_output><empty_output><jupyter_text>MAKING PREDICTIONS<jupyter_code>predictions=knn.predict(X_new)
print(predictions)
iris['target_names'][predictions]<jupyter_output><empty_output><jupyter_text>CHECKING ACCURACY<jupyter_code>knn.score(X_test,y_test)<jupyter_output><empty_output><jupyter_text>Decision Tree Algorithm<jupyter_code>import graphviz
from sklearn.tree import export_graphviz

import numpy as np
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from sklearn.tree import export_text

iris=load_iris()
x=iris.data
y=iris.target
tree_clf=DecisionTreeClassifier()
model=tree_clf.fit(x,y)
pro=tree_clf.predict_proba([[6,3,4.8,1.8]])
r = export_text(model, feature_names=iris['feature_names'])
print(r)
print(pro)
r=pro
print(pro[0])
result=np.where(pro[0]==1)
print(result)
iris['target_names'][result]
tree.plot_tree(tree_clf)
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(25,20))
_ = tree.plot_tree(tree_clf, 
                   feature_names=iris.feature_names,  
                   class_names=iris.target_names,
                   filled=True)

from  sklearn import  datasets
iris=datasets.load_iris()
x=iris.data
y=iris.target
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.5)
from sklearn import tree
classifier=tree.DecisionTreeClassifier()
classifier.fit(x_train,y_train)
predictions=classifier.predict(x_test)
print(predictions)
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,predictions))
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_text
iris = load_iris()
X = iris['data']
y = iris['target']
decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
decision_tree = decision_tree.fit(X, y)
r = export_text(decision_tree, feature_names=iris['feature_names'])
print(r)<jupyter_output>|--- petal width (cm) <= 0.80
|   |--- class: 0
|--- petal width (cm) >  0.80
|   |--- petal width (cm) <= 1.75
|   |   |--- class: 1
|   |--- petal width (cm) >  1.75
|   |   |--- class: 2<jupyter_text>RANDOM FOREST<jupyter_code>from sklearn.datasets import load_iris
iris=load_iris()
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(iris['data'],iris['target'],random_state=0)

#split dataset into training set and testing set
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)#70%training and 30% testing set
#import randomforest classifier
from sklearn.ensemble import RandomForestClassifier
#create a gaussian classifier
clf=RandomForestClassifier(n_estimators=100)
#train the model using the training set
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
#import scikitlearn metric model for accuracy calculation
from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
import numpy as np
X_new=np.array([[5,2.9,1,0.2]])
X_new.shape
predictions=clf.predict(X_new)
print(predictions)
iris['target_names'][predictions]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup