<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 DM_05_04
Import Libraries
Step1:  Read CSV
Step2:  Save state_code to label outliers. "data" contains just quantitative variables.
Step3:  Univariate Outliers
Create a box plot to display univariate outliers on "modern dance."
Step4:  Get quantile values and IQR for outlier limits.
Step5:  Get positions of outliers and use state_code for labels.
Step6:  Create box plot for "modern dance."
Step7:  Bivariate Outliers
Create a scatterplot with an ellipse as a boundary for outliers.
Use the Google search terms "data science" and "ceo" for this example.
Step8:  Compute the "elliptical envelope."
Step9:  Get the names and positions of outliers.
Step10:  Calculate the decision boundary for the scatterplot.
Step11:  Draw the scatterplot with the elliptical envelope and label the outliers.
Step12:  Multivariate Outliers
Use the one-class support vector machine (SVM) algorithm to classify unusual cases.
Step13:  List the names of the outlying states based on the one-class SVM.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import pylab
import numpy as np
import pandas as pd
from sklearn.svm import OneClassSVM
from sklearn.covariance import EllipticEnvelope
pylab.rcParams.update({'font.size': 14})
Explanation: DM_05_04
Import Libraries
End of explanation
df = pd.read_csv("AnomalyData.csv")
df.head()
Explanation: Read CSV
End of explanation
state_code = df["state_code"]
data = df.loc[:, "data science": "Openness"]
Explanation: Save state_code to label outliers. "data" contains just quantitative variables.
End of explanation
param = "modern dance"
Explanation: Univariate Outliers
Create a box plot to display univariate outliers on "modern dance."
End of explanation
qv1 = data[param].quantile(0.25)
qv2 = data[param].quantile(0.5)
qv3 = data[param].quantile(0.75)
qv_limit = 1.5 * (qv3 - qv1)
Explanation: Get quantile values and IQR for outlier limits.
End of explanation
un_outliers_mask = (data[param] > qv3 + qv_limit) | (data[param] < qv1 - qv_limit)
un_outliers_data = data[param][un_outliers_mask]
un_outliers_name = state_code[un_outliers_mask]
Explanation: Get positions of outliers and use state_code for labels.
End of explanation
fig = pylab.figure(figsize=(4,6))
ax = fig.add_subplot(1, 1, 1)
for name, y in zip(un_outliers_name, un_outliers_data):
    ax.text(1, y, name)
ax.boxplot(data[param])
ax.set_ylabel(param)
Explanation: Create box plot for "modern dance."
End of explanation
params = ["data science", "ceo"]
params_data = np.array([df[params[0]], df[params[1]]]).T
Explanation: Bivariate Outliers
Create a scatterplot with an ellipse as a boundary for outliers.
Use the Google search terms "data science" and "ceo" for this example.
End of explanation
ee = EllipticEnvelope()
ee.fit(params_data)
Explanation: Compute the "elliptical envelope."
End of explanation
biv_outliers_mask = ee.predict(params_data) == -1
biv_outliers_data = params_data[biv_outliers_mask]
biv_outliers_name = state_code[biv_outliers_mask]
Explanation: Get the names and positions of outliers.
End of explanation
xx, yy = np.meshgrid(np.linspace(params_data[:, 0].min(), params_data[:, 0].max(), 100),
                     np.linspace(params_data[:, 1].min(), params_data[:, 1].max(), 100))
zz = ee.decision_function(np.c_[xx.ravel(), yy.ravel()])
zz = zz.reshape(xx.shape)
Explanation: Calculate the decision boundary for the scatterplot.
End of explanation
fig = pylab.figure(figsize=(10,10))
ax = fig.add_subplot(1, 1, 1)
for name, xy in zip(biv_outliers_name, biv_outliers_data):
    ax.text(xy[0], xy[1], name)
ax.contour(xx, yy, zz, levels=[0], linewidths=2)
ax.scatter(params_data[:, 0], params_data[:, 1], color='black')
ax.set_xlabel(params[0])
ax.set_ylabel(params[1])
Explanation: Draw the scatterplot with the elliptical envelope and label the outliers.
End of explanation
ocsvm = OneClassSVM(nu=0.25, gamma=0.05)
ocsvm.fit(data)
Explanation: Multivariate Outliers
Use the one-class support vector machine (SVM) algorithm to classify unusual cases.
End of explanation
# 
state_code[ocsvm.predict(data) == -1]
Explanation: List the names of the outlying states based on the one-class SVM.
End of explanation
<END_TASK>