Challenge

Project 2

# importing packages
import pandas as pd
import numpy as np
import seaborn as sns

import matplotlib.mlab as mlab
import matplotlib
import matplotlib.pyplot as plt

# read the data
df = pd.read_csv('downloads:/project2/auto.csv')
print(df.head())
print(df.info())
print(df.describe())
print(df.describe(include = "all"))

# Adding column names to dataframe
df.columns = ["symboling","normalized-losses","make","fuel","aspiration","number of doors","body-style","drive-wheels","engine-location","wheel-base","length","width","height","curb-weight","engine-type","num-of-cylinders","engine-size","fuel-system","bore","stroke","compression-ratio","horse-power","peak-rpm","city-mpg","price"]

# Display the column names
print(list(df.columns))
print(df.head())
print(df.describe())

df = df.replace('?', np.NAN)
print(df.head())

df [["price"]] = df[["price"]].astype(str).astype(float)
mean = df[["price"]].mean(axis=0, skipna = True)
print('mean value is :',mean )
df[["price"]] = df[["price"]].replace(np.NaN, mean)

df[["normalized-losses"]] = df[["normalized-losses"]].astype(str).astype(float)
mean = df[["normalized-losses"]].mean(axis=0,skipna=True)
print('mean value is: ',mean)
df[["normalized-losses"]] = df[["normalized-losses"]].replace(np.NaN, mean)

df[["bore"]] = df[["bore"]].astype(str).astype(float)
mean = df[["bore"]].mean(axis=0, skipna = True)
print('mean value is: ',mean)
df[["bore"]] = df[["bore"]].replace(np.NaN,mean)

df[["stroke"]] = df[["stroke"]].astype(str).astype(float)
mean = df[["stroke"]].mean(axis=0, skipna = True)
print('mean value is: ',mean)
df[["stroke"]] = df[["stroke"]].replace(np.NaN,mean)

df[["horsepower"]] = df[["horsepower"]].astype(str).astype(float)
mean = df[["horsepower"]].mean(axis=0, skipna = True)
print('mean value is: ',mean)
df[["horsepower"]] = df[["horsepower"]].replace(np.NaN,mean)

df[["peak-rpm"]] = df[["peak-rpm"]].astype(str).astype(float)
mean = df[["peak-rpm"]].mean(axis=0, skipna = True)
print('mean value is: ',mean)
df[["peak-rpm"]] = df[["peak-rpm"]].replace(np.NaN,mean)

print(df.info())
#print(df)
print(df.loc[7:12,['price']])

#Binding data
# Binning the price to low, medium, High
bindwidth = int((max(df['price'])-min(df['price']))/3.0)
bins = range(int(min(df['price'])),int(max(df['price'])),bindwidth)
df['price-binned'] = pd.cut(df['price'],bins, labels = ['low','medium','High'])
#df["price-Bin"] = df['price-binned']
#print(df.loc[15:20,,['price','price-binned']])
print(df)

# Binning the horsepower to low, medium, High
bindwidth = int((max(df['horsepower'])-min(df['horsepower']))/3.0)
bins = range(int(min(df['horsepower'])),int(max(df['horsepower'])),bindwidth)
df['horsepower-binned'] = pd.cut(df['horsepower'],bins, labels = ['low','medium','High'])
#df["horsepower-Bin"] = df['horsepower-binned']
#print(df.loc[15:20,,['horsepower','horsepower-binned']])
print(df)

# label encoding the data
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['make-binned']= le.fit_transform(df['make'])
print(df['make-binned'])
print(df)

# visualization of correction with seaborn heatmap
f,ax = plt.subplots(figsize = (10,10))
sns.heatmap(df.corr(),annot = True)
plt.show()

# Numerical variables and their correction with price
sns.regplot(x = "engine-size",y = "price", data = df)
plt.show()
print(df[["engine-size", "price"]].corr())
sns.regplot(x = "Horsepower",y = "price", data = df)
plt.show()
print(df[["Horsepower", "price"]].corr())
sns.regplot(x = "highway-mpg",y = "price", data = df)
plt.show()
print(df[["highway", "price"]].corr())
sns.regplot(x = "city-mpg",y = "price", data = df)
plt.show()
print(df[["city-mpg", "price"]].corr())

# categorial variables and their correlation with the price
sns.boxplot(x="engine-location", y="price", data = df)
plt.show()
sns.boxplot(x="aspiration", y="price", data = df)
plt.show()
sns.boxplot(x="body-style", y="price", data = df)
plt.show()

#Prediction with kmeans
df.columns = df.columns.str.replace("-","-")

df1 = pd.DataFrame(columns = ["symboling","make","fuel","aspiration","no. of doors","body_style","drive_wheels","engine_location","engine_type","no. of cylinders","fuel_system","compression_ratio","price_binned","horsepower_binned","make_binned"])
df1 = df[["symboling","make","fuel","aspiration","no. of doors","body_style","drive_wheels","engine_location","engine_type","no. of cylinders","fuel_system","compression_ratio","price_binned","horsepower_binned","make_binned"]]
#print(df1)

df2 = pd.DataFrame(columns = ["normalized_losses","wheel_base","length","width","height","curb_weight","engine_size","bore","stroke","horsepower","peak_rpm","city_mpg","highway_mpg","price"])
df2 = df[["normalized_losses","wheel_base","length","width","height","curb_weight","engine_size","bore","stroke","horsepower","peak_rpm","city_mpg","highway_mpg","price"]]
df2_norm = (df2 - df2.min()) / (df2.max() - df2.min()) # scaling
#print(df2_norm)

df3 = pd.concat([df2_norm, df1],axis = 1)
#print(df3)

#print('0 : alfa-romero',"n" '1: audi',"m" '2 : bmw',"n" '3 : chevrolet',"n" '4 : dodge',"n" '5 : honda',"n" '6 : isuzu',"n" '7 : jaguar',"n" '8 : mazda',"n" '9 : mercedes-benz',"n" '10 : mercury', "n" '11 : mitsubishi', "n" '12 : nissan', "n" '13 : peugot',"n" '14 : plymouth',"n" '15 : porsche', "n" '16 : renault',"n" '17: saab',"n" '18 : subaru',"n" '19 : toyota',"n" '20 : volkswagen',"n" '21 : volvo')
sns.countplot(x = df3.make_binned, label = "counts")
plt.show()

# Clustering for make and horsepower
data = df3[["make_binned","price"]]

from sklearn.cluster import Kmeans
sse = []

# Determine the suitable 'k' value from elbow curve
for k in range (2,10):
kmeans = KMeans(n_clusters = k)
KMeans.fit(data)
sse.append(kmeans.inertia_)

plt.plot(range(2,10),sse)
plt.title("Elbow Curve")
plt.show()

km = KMeans(n_clusters = 7)
Y = km.fit_predict(data)

data["Cluster"] = Y
#print(data)

plt.scatter(data.make_binned[data.Cluster == 0], data.prince[data.Cluster == 0], color = "red")
plt.scatter(data.make_binned[data.Cluster == 1], data.prince[data.Cluster == 1], color = "Cyan")
plt.scatter(data.make_binned[data.Cluster == 2], data.prince[data.Cluster == 2], color = "pink")
plt.scatter(data.make_binned[data.Cluster == 3], data.prince[data.Cluster == 3], color = "brown")
plt.scatter(data.make_binned[data.Cluster == 4], data.prince[data.Cluster == 4], color = "gold")
plt.scatter(data.make_binned[data.Cluster == 5], data.prince[data.Cluster == 5], color = "marron")
plt.scatter(data.make_binned[data.Cluster == 6], data.prince[data.Cluster == 6], color = "yellow")
plt.scatter(data.make_binned[data.Cluster == 7], data.prince[data.Cluster == 7], color = "orange")
plt.scatter(data.make_binned[data.Cluster == 8], data.prince[data.Cluster == 8], color = "green")

plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:,1], s = 150, color = "black",marker = '*')
plt.title("k-means Data Clustering")
plt.show()

#Prediction with supervised learning
from sklearn import datasets, linear_model
from sklearn.metrics import confussion_matrix
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

x = data[["price"]]
y = data[["make_binned"]]


#print(x)
#print(y)
x_train, x_test,y_train,y_test = train_test_split(x, y, test_size = 0.40, random_state = 150)

#print("x_train: ",x_train)
#print("y_train: ",y_train)
#print("x_test: ",x_test)
#print("y_test: ",y_test)

import sklearn.linear_model
model = sklearn.linear_model.linearRegression()
model.fit(X,Y)
plt.show()
#print('The score is : ', model.score(x_test, y_test))

y_predict = model.predict(x_test)
#print(y_predict)

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.