Mohit8219 commited on
Commit
51b4510
·
verified ·
1 Parent(s): 5938866

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -0
app.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ data = pd.read_csv('/content/train1.csv')
3
+
4
+ data.head()
5
+
6
+ data.corr()
7
+
8
+ import pandas as pd
9
+ import numpy as np
10
+ from sklearn.model_selection import train_test_split
11
+ from sklearn.preprocessing import StandardScaler
12
+ from sklearn.neighbors import KNeighborsClassifier
13
+ from sklearn.metrics import classification_report, accuracy_score
14
+ import matplotlib.pyplot as plt
15
+ import seaborn as sns
16
+
17
+
18
+ # Split dataset into independent and dependent variables
19
+ X = data.drop('price_range', axis=1)
20
+ y = data['price_range']
21
+
22
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
23
+
24
+ knn = KNeighborsClassifier(n_neighbors=5, metric = 'minkowski' , p=1)
25
+ knn.fit(X_train, y_train)
26
+
27
+ y_pred = knn.predict(X_test)
28
+
29
+ print("Accuracy:", accuracy_score(y_test, y_pred))
30
+ print("Classification Report:\n", classification_report(y_test, y_pred))
31
+
32
+
33
+ import pandas as pd
34
+ import matplotlib.pyplot as plt
35
+ import seaborn as sns # Import seaborn for scatterplot
36
+
37
+ # Create a new data point
38
+ new_data_point = pd.DataFrame({
39
+ 'battery_power': [1500],
40
+ 'blue': [1],
41
+ 'clock_speed': [2.5],
42
+ 'dual_sim': [1],
43
+ 'fc': [16],
44
+ 'four_g': [1],
45
+ 'int_memory': [32],
46
+ 'm_dep': [0.5],
47
+ 'mobile_wt': [150],
48
+ 'n_cores': [8],
49
+ 'pc': [20],
50
+ 'px_height': [1080],
51
+ 'px_width': [1920],
52
+ 'ram': [4000],
53
+ 'sc_h': [15],
54
+ 'sc_w': [8],
55
+ 'talk_time': [10],
56
+ 'three_g': [1],
57
+ 'touch_screen': [1],
58
+ 'wifi': [1]
59
+ })
60
+
61
+ # Predict the price range for the new data point
62
+ predicted_price_range = knn.predict(new_data_point)
63
+
64
+ print("Predicted price range for the new data point:", predicted_price_range[0])
65
+
66
+ # Visualize the data (example with a scatter plot)
67
+ plt.figure(figsize=(10, 6))
68
+ sns.scatterplot(x='ram', y='battery_power', hue='price_range', data=data)
69
+ plt.title('Mobile Phone Price Range')
70
+ plt.xlabel('RAM')
71
+ plt.ylabel('Battery Power')
72
+ plt.show()
73
+
74
+ # **KMeans**
75
+
76
+
77
+
78
+ from sklearn.cluster import KMeans
79
+ from sklearn.metrics import adjusted_rand_score, confusion_matrix
80
+
81
+ kmeans = KMeans(n_clusters=4, random_state=42)
82
+ kmeans.fit(X)
83
+
84
+ #Predict cluster assignments
85
+ cluster_labels = kmeans.labels_
86
+
87
+ #Evaluate the clustering performance by comparing clusters with actual price_range labels
88
+ # We use Adjusted Rand Index (ARI) and a confusion matrix for evaluation
89
+ ari = adjusted_rand_score(y, cluster_labels)
90
+ print(f"Adjusted Rand Index (ARI): {ari}")
91
+
92
+ conf_matrix = confusion_matrix(y, cluster_labels)
93
+
94
+ # Visualize the confusion matrix
95
+ plt.figure(figsize=(8, 6))
96
+ sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=range(4), yticklabels=range(4))
97
+ plt.xlabel("Cluster Labels")
98
+ plt.ylabel("Actual Price Range")
99
+ plt.title("Confusion Matrix: K-Means Clustering vs Actual Price Range")
100
+ plt.show()
101
+
102
+ from sklearn.decomposition import PCA
103
+
104
+ pca = PCA(n_components=2)
105
+ X_pca = pca.fit_transform(X)
106
+
107
+ plt.figure(figsize=(8, 6))
108
+ sns.scatterplot(x=X_pca[:, 0], y=X_pca[:, 1], hue=cluster_labels, palette="Set1", s=100, edgecolor="k")
109
+ plt.title("K-Means Clusters Visualization (2D PCA Projection)")
110
+ plt.xlabel("PCA Component 1")
111
+ plt.ylabel("PCA Component 2")
112
+ plt.show()
113
+
114
+ # prompt: adjusted_rand_score
115
+
116
+ from sklearn.metrics import adjusted_rand_score
117
+
118
+ # Assuming 'y' is the true labels and 'cluster_labels' are the predicted cluster assignments
119
+ ari = adjusted_rand_score(y, cluster_labels)
120
+ print(f"Adjusted Rand Index (ARI): {ari}")
121
+
122
+
123
+ !pip install gradio
124
+
125
+ # Define the prediction function
126
+ 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):
127
+ new_data_point = pd.DataFrame({
128
+ 'battery_power': [battery_power],
129
+ 'blue': [blue],
130
+ 'clock_speed': [clock_speed],
131
+ 'dual_sim': [dual_sim],
132
+ 'fc': [fc],
133
+ 'four_g': [four_g],
134
+ 'int_memory': [int_memory],
135
+ 'm_dep': [m_dep],
136
+ 'mobile_wt': [mobile_wt],
137
+ 'n_cores': [n_cores],
138
+ 'pc': [pc],
139
+ 'px_height': [px_height],
140
+ 'px_width': [px_width],
141
+ 'ram': [ram],
142
+ 'sc_h': [sc_h],
143
+ 'sc_w': [sc_w],
144
+ 'talk_time': [talk_time],
145
+ 'three_g': [three_g],
146
+ 'touch_screen': [touch_screen],
147
+ 'wifi': [wifi]
148
+ })
149
+
150
+ prediction = knn.predict(new_data_point)
151
+ return f"Predicted price range: {prediction[0]}"
152
+
153
+ # Create the Gradio interface
154
+ interface = gr.Interface(
155
+ fn=predict_price_range,
156
+ inputs=[
157
+ gr.Slider(minimum=0, maximum=3000, step=1, label="Battery Power"),
158
+ gr.Checkbox(label="Bluetooth"),
159
+ gr.Slider(minimum=0, maximum=3.0, step=0.1, label="Clock Speed (GHz)"),
160
+ gr.Checkbox(label="Dual SIM"),
161
+ gr.Slider(minimum=0, maximum=50, step=1, label="Front Camera (MP)"),
162
+ gr.Checkbox(label="4G"),
163
+ gr.Slider(minimum=0, maximum=256, step=1, label="Internal Memory (GB)"),
164
+ gr.Slider(minimum=0, maximum=1, step=0.1, label="Mobile Depth (cm)"),
165
+ gr.Slider(minimum=100, maximum=3000, step=1, label="Mobile Weight (g)"),
166
+ gr.Slider(minimum=1, maximum=8, step=1, label="Number of Cores"),
167
+ gr.Slider(minimum=0, maximum=100, step=1, label="PC"),
168
+ gr.Slider(minimum=0, maximum=4000, step=1, label="Pixel Height"),
169
+ gr.Slider(minimum=0, maximum=4000, step=1, label="Pixel Width"),
170
+ gr.Slider(minimum=0, maximum=8000, step=1, label="RAM (MB)"),
171
+ gr.Slider(minimum=0, maximum=20, step=1, label="Screen Height (cm)"),
172
+ gr.Slider(minimum=0, maximum=20, step=1, label="Screen Width (cm)"),
173
+ gr.Slider(minimum=0, maximum=100, step=1, label="Talk Time (hours)"),
174
+ gr.Checkbox(label="3G"),
175
+ gr.Checkbox(label="Touch Screen"),
176
+ gr.Checkbox(label="WiFi")
177
+ ],
178
+ outputs="text",
179
+ title="Mobile Price Range Predictor",
180
+ description="Enter the features of a mobile phone to predict its price range."
181
+ )
182
+
183
+ if __name__ == "__main__":
184
+ interface.launch()
185
+
186
+