gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Generate a mini AI code
- User
Generate a mini AI code
- Assistant
Creating a simple mini AI code largely depends on what you want it to achieve. Below, I'll provide a basic Python example using a machine learning library like scikit-learn to implement a simple classifier. This example uses the Iris dataset, a classic dataset in machine learning:
# Import necessary libraries from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score # Load the Iris dataset iris = datasets.load_iris() X = iris.data # Features y = iris.target # Labels # Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Feature scaling for better performance scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # Initialize the classifier knn = KNeighborsClassifier(n_neighbors=3) # Train the classifier knn.fit(X_train, y_train) # Make predictions y_pred = knn.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy: {accuracy * 100:.2f}%") # Example prediction for a new data point new_data = [[5.1, 3.5, 1.4, 0.2]] # Input a new sample new_data_scaled = scaler.transform(new_data) prediction = knn.predict(new_data_scaled) print(f"Predicted class for the new data point: {iris.target_names[prediction][0]}")