Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. from sklearn.linear_model import LinearRegression
  2. from sklearn.preprocessing import StandardScaler
  3. from sklearn.preprocessing import PolynomialFeatures
  4. import re
  5.  
  6. cols_2012 = crime_realEstate.filter(regex='_2012').columns
  7. crime_realEstate['Area_Name']=crime_realEstate['Area_Name'].apply(lambda x: re.sub(' ', '_', str(x)))
  8. regDF_2012 = crime_realEstate[cols_2012]
  9. regDF_2012 = regDF_2012.assign(community_code=crime_finalDF['community_area'])
  10. regDF_2012.dropna(inplace=True)
  11. X_feats = regDF_2012.drop(['Avg_Price_2012'], axis=1)
  12. y_label = regDF_2012['Avg_Price_2012'].values
  13.  
  14. from sklearn.preprocessing import scale
  15. from sklearn.decomposition import PCA
  16. from sklearn.model_selection import KFold
  17. from sklearn.model_selection import cross_val_score
  18. import numpy as np
  19.  
  20. pca = PCA()
  21. pca_ = pca.fit_transform(scale(regDF_2012))
  22.  
  23. np.cumsum(np.round(pca.explained_variance_ratio_, decimals=4)*100)
  24.  
  25. n = len(pca_)
  26. kf_10 = KFold(n, n_folds=10, shuffle=True, random_state=0)
  27. # kf_10 = KFold(n, shuffle=True, random_state=2)
  28. regr = LinearRegression()
  29. mse = []
  30.  
  31. score = -1*cross_val_score(regr, np.ones((n,1)), y.ravel(), cv=kf_10, scoring='mean_squared_error').mean()
  32. mse.append(score)
  33.  
  34. for i in np.arange(1,6):
  35. score = -1*cross_val_score(regr, X_reduced[:,:i], y.ravel(), cv=kf_10, scoring='mean_squared_error').mean()
  36. mse.append(score)
  37.  
  38. fig, (ax1, ax2) = plt.subplots(1,2, figsize=(12,5))
  39. ax1.plot(mse, '-v')
  40. ax2.plot([1,2,3,4,5], mse[1:6], '-v')
  41. ax2.set_title('Intercept excluded from plot')
  42.  
  43. for ax in fig.axes:
  44. ax.set_xlabel('Number of principal components in regression')
  45. ax.set_ylabel('MSE')
  46. ax.set_xlim((-0.2,5.2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement