<jupyter_start><jupyter_text><jupyter_code>check_dist(data)
# checking correlation matrix
def corr_met(data):
    corrmat = data.corr()
    cols = corrmat.nlargest(15, 'price')['price'].index
    cm = np.corrcoef(data[cols].values.T)
    f, ax = plt.subplots(figsize = (12,9)) #defining figure size
    sns.set(font_scale = 1.25) #font size
    hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values, xticklabels=cols.values, cmap="YlGnBu")
    plt.show()
corr_met(data)
corrmat = data.corr()
cols = corrmat.nlargest(15, 'price')['price'].index
data = data[cols]
def graph(data):
    sns.distplot(data, fit = norm, color = 'g')
    fig = plt.figure()
print("Before normalizing data: ")
graph(data['price'])
#########
data['price'] = np.log(data['price'])
print("Plot after normalizing: ")
graph(data['price'])
print("Plot before normalizing: ")
graph(data['sqft_living'])

data['sqft_living'] = np.log(data['sqft_living'])
print("Plot after normalizing: ")
graph(data['sqft_living'])
print("Plot before normalizing: ")
graph(data['sqft_living15'])
data['sqft_living15'] = np.log(data['sqft_living15'])
print("Plot after normalizing: ")
graph(data['sqft_living15'])
y = data.iloc[:, 0:1]
X = data.iloc[:,:]
X.drop('price', axis = 1, inplace = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 1)
# we'll write two functions here, determines our score
scorer = make_scorer(mean_squared_error, greater_is_better = False)

def mse_cv_test(model):
    mse= - cv.cross_val_score(model, X_test, y_test, scoring = scorer, cv = 10)
    return(mse)

def r2_scorer(pred):
    r2 = r2_score(y_test, pred)
    print("R2 Score: %.3f" %r2)
lasso = LassoCV(alphas = [0.001, 0.01, 0.1, 1, 2], max_iter = 50000, cv = 10)
lasso.fit(X_train, y_train)
alpha = lasso.alpha_
print("Best alpha :", alpha)
y_test_las = lasso.predict(X_test)
r2_scorer(y_test_las)<jupyter_output>Best alpha : 0.001
R2 Score: 0.743<jupyter_text>$(True Y) - (Estimated Y) = Error$$Error^2 = Square Error$$All(Square Error) / n = MSE$ The mean squared error tells you how close a regression line is to a set of points. It does this by taking the distances from the points to the regression line (these distances are the “errors”) and squaring them. (True Y) - (Estimated Y) = ErrorError ** 2 = Square ErrorAll(Square Error) / n = MSEExample...<jupyter_code>points = [26,32,29,45,34]
estim = [27.4, 31.5, 27.1, 43.6, 33.2]
y_data = pd.DataFrame({'Y': points})
y_data.head()

es_data = pd.DataFrame({'Estimated':estim})
es_data.head()

#add datas together -> we could use merge, concat, etc.
full_data = y_data.join(es_data)
full_data.head()
full_data['Error'] = full_data['Y'] - full_data['Estimated']
full_data.head()
full_data['Error^2'] = full_data['Error']**2
full_data.head()
# to find the MSE, we'll add all values in Error-squared and divide by 5
mse_value = np.sum(full_data['Error^2'])/5
print("MSE for this data is: {:.2f}".format(mse_value))
print("Lasso MSE on Test set : %.3f" %(mse_cv_test(lasso).mean()))
#implementing kernel ridge regression
ridge_reg = KernelRidge(kernel = 'linear', gamma = None, degree = None, coef0 = 1, kernel_params = None, alpha = 1)
ridge_reg.fit(X_train, y_train)
ridge_prid = ridge_reg.predict(X_test)
r2_scorer(ridge_prid)
print("Linear KRR MSE on Test set : %.3f" %(mse_cv_test(ridge_reg).mean()))<jupyter_output>Linear KRR MSE on Test set : 0.100<jupyter_text>Performing GridSearchCV<jupyter_code>from sklearn.grid_search import GridSearchCV
CV_ridge = GridSearchCV(KernelRidge(kernel = 'polynomial', gamma = 1), cv = 10, param_grid = {'degree':[2,5,6]})
CV_ridge.fit(X_train, y_train)
CV_ridge_pred = CV_ridge.predict(X_test)
r2_scorer(CV_ridge_pred = CV_ridge.predict(X_test))
print("Polynomial KRR MSE on Test set : %.3f" %(mse_cv_test(CV_ridge).mean()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1617873651771957, 'fraction_numerical': 0.02953261427837699, 'mean_word_length': 5.845342706502636, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '390591', 'license': 'MIT', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1496, 'n_words': 345, 'path': 'Data Exploring/Data Exploring 12-25-17/Ridge+Lasso+OLS Regression.ipynb', 'repo_name': 'Olow304/Data-Analysis-Science', 'size': '3894'}