Mobile Price Classification using Machine Learning Models
This project involves using various machine learning models to classify mobile phones into different price ranges based on their features. We utilize popular libraries such as pandas for data manipulation and scikit-learn for model training and evaluation. The models used include SVC, DecisionTreeClassifier, RandomForestClassifier, and GradientBoostingClassifier. The dataset used is provided in 'train.csv'.
- Python 3.x
- pandas
- matplotlib
- scikit-learn
- Clone the repository or download the project files.
- Ensure you have the required libraries installed. You can install them using pip:
pip install pandas matplotlib scikit-learn
-
Load the dataset:
import pandas as pd df = pd.read_csv('train.csv')
-
Print out the first 5 rows of the dataset:
df.head()
-
Find the unique values in the price range:
df['price_range'].unique()
-
Check for null values:
print(df.isnull().sum())
-
Data Preprocessing:
from sklearn.model_selection import train_test_split X = df.drop(columns=['price_range']) y = df.iloc[:, -1] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
-
Get the description of each column in the training data:
X_train.describe()
-
Train and evaluate models:
from sklearn.metrics import classification_report from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import GradientBoostingClassifier import matplotlib.pyplot as plt def evaluationTest(model): y_pred = model.predict(X_test) print(classification_report(y_test, y_pred)) fig, ax = plt.subplots(figsize=(8, 5)) cmp = ConfusionMatrixDisplay(confusion_matrix(y_test, y_pred), display_labels=["class_0", "class_1", "class_2", "class_3"]) cmp.plot(ax=ax) plt.show() models = { 'Decision Tree': DecisionTreeClassifier(), 'Random Forest': RandomForestClassifier(), 'Support Vector Machine': SVC(kernel='linear', gamma=0.5, C=1.0), 'Gradient Boosting': GradientBoostingClassifier(), } for name, model in models.items(): model.fit(X_train, y_train) print(name) evaluationTest(model)