-
Notifications
You must be signed in to change notification settings - Fork 101
/
降維-01.py
49 lines (35 loc) · 1.18 KB
/
降維-01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
from sklearn import manifold
import pandas as pd
import matplotlib.pyplot as plt
#----------------------------------------------------
# 讀入資料, pandas.DataFrame
df=pd.read_csv('iris.csv',
sep=',',
names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'speices'])
df_feature=df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
df_label=df[['speices']]
#----------------------------------------------------
mds=manifold.MDS(n_components=2)
Xtrans=mds.fit_transform(df_feature, df_label)
#----------------------------------------------------
# 設定字型及大小
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['font.size'] = 14
# 設定圖標題
plt.title('IRIS dataset')
# 設定x軸及y軸標題
plt.xlabel('X1')
plt.ylabel('X2')
# 資料表內的grid
plt.grid(True)
# 設定x軸及y軸的尺規範圍
plt.axis([-5, 5, -5, 5])
# 繪製資料
plt.plot(Xtrans[:50, 0], Xtrans[:50, 1], 'ro')
plt.plot(Xtrans[50:100, 0], Xtrans[50:100, 1], 'bd')
plt.plot(Xtrans[100:, 0], Xtrans[100:, 1], 'g^')
# 設定資料說明
plt.legend(['setosa', 'versicolor', 'virginica'], numpoints=1, loc='upper left')
# 顯示圖表
plt.show()