Zain Khan

Information Science & Computer Science Student at CU Boulder.

portfolio.sql

    
namerolelocation
Zain KhanInformation Science & Computer Science StudentBoulder

About me

I am an Information Science and Computer Science student at CU Boulder. I am extremely passionate about working with data and creating ways to understand it. Outside of school and work I’m a photographer. It’s a big reason I love exploring new places and meeting new people, and it’s the main way I do both. Currently open to data and analytics roles.

Projects

Select a project to see the full breakdown.

Machine Learning

Predicting Car Fuel Economy

Dataset: Auto MPG  |  Tools: Python, scikit-learn

Trained linear and polynomial regression models on 398 cars from the 1970s and 80s to predict miles per gallon. The polynomial model explained about 89% of the variation in fuel economy on cars it had never seen.

SQL

Camera Database Schema

Database: SQLite  |  Tools: SQL

Designed a relational database for camera bodies, lenses and lens mounts, with six tables and a join table that tracks which lenses fit which bodies.

Coming soon

More to come

More projects will be added here soon.

Skills

Programming

Python, Java, C++

Data & Analysis

SQL, pandas, scikit-learn, regression modeling, relational schema design, Excel, Tableau

Systems & IT

Windows, macOS, Linux, hardware troubleshooting, virtualized environments

Security

System hardening, ethical hacking basics, Linux terminal

Contact

SQL

Camera Database Schema

Database: SQLite
Tools: SQL

The idea

For this project I designed my own database schema by sketching one out. I picked cameras because it's something I'm interested in, and because camera bodies and lenses have a real relationship: a lens only fits a body that uses the same mount.

The schema

mountsmount_idPKintegernametextcamera_bodiesbody_idPKintegermodeltextbrandtextcountrytextmount_idFKintegersensor_sizetextmegapixelsrealrelease_yearintegeroperating_systemtextprice_usdreallenseslens_idPKintegermodeltextbrandtextcountrytextmount_idFKintegerfocal_min_mmintegerfocal_max_mmintegermax_aperturerealprice_usdreal

A query on the database

This query looks up my own setup, a Sony A7C II with a 24-70mm f/2.8 G Master lens, by joining the tables back together.

SELECT cb.brand,
       cb.model AS camera,
       l.model AS lens,
       m.name AS mount,
       cb.megapixels,
       l.focal_min_mm || '-' || l.focal_max_mm || 'mm' AS focal_range,
       'f/' || l.max_aperture AS max_aperture
FROM camera_bodies cb
JOIN mounts m ON cb.mount_id = m.mount_id
JOIN lenses l ON l.mount_id = cb.mount_id
WHERE cb.model = 'A7C II'
  AND l.model = 'FE 24-70mm F2.8 GM';

brandcameralensmountmegapixelsfocal_rangemax_aperture
SonyA7C IIFE 24-70mm F2.8 GMSony E33.024-70mmf/2.8

It starts at camera_bodies, joins mounts to get the mount name, then finds the lenses that use the same mount as that camera, and filters down to the two I own.

Machine Learning

Predicting Car Fuel Economy

Dataset: Auto MPG, 398 cars from 1970–82
Tools: Python, pandas, scikit-learn, matplotlib

0.892test R², polynomial model
0.806test R², linear baseline
398cars, 80 held back for testing

The dataset and why I chose it

This dataset has information on car models from the 70's and 80's. Older vehicles are an interest of mine, so being able to visualize data about them is something I really wanted to do. It also has a variety of datatypes to work with, which gave me a lot more to work with. The goal is to predict a car's fuel economy from its other features. I thought with how simple cars were back then, that it would be easier to estimate fuel economy.

Cleaning and feature engineering

  1. Missing values. Horsepower was stored as text because six rows had a "?" in them, so those had to be changed into proper null values before I could work with them. I filled them in with the median, which is 93.5.
  2. Balance and outliers. It seems that the origin column is imbalanced, with a vast majority of the cars being American made, 249 of 398 cars. Odd numbered cylinder engines are also incredibly uncommon, and four cylinders is the most common at 204 cars. Since fuel economy is a number, there aren't any classes to balance. I kept all of the rows because there were no absurd values that didn't seem accurate for a car from this era.
  3. Encoding. There are 305 unique car names out of 398 rows, so most are unique and the model wouldn't have much to learn from if I one hot encoded them. I pulled out the brand, which is the first word of the name, instead and fixed the typos and duplicates, like "chevy" and "chevrolet". Origin was one hot encoded since the numbers are just labels and not sequential.
  4. Correlated columns. Fuel economy has the strongest correlation with weight, and it is negative, so the heavier a car is, the worse its fuel efficiency is. Weight, displacement, horsepower, and cylinders are also all correlated with each other, which makes sense since the larger a vehicle is, the more powerful it seemingly needs to be. They were too highly correlated, so I removed cylinders and displacement so they don't harm the model.
0102030405060709152127333945fuel economy (mpg)number of cars
Most cars fall between about 13 and 30 mpg, with a smaller group of really efficient ones.
mpgcyldisphpweightaccelyear
mpg1.00-0.78-0.80-0.77-0.830.420.58
cyl-0.781.000.950.840.90-0.51-0.35
disp-0.800.951.000.900.93-0.54-0.37
hp-0.770.840.901.000.86-0.69-0.41
weight-0.830.900.930.861.00-0.42-0.31
accel0.42-0.51-0.54-0.69-0.421.000.29
year0.58-0.35-0.37-0.41-0.310.291.00
Correlation between columns. Red means they go up together and blue means one goes up as the other goes down. Weight has the strongest link to fuel economy, at -0.83.

Finding clusters without using mpg

I left out fuel economy, used only the features, and ran K-means on them. The elbow is at k=2 because that's where the most pronounced bend in the chart is. It split the cars into two groups: 301 lighter, lower horsepower cars that average about 2,592 lb and 86 hp and 97 much heavier and more powerful ones that average about 4,144 lb and 162 hp. The fuel economy between the two groups varies drastically, with the more efficient group at about 26.4 mpg and the more powerful, less efficient group at only a little over 14.5.

06001200180012345678number of clusters (k)inertiaelbow at k = 2
Elbow plot. The bend is at 2 clusters.
501001502002502,0003,0004,0005,000weight (lb)horsepower
lighter, lower horsepower   heavier, more powerful

Train/test split and the models

I held out 20% of the cars, which is 80, to test on and trained on the other 318. The model can't see the test cars while it trains, otherwise it would memorize them and make the scores look better than they actually are. Based on the shape of the fuel economy vs weight and horsepower plots there's a bit of a curve, so a polynomial regression made the most sense using degree 2 on horsepower, weight, acceleration, and model year. I trained a plain linear regression too, since knowing how it acts can show where it might be more accurate.

Results

R² is basically how much of the variation in fuel economy the model explains. On cars it hadn't seen before, the polynomial model explained about 89.2% of it and the linear model explained about 80.6%, so my initial prediction was correct. They are both somewhat accurate, but the polynomial version does have that slight edge, and on the graph it gets slightly tighter when the fuel economy is higher. The training and test scores were close for both, so neither seems badly overfit.

Linear0.806train 0.838
Polynomial0.892train 0.896
10203040501020304050actual fuel economy (mpg)predicted fuel economy (mpg)
Predicted fuel economy against actual fuel economy for the 80 test cars. The closer a dot is to the dashed line, the better the prediction.

What I learned

Fuel economy is driven by how big and powerful a vehicle is. Weight, displacement, horsepower, and cylinders were all negatively correlated with fuel economy, and letting the model curve got it about nine points of R² higher than a straight line. The data is small and outdated, so I don't expect it to be incredibly accurate for newer vehicles, considering the advancements made in technology and efficiency. There also aren't enough 3 and 5 cylinder cars for the model to learn much from.

I got a lot more comfortable with pandas and scikit-learn. pandas made the cleaning pretty easy, like turning the bad horsepower values into nulls with to_numeric and one hot encoding the brands with get_dummies. scikit-learn covered everything else, like SimpleImputer for the median, KMeans for the clusters, and a pipeline that scales the columns and adds the polynomial terms, so the exact same steps get applied to the test cars. The biggest thing I learned is to split the data before fitting anything and to score the model on cars it hasn't seen, since that is what shows whether it really works. I also refined my matplotlib skills.