carmelo-cyber / Carmelo_GitHub

All code for any situation
0 stars 0 forks source link

AI Code #7

Open carmelo-cyber opened 1 year ago

carmelo-cyber commented 1 year ago

Import necessary libraries

from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier

Load the iris dataset

iris = datasets.load_iris()

Split the data into training and test sets

X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3)

Train a random forest classifier on the training data

clf = RandomForestClassifier(n_estimators=100) clf.fit(X_train, y_train)

Evaluate the classifier on the test data

accuracy = clf.score(X_test, y_test) print(f"Model accuracy: {accuracy:.2f}")

Use the classifier to make predictions on new data

new_data = [[5.1, 3.5, 1.4, 0.2], [6.3, 3.3, 4.7, 1.6]] predictions = clf.predict(new_data) print(f"Predictions: {predictions}")