Mobile Price Range Classifier¶
Credit: AITS Cainvas Community
Photo by Eugene Machiavelli for Shakuro on Dribbble
You'll want to evaluate almost every model you ever build. In most (though not all) applications, the relevant measure of model quality is predictive accuracy. In other words, will the model's predictions be close to what actually happens.
Many people make a huge mistake when measuring predictive accuracy. They make predictions with their training data and compare those predictions to the target values in the training data. You'll see the problem with this approach and how to solve it.
The Internet of Things (IoT) is a network of intelligent devices ranging from home appliances to industrial equipment that can become connected to the Internet, monitor themselves, send contextual information such as pressure, location, and temperature, and communicate somehow, anytime, anywhere on the planet. IoT means “connecting anyone, anything, anytime, anyplace, any service and any network”. The proliferation of mobile connectivity and the decreasing prices of sensors and processors are encouraging the rapid growth of the IoT and IoE. Smart devices, for example, smartphones, smartwatches, PDAs, phablets, and tablets, will be the primary interaction tools used by people in a connected environment including cars, homes, and workplaces. A smartphone can be considered a miniature computer that has a virtual store of many applications such as games, different browsers, maps, emails, image editors, and that help to turn it into a device that is smarter than a regular cell phone.
In this project, we are focusing on how the mobile prices are ranging between different values and how can we enhance it. The technology necessary for all the example applications of IoT and IoE, stated in the previous sections, to succeed is available today. RFID, Bluetooth, NFC, 3G, 4G, 5G, etc. can transfer data over the Internet, also batteries’ technologies have evolved; for example, wireless and solar power batteries and long-lasting batteries are available in today’s market.
Importing the Dataset¶
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/archive_nMtDHpe.zip
!unzip archive_nMtDHpe.zip
Importing necessary libraries¶
# some necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
Loading the Data¶
# loading the training dataset
dataset = pd.read_csv('train.csv')
dataset.head(10)
Now, we check for the columns
dataset.columns
Data Preprocessing¶
#Changing pandas dataframe to numpy array
X = dataset.iloc[:,:20].values
y = dataset.iloc[:,20:21].values
Now, we are normalizing the data
#Normalizing the data
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
print('Normalized data:')
print(X[0])
Here, we are doing the one hot encoding
#One hot encode
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
y = ohe.fit_transform(y).toarray()
print('One hot encoded array:')
print(y[0:5])
Splitting of the Data will be done here
#Train test split of model
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.1,random_state = 0)
Building our Model¶
# importing libraries
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
# creating the model
model = Sequential()
model.add(Dense(16, input_dim=20, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(12, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(4, activation='softmax'))
#To visualize neural network
model.summary()
# compiling the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fitting the model
history = model.fit(X_train, y_train, epochs=100, batch_size=64)
Now, we are doing the predictions on the testing dataset and then convert them to labels.
y_pred = model.predict(X_test)
#Converting predictions to label
pred = list()
for i in range(len(y_pred)):
pred.append(np.argmax(y_pred[i]))
#Converting one hot encoded test label to label
test = list()
for i in range(len(y_test)):
test.append(np.argmax(y_test[i]))
Now, we are checking the accuracy score
from sklearn.metrics import accuracy_score
a = accuracy_score(pred,test)
print('Accuracy is:', a*100)
#Using test data as validation data.
history1 = model.fit(X_train, y_train,validation_data = (X_test,y_test), epochs=100, batch_size=64)
Plotting the graph for Model Accuracy¶
# Model Accuracy
plt.plot(history1.history['accuracy'])
plt.plot(history1.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
Plotting graph for Model Loss¶
# Model Loss
plt.plot(history1.history['loss'])
plt.plot(history1.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
Saving the Model¶
Now, we save our model
model.save('mobile_price_range.h5')
Doing some predictions¶
from tensorflow.keras.models import load_model
# loading the model for predictions
m = load_model('mobile_price_range.h5')
# predicting the values
m.predict_classes(X_test)
DeepCC¶
!deepCC mobile_price_range.h5