<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Multivariable Regression Model of FBI Property Crime Statistics
Using the FBI
Step1:  Perfect accuracy, as expected. However......
Predicting ALL property crimes is a more interesting question. 
Building a Model to Predict Property Crimes (without using the Property Crime features)
To start, let's take a look at how each of the non-property crime features interact with property crime.
Step2:  That single outlier is making the relationships difficult to view. Let's remove the outlier.
Step3:  There is a large number of 0's for Murder. Perhaps let's use a binary value for murder occurring vs no murder occurring.
Step4:  Noice!!
What about performance when the binary Murder feature is used?
Step5:  There is a slight increase of performance when the binary indicator for murder is used.  
Leave no man behind!
Reintroduce the outlier to the model.
Step6:  Hmmmm....it seems that outlier result has also heavily weighted the R-squared result and coefficients. The linear model which did not incorporate the outlier is likely to be a better indicator of overall trends and accuracy. 
Best Model
Step7:  Validating regression models for prediction
Now let's use cross-validation to obtain a more accurate description of our accuracy
Step8:  Test the Model with Data From Another State
Now let's test our model with the 2013 Crime Rate dataset for California. Will the predictive power be similar?
<ASSISTANT_TASK:>
Python Code:
import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns 
from sklearn import linear_model
# Suppress annoying harmless error.
warnings.filterwarnings(
    action="ignore"
)
data_path = "https://raw.githubusercontent.com/Thinkful-Ed/data-201-resources/master/New_York_offenses/NEW_YORK-Offenses_Known_to_Law_Enforcement_by_City_2013%20-%2013tbl8ny.csv"
data = pd.read_csv(data_path, delimiter = ',', skiprows=4, header=0, skipfooter=3, thousands=',')
data = pd.DataFrame(data)
data.head()
# Instantiate and fit our model.
regr = linear_model.LinearRegression()
Y = data['Property\ncrime'].values.reshape(-1, 1)
X = data[["Larceny-\ntheft", "Motor\nvehicle\ntheft", "Burglary"]]
regr.fit(X, Y)
# Inspect the results.
print('\nCoefficients: \n', regr.coef_)
print('\nIntercept: \n', regr.intercept_)
print('\nR-squared:')
print(regr.score(X, Y))
Explanation: Multivariable Regression Model of FBI Property Crime Statistics
Using the FBI:UCR Crime dataset, which can be found here,   build a regression model to predict property crimes. 
The FBI defines property crime as including the offenses of burglary, larceny-theft, motor vehicle theft, and arson. To predict property crime, one can simply use these features.
End of explanation
plt.figure(figsize=(15,5))
sns.pairplot(data, vars =['Property\ncrime', 'Population', 'Violent\ncrime',
       'Murder and\nnonnegligent\nmanslaughter',
       'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']) 
plt.show()
Explanation: Perfect accuracy, as expected. However......
Predicting ALL property crimes is a more interesting question. 
Building a Model to Predict Property Crimes (without using the Property Crime features)
To start, let's take a look at how each of the non-property crime features interact with property crime.
End of explanation
dataCleaned = data[data["Property\ncrime"] < 20000]
plt.figure(figsize=(15,5))
sns.pairplot(dataCleaned, vars =['Property\ncrime', 'Population', 'Violent\ncrime',
       'Murder and\nnonnegligent\nmanslaughter',
       'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']) 
plt.show()
plt.scatter(dataCleaned["Property\ncrime"], dataCleaned["Murder and\nnonnegligent\nmanslaughter"])
plt.title('Raw values')
plt.xlabel("Property Crime")
plt.ylabel("Murder")
plt.show()
Explanation: That single outlier is making the relationships difficult to view. Let's remove the outlier.
End of explanation
dataCleaned["Murder"] = dataCleaned['Murder and\nnonnegligent\nmanslaughter'].apply(lambda x: 0 if x == 0 else 1)
plt.scatter(dataCleaned["Property\ncrime"], dataCleaned["Murder"])
plt.title('Raw values')
plt.xlabel("Property Crime")
plt.ylabel("Murder")
plt.show()
dataCleaned.head()
regr = linear_model.LinearRegression()
Y = dataCleaned['Property\ncrime'].values.reshape(-1, 1)
X = dataCleaned[['Population', 'Violent\ncrime',
       'Murder and\nnonnegligent\nmanslaughter',
       'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
regr.fit(X, Y)
# Inspect the results.
print('\nCoefficients: \n', regr.coef_)
print('\nIntercept: \n', regr.intercept_)
print('\nR-squared:')
print(regr.score(X, Y))
Explanation: There is a large number of 0's for Murder. Perhaps let's use a binary value for murder occurring vs no murder occurring.
End of explanation
regr = linear_model.LinearRegression()
Y = dataCleaned['Property\ncrime'].values.reshape(-1, 1)
X = dataCleaned[['Population', 'Violent\ncrime',
       'Murder', 'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
regr.fit(X, Y)
# Inspect the results.
print('\nCoefficients: \n', regr.coef_)
print('\nIntercept: \n', regr.intercept_)
print('\nR-squared:')
print(regr.score(X, Y))
Explanation: Noice!!
What about performance when the binary Murder feature is used?
End of explanation
data["Murder"] = data['Murder and\nnonnegligent\nmanslaughter'].apply(lambda x: 0 if x == 0 else 1)
regr = linear_model.LinearRegression()
Y = data['Property\ncrime'].values.reshape(-1, 1)
X = data[['Population', 'Violent\ncrime',
       'Murder', 'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
regr.fit(X, Y)
# Inspect the results.
print('\nCoefficients: \n', regr.coef_)
print('\nIntercept: \n', regr.intercept_)
print('\nR-squared:')
print(regr.score(X, Y))
Explanation: There is a slight increase of performance when the binary indicator for murder is used.  
Leave no man behind!
Reintroduce the outlier to the model.
End of explanation
regr = linear_model.LinearRegression()
Y = dataCleaned['Property\ncrime'].values.reshape(-1, 1)
X = dataCleaned[['Population', 'Violent\ncrime',
       'Murder', 'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
regr.fit(X, Y)
# Inspect the results.
print('\nCoefficients: \n', regr.coef_)
print('\nIntercept: \n', regr.intercept_)
print('\nR-squared:')
print(regr.score(X, Y))
Explanation: Hmmmm....it seems that outlier result has also heavily weighted the R-squared result and coefficients. The linear model which did not incorporate the outlier is likely to be a better indicator of overall trends and accuracy. 
Best Model:
End of explanation
from sklearn.cross_validation import cross_val_score
regr = linear_model.LinearRegression()
y = data['Property\ncrime'].values.reshape(-1, 1)
X = data[['Population', 'Violent\ncrime',
       'Murder', 'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
scores = cross_val_score(regr, X, y, cv = 10)
print("Percent accuracy within each fold:\n")
print(scores)
print("\nMean accuracy:\n")
print(scores.mean())
Explanation: Validating regression models for prediction
Now let's use cross-validation to obtain a more accurate description of our accuracy:
End of explanation
data_path = "files/table_8_offenses_known_to_law_enforcement_california_by_city_2013.csv"
dataCA = pd.read_csv(data_path, delimiter = ',', skiprows=4, header=0, skipfooter=3, thousands=',')
dataCA = pd.DataFrame(dataCA)
dataCA.head()
dataCA["Murder"] = dataCA['Murder and\nnonnegligent\nmanslaughter'].apply(lambda x: 0 if x == 0 else 1)
y = dataCA['Property\ncrime'].values.reshape(-1, 1)
X = dataCA[['Population', 'Violent\ncrime',
       'Murder', 'Rape\n(legacy\ndefinition)2',
       'Robbery', 'Aggravated\nassault']]
scores = cross_val_score(regr, X, y, cv = 10)
print("Percent accuracy within each fold:\n")
print(scores)
print("\nMean accuracy:\n")
print(scores.mean())
Explanation: Test the Model with Data From Another State
Now let's test our model with the 2013 Crime Rate dataset for California. Will the predictive power be similar?
End of explanation
<END_TASK>