In [1]:
import warnings
warnings.filterwarnings('ignore')
In [2]:
%matplotlib inline
%pylab inline
In [3]:
# we need to make sure we have 0.20 for Plotting
# !conda update pandas -y
In [4]:
import pandas as pd
print(pd.__version__)
In [5]:
df = pd.read_csv('./insurance-customers-300.csv', sep=';')
In [6]:
y=df['group']
In [7]:
df.drop('group', axis='columns', inplace=True)
In [8]:
X = df.as_matrix()
In [9]:
df.describe()
Out[9]:
In [10]:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
plt.clf()
plt.figure(figsize=(20, 20))
CMAP = ListedColormap(['#AA4444', '#006000', '#FFFF00'])
pd.plotting.scatter_matrix(df, c=y, cmap=CMAP, s=200, edgecolor='black', figsize=(20, 20), diagonal='kde')
plt.show()
# plt.savefig('scatter-matrix.png')
In [11]:
# ignore this, it is just technical code
# should come from a lib, consider it to appear magically
# http://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
cmap_bold = ListedColormap(['#AA4444', '#006000', '#AAAA00'])
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#FFFFDD'])
font_size=25
def meshGrid(x_data, y_data):
h = 1 # step size in the mesh
x_min, x_max = x_data.min() - 1, x_data.max() + 1
y_min, y_max = y_data.min() - 1, y_data.max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
return (xx,yy)
def plotPrediction(clf, x_data, y_data, x_label, y_label, colors, title="", mesh=True):
xx,yy = meshGrid(x_data, y_data)
plt.figure(figsize=(20,10))
if mesh:
Z = clf.predict(np.c_[yy.ravel(), xx.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.scatter(x_data, y_data, c=colors, cmap=cmap_bold, s=80, marker='o')
plt.xlabel(x_label, fontsize=font_size)
plt.ylabel(y_label, fontsize=font_size)
plt.title(title, fontsize=font_size)
In [12]:
X_kmh_age = X[:, :2]
X_2_dim = X_kmh_age
In [13]:
plotPrediction(None, X_2_dim[:, 1], X_2_dim[:, 0],
'Age', 'Max Speed', y, mesh=False,
title="All Data Max Speed vs Age")
In [11]:
corrmat = df.corr()
In [15]:
import seaborn as sns
sns.heatmap(corrmat, annot=True)
figure = plt.gcf()
figure.set_size_inches(10, 10)
plt.show()
# plt.savefig('corr.png')
In [16]:
def plot(col1, col2):
# https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.jointplot.html#seaborn.jointplot
sns.jointplot(df[col1],df[col2],dropna=True, kind="hex")
figure = plt.gcf()
figure.set_size_inches(10, 10)
# for notebook
plt.show()
# plt.savefig('%s/%s_%s.png'%(IMG_DIR, col1, col2), dpi = DPI)
In [17]:
plot('max speed', 'age')
In [18]:
plot('max speed', 'thousand km per year')
In [19]:
plot('age', 'thousand km per year')
In [20]:
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
In [23]:
max_speed = df['max speed']
thousand_km_per_year = df['thousand km per year']
plt.scatter(max_speed, thousand_km_per_year, color='black')
plt.xlabel('Max Speed')
plt.ylabel('Thousand Km per Year')
Out[23]:
In [24]:
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(max_speed.reshape(-1, 1), thousand_km_per_year.reshape(-1, 1))
Out[24]:
In [25]:
thousand_km_per_year_pred = regr.predict(max_speed.reshape(-1, 1))
In [27]:
mean_squared_error(thousand_km_per_year, thousand_km_per_year_pred)
Out[27]:
In [28]:
# Explained variance score: 1 is perfect prediction, pretty good score
r2_score(thousand_km_per_year, thousand_km_per_year_pred)
Out[28]:
In [29]:
plt.scatter(max_speed, thousand_km_per_year, color='black')
plt.xlabel('Max Speed')
plt.ylabel('Thousand Km per Year')
plt.plot(max_speed, thousand_km_per_year_pred, color='red', linewidth=3)
Out[29]:
In [ ]: