import pandas as pd data = pd.read_csv('train1.csv') data.head() data.corr() import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import classification_report, accuracy_score import matplotlib.pyplot as plt import seaborn as sns # Split dataset into independent and dependent variables X = data.drop('price_range', axis=1) y = data['price_range'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) knn = KNeighborsClassifier(n_neighbors=5, metric = 'minkowski' , p=1) knn.fit(X_train, y_train) y_pred = knn.predict(X_test) print("Accuracy:", accuracy_score(y_test, y_pred)) print("Classification Report:\n", classification_report(y_test, y_pred)) import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # Import seaborn for scatterplot # Create a new data point new_data_point = pd.DataFrame({ 'battery_power': [1500], 'blue': [1], 'clock_speed': [2.5], 'dual_sim': [1], 'fc': [16], 'four_g': [1], 'int_memory': [32], 'm_dep': [0.5], 'mobile_wt': [150], 'n_cores': [8], 'pc': [20], 'px_height': [1080], 'px_width': [1920], 'ram': [4000], 'sc_h': [15], 'sc_w': [8], 'talk_time': [10], 'three_g': [1], 'touch_screen': [1], 'wifi': [1] }) # Predict the price range for the new data point predicted_price_range = knn.predict(new_data_point) print("Predicted price range for the new data point:", predicted_price_range[0]) # Visualize the data (example with a scatter plot) plt.figure(figsize=(10, 6)) sns.scatterplot(x='ram', y='battery_power', hue='price_range', data=data) plt.title('Mobile Phone Price Range') plt.xlabel('RAM') plt.ylabel('Battery Power') plt.show() # **KMeans** from sklearn.cluster import KMeans from sklearn.metrics import adjusted_rand_score, confusion_matrix kmeans = KMeans(n_clusters=4, random_state=42) kmeans.fit(X) #Predict cluster assignments cluster_labels = kmeans.labels_ #Evaluate the clustering performance by comparing clusters with actual price_range labels # We use Adjusted Rand Index (ARI) and a confusion matrix for evaluation ari = adjusted_rand_score(y, cluster_labels) print(f"Adjusted Rand Index (ARI): {ari}") conf_matrix = confusion_matrix(y, cluster_labels) # Visualize the confusion matrix plt.figure(figsize=(8, 6)) sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=range(4), yticklabels=range(4)) plt.xlabel("Cluster Labels") plt.ylabel("Actual Price Range") plt.title("Confusion Matrix: K-Means Clustering vs Actual Price Range") plt.show() from sklearn.decomposition import PCA pca = PCA(n_components=2) X_pca = pca.fit_transform(X) plt.figure(figsize=(8, 6)) sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=cluster_labels, palette="Set1", s=100, edgecolor="k") plt.title("K-Means Clusters Visualization (2D PCA Projection)") plt.xlabel("PCA Component 1") plt.ylabel("PCA Component 2") plt.show() # prompt: adjusted_rand_score from sklearn.metrics import adjusted_rand_score # Assuming 'y' is the true labels and 'cluster_labels' are the predicted cluster assignments ari = adjusted_rand_score(y, cluster_labels) print(f"Adjusted Rand Index (ARI): {ari}") # Define the prediction function def predict_price_range(battery_power, blue, clock_speed, dual_sim, fc, four_g, int_memory, m_dep, mobile_wt, n_cores, pc, px_height, px_width, ram, sc_h, sc_w, talk_time, three_g, touch_screen, wifi): new_data_point = pd.DataFrame({ 'battery_power': [battery_power], 'blue': [blue], 'clock_speed': [clock_speed], 'dual_sim': [dual_sim], 'fc': [fc], 'four_g': [four_g], 'int_memory': [int_memory], 'm_dep': [m_dep], 'mobile_wt': [mobile_wt], 'n_cores': [n_cores], 'pc': [pc], 'px_height': [px_height], 'px_width': [px_width], 'ram': [ram], 'sc_h': [sc_h], 'sc_w': [sc_w], 'talk_time': [talk_time], 'three_g': [three_g], 'touch_screen': [touch_screen], 'wifi': [wifi] }) prediction = knn.predict(new_data_point) return f"Predicted price range: {prediction[0]}" # Create the Gradio interface import gradio as gr interface = gr.Interface( fn=predict_price_range, inputs=[ gr.Slider(minimum=0, maximum=3000, step=1, label="Battery Power"), gr.Checkbox(label="Bluetooth"), gr.Slider(minimum=0, maximum=3.0, step=0.1, label="Clock Speed (GHz)"), gr.Checkbox(label="Dual SIM"), gr.Slider(minimum=0, maximum=50, step=1, label="Front Camera (MP)"), gr.Checkbox(label="4G"), gr.Slider(minimum=0, maximum=256, step=1, label="Internal Memory (GB)"), gr.Slider(minimum=0, maximum=1, step=0.1, label="Mobile Depth (cm)"), gr.Slider(minimum=100, maximum=3000, step=1, label="Mobile Weight (g)"), gr.Slider(minimum=1, maximum=8, step=1, label="Number of Cores"), gr.Slider(minimum=0, maximum=100, step=1, label="PC"), gr.Slider(minimum=0, maximum=4000, step=1, label="Pixel Height"), gr.Slider(minimum=0, maximum=4000, step=1, label="Pixel Width"), gr.Slider(minimum=0, maximum=8000, step=1, label="RAM (MB)"), gr.Slider(minimum=0, maximum=20, step=1, label="Screen Height (cm)"), gr.Slider(minimum=0, maximum=20, step=1, label="Screen Width (cm)"), gr.Slider(minimum=0, maximum=100, step=1, label="Talk Time (hours)"), gr.Checkbox(label="3G"), gr.Checkbox(label="Touch Screen"), gr.Checkbox(label="WiFi") ], outputs="text", title="Mobile Price Range Predictor", description="Enter the features of a mobile phone to predict its price range." ) if __name__ == "__main__": interface.launch()