Machine learning classification#

Building machine learning models to assign data to classes.

Machine learning has become an increasingly popular tool for solving classification problems.

The goal is to assign data points to pre-defined classes based on their features or attributes. This technique has numerous applications in a wide range of fields, from image and speech recognition to fraud detection and spam filtering. Building machine learning models to assign data to classes involves training algorithms on labelled datasets. Each data point is associated with a specific class label. By analyzing the relationships between the input features and the output labels, these models can learn to accurately classify new, unseen data points with high accuracy.

In this way, machine learning provides a powerful tool for automating classification tasks and enabling more efficient and effective decision-making.

How To#

from sklearn.model_selection import train_test_split
import pandas as pd

df = pd.read_csv("data/housing.csv")
df.head()
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity
0 -122.23 37.88 41.0 880.0 129.0 322.0 126.0 8.3252 452600.0 NEAR BAY
1 -122.22 37.86 21.0 7099.0 1106.0 2401.0 1138.0 8.3014 358500.0 NEAR BAY
2 -122.24 37.85 52.0 1467.0 190.0 496.0 177.0 7.2574 352100.0 NEAR BAY
3 -122.25 37.85 52.0 1274.0 235.0 558.0 219.0 5.6431 341300.0 NEAR BAY
4 -122.25 37.85 52.0 1627.0 280.0 565.0 259.0 3.8462 342200.0 NEAR BAY
df = df.dropna()
x_train, x_, y_train, y_ = train_test_split(df.drop(["longitude","latitude","ocean_proximity"], axis=1), 
                                                    df.ocean_proximity, test_size=.5)

x_val, x_test, y_val, y_test = train_test_split(x_, y_, test_size=.5)

Nearest Neighbours#

from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=10)
model.fit(x_train, y_train)
KNeighborsClassifier(n_neighbors=10)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
model.score(x_val, y_val)
0.6184416601409554

Random Forest#

from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.fit(x_train, y_train)
RandomForestClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
rf.score(x_val, y_val)
0.6746280344557557
rf.feature_importances_
array([0.12220447, 0.12414133, 0.10335339, 0.12570916, 0.1070064 ,
       0.1282978 , 0.28928745])

Logistic Regression#

from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=10000)
model.fit(x_train, y_train)
LogisticRegression(max_iter=10000)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
model.score(x_val, y_val)
0.5935787000783085
model.coef_
array([[-1.28589213e-02, -1.45582057e-04,  7.80472621e-06,
         1.97107580e-03, -2.38656787e-04, -4.15225567e-04,
         6.19538604e-06],
       [ 3.08189670e-02,  9.93227568e-04,  3.85477519e-03,
         1.37368748e-03, -7.43553753e-03,  3.61904769e-03,
        -7.00123023e-06],
       [-4.69770913e-04, -1.01691813e-03,  9.29061499e-05,
        -3.56225144e-03, -7.30620898e-04, -5.10418676e-05,
        -5.99984845e-06],
       [ 1.13350769e-03,  3.49030172e-04, -7.10773978e-03,
        -7.49259658e-04,  1.02013792e-02, -1.43019990e-03,
         2.43911231e-06],
       [-1.86237825e-02, -1.79757552e-04,  3.15225372e-03,
         9.66747823e-04, -1.79656402e-03, -1.72258035e-03,
         4.36658034e-06]])

Exercise#

Test different numbers of neighbours for the KNN classifier and see how pre-processing like scaling affects our results.

Additional Resources#