Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Faktoranalízis Python-ban
- #!/usr/bin/env python
- # coding: utf-8
- import os
- import pandas as pd
- import numpy as np
- from sklearn.decomposition import PCA
- from sklearn import preprocessing
- import matplotlib.pyplot as plt
- ldir=os.chdir('C:\\Users\\36307\\Documents\\hdd\\Egyetem\\')
- df=pd.read_csv('2017.csv')
- df
- countries = df['Country'].tolist()
- df.drop(['Happiness.Score','Happiness.Rank','Whisker.high', 'Whisker.low'],axis=1,inplace=True)
- df = df.set_index('Country')
- df.head()
- scaled_data = preprocessing.scale(df)
- scaled_data
- pca = PCA()
- pca.fit(scaled_data)
- pca_data = pca.transform(scaled_data)
- pca_data
- per_var = np.round(pca.explained_variance_ratio_* 100, decimals=1)
- labels = ['FK' + str(x) for x in range(1, len(per_var)+1)]
- plt.bar(x=range(1,len(per_var)+1), height=per_var, tick_label=labels)
- plt.ylabel('Magyarázóerő (%)')
- plt.xlabel('Főkomponens')
- plt.title('Scree Plot')
- plt.show()
- loading_scores = pd.Series(pca.components_[0])
- sorted_loading_scores = loading_scores.abs().sort_values(ascending=False)
- print(sorted_loading_scores)
- loadings = pd.Series(loading_scores)
- factors = pd.Series(df.columns)
- pd.concat([loadings, factors], axis=1)
- from sklearn.metrics import euclidean_distances
- from sklearn import manifold
- seed = np.random.RandomState(seed=3)
- similarities = euclidean_distances(df)
- mds = manifold.MDS(n_components=3, max_iter=3000, eps=1e-9,
- random_state=seed, dissimilarity="precomputed", n_jobs=1)
- pos = mds.fit(similarities).embedding_
- pos
- from matplotlib import pyplot
- from mpl_toolkits.mplot3d import Axes3D
- from numpy.random import rand
- from pylab import figure
- fig = figure()
- ax = Axes3D(fig)
- for i in range(len(pos)):
- ax.scatter(pos[i,0],pos[i,1],pos[i,2],color='b')
- ax.text(pos[i,0],pos[i,1],pos[i,2], countries[i], size=6, zorder=1, color='k')
- ax.set_xlabel('x')
- ax.set_ylabel('y')
- ax.set_zlabel('z')
- ax.set_title('MDS')
- pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement