Advertisement
BasicTask

Faktoranalízis Python

Feb 10th, 2020
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. ##Faktoranalízis Python-ban
  2. #!/usr/bin/env python
  3. # coding: utf-8
  4.  
  5. import os
  6. import pandas as pd
  7. import numpy as np
  8. from sklearn.decomposition import PCA
  9. from sklearn import preprocessing
  10. import matplotlib.pyplot as plt
  11.  
  12.  
  13. ldir=os.chdir('C:\\Users\\36307\\Documents\\hdd\\Egyetem\\')
  14. df=pd.read_csv('2017.csv')
  15. df
  16.  
  17.  
  18. countries = df['Country'].tolist()
  19. df.drop(['Happiness.Score','Happiness.Rank','Whisker.high', 'Whisker.low'],axis=1,inplace=True)
  20. df = df.set_index('Country')
  21. df.head()
  22.  
  23.  
  24.  
  25. scaled_data = preprocessing.scale(df)
  26. scaled_data
  27.  
  28.  
  29. pca = PCA()
  30. pca.fit(scaled_data)
  31. pca_data = pca.transform(scaled_data)
  32. pca_data
  33.  
  34.  
  35.  
  36. per_var = np.round(pca.explained_variance_ratio_* 100, decimals=1)
  37. labels = ['FK' + str(x) for x in range(1, len(per_var)+1)]
  38. plt.bar(x=range(1,len(per_var)+1), height=per_var, tick_label=labels)
  39. plt.ylabel('Magyarázóerő (%)')
  40. plt.xlabel('Főkomponens')
  41. plt.title('Scree Plot')
  42. plt.show()
  43.  
  44.  
  45. loading_scores = pd.Series(pca.components_[0])
  46. sorted_loading_scores = loading_scores.abs().sort_values(ascending=False)
  47. print(sorted_loading_scores)
  48.  
  49.  
  50. loadings = pd.Series(loading_scores)
  51. factors = pd.Series(df.columns)
  52. pd.concat([loadings, factors], axis=1)
  53.  
  54.  
  55. from sklearn.metrics import euclidean_distances
  56. from sklearn import manifold
  57.  
  58.  
  59. seed = np.random.RandomState(seed=3)
  60. similarities = euclidean_distances(df)
  61. mds = manifold.MDS(n_components=3, max_iter=3000, eps=1e-9,
  62.       random_state=seed, dissimilarity="precomputed", n_jobs=1)
  63. pos = mds.fit(similarities).embedding_
  64. pos
  65.  
  66.  
  67. from matplotlib import pyplot
  68. from mpl_toolkits.mplot3d import Axes3D
  69. from numpy.random import rand
  70. from pylab import figure
  71.  
  72. fig = figure()
  73. ax = Axes3D(fig)
  74.  
  75. for i in range(len(pos)):
  76.  ax.scatter(pos[i,0],pos[i,1],pos[i,2],color='b')
  77.  ax.text(pos[i,0],pos[i,1],pos[i,2], countries[i], size=6, zorder=1, color='k')
  78. ax.set_xlabel('x')
  79. ax.set_ylabel('y')
  80. ax.set_zlabel('z')
  81. ax.set_title('MDS')
  82. pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement