Decision Tree
Decision Tree
iris = datasets.load_iris()
X = iris.data
y = iris.target
dtree = DecisionTreeClassifier()
dtree.fit(X_train, y_train)
y_pred = dtree.predict(X_test)
This code loads the iris dataset, splits it into train and test sets, creates a decision tree classifier,
trains the model, makes predictions on the test set, and evaluates the model by computing accuracy.
You can modify this code as per your requirement, for example, you can tune hyperparameters to
optimize the decision tree, or you can use cross-validation to evaluate the model.
# Import necessary libraries
iris = load_iris()
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)
y_pred = rfc.predict(X_test)
print("Accuracy:", accuracy)
Linear Regression
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3, 5, 7, 9, 11])
x = x.reshape((-1, 1))
y = y.reshape((-1, 1))
model = LinearRegression().fit(x, y)
print('Coefficients:', model.coef_)
print('Intercept:', model.intercept_)
# Predict a value
y_pred = model.predict([[6]])
plt.show()
KNN
import pandas as pd
import numpy as np
iris_data = load_iris()
X = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)
y = pd.Series(iris_data.target)
knn = KNeighborsClassifier(n_neighbors=3)
y_pred = knn.predict(X_test)
print('Accuracy:', accuracy)