Skip to content

Instantly share code, notes, and snippets.

@durveshshah
Last active February 5, 2024 02:27
Show Gist options
  • Save durveshshah/2337a7ff0035f0eaa97617be38a2f9a4 to your computer and use it in GitHub Desktop.
Save durveshshah/2337a7ff0035f0eaa97617be38a2f9a4 to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Suppose we have data for smartphone options with features like price, screen size, and camera quality
# Let's generate some sample data for demonstration purposes
# Features: [price, screen size, camera quality]
X = np.array([
[500, 5.5, 12],
[700, 6.0, 16],
[1000, 6.5, 20],
[1200, 6.3, 18],
[800, 6.2, 14],
[400, 5.0, 8],
[300, 4.7, 8],
[200, 4.0, 5],
[150, 4.0, 5],
[100, 3.5, 5],
])
# Labels: 1 for popular option, 0 for unpopular option
y = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a decision tree classifier
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# Evaluate accuracy on test set
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Now let's create a decoy smartphone with inferior specifications
decoy_smartphone = np.array([[100, 3.0, 3]])
# Predict whether the decoy smartphone is popular or unpopular
decoy_prediction = clf.predict(decoy_smartphone)
print("Decoy Prediction:", decoy_prediction)
# Adjusting the specifications of the decoy smartphone to resemble popular smartphones
decoy_smartphone_popular = np.array([[800, 6.0, 16]])
# Predict whether the adjusted decoy smartphone is popular or unpopular
decoy_prediction_popular = clf.predict(decoy_smartphone_popular)
print("Adjusted Decoy Prediction:", decoy_prediction_popular)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment