机器学习——tsne降维处理

不念不忘少年蓝@ 2022-12-26 04:44 388阅读 0赞
  1. import warnings
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import seaborn as sns
  6. #依赖
  7. from sklearn.manifold import TSNE
  8. from sklearn.preprocessing import StandardScaler
  9. sns.set()
  10. warnings.filterwarnings('ignore')
  11. df = pd.read_csv(
  12. 'https://labfile.oss.aliyuncs.com/courses/1283/telecom_churn.csv')
  13. df.head()
  14. X = df.drop(['Churn','State'],axis=1)
  15. #转化为数值
  16. X['International plan'] = X['International plan'].map({ 'Yes':1,'No':0})
  17. X['Voice mail plan'] = X['Voice mail plan'].map({ 'Yes':1,'No':0})
  18. #StandardScaler()方法归一化数据
  19. scaler=StandardScaler()
  20. X_scaled = scaler.fit_transform(X)
  21. #构建t-sine
  22. tsne = TSNE(random_state=17)
  23. tsne_repr = tsne.fit_transform(X_scaled)
  24. #绘图
  25. #plt.scatter(tsne_repr[:,0],tsne_repr[:,1],alpha=.5)
  26. #绘图加上色
  27. #plt.scatter(tsne_repr[:,0],tsne_repr[:,1],c=df['Churn'].map({False:'blue',True:'orange'}),alpha=.5)
  28. _, axes = plt.subplots(1, 2, sharey=True, figsize=(12, 5))
  29. for i, name in enumerate(['International plan', 'Voice mail plan']):
  30. axes[i].scatter(tsne_repr[:, 0], tsne_repr[:, 1],
  31. c=df[name].map({ 'Yes': 'orange', 'No': 'blue'}), alpha=.5)
  32. axes[i].set_title(name)

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,388人围观)

还没有评论,来说两句吧...

相关阅读

    相关 机器学习-PCA

    PCA(Principal Component Analysis 主成分分析) PCA主要用于非线性数据的降维,需要用到核技巧。因此在使用的时候需要选择合适的核函数并对核函