🔠分類範例¶
SVM使用範例¶
In [ ]:
Copied!
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
載入iris資料集¶
In [ ]:
Copied!
from sklearn.datasets import load_iris
from sklearn.datasets import load_iris
In [ ]:
Copied!
iris = load_iris()
iris = load_iris()
In [ ]:
Copied!
iris.feature_names
iris.feature_names
In [ ]:
Copied!
iris.target_names
iris.target_names
In [ ]:
Copied!
X = iris.data
Y = iris.target
X = iris.data
Y = iris.target
In [ ]:
Copied!
len(X)
len(X)
In [ ]:
Copied!
X[0]
X[0]
In [ ]:
Copied!
Y
Y
產生訓練集跟測試集¶
In [ ]:
Copied!
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split
In [ ]:
Copied!
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
使用SVM來做分類¶
In [ ]:
Copied!
#1. 載入模型
from sklearn.svm import SVC
#1. 載入模型
from sklearn.svm import SVC
In [ ]:
Copied!
#2. 建立模型
clf = SVC()
#2. 建立模型
clf = SVC()
In [ ]:
Copied!
#3. 訓練模型
clf.fit(x_train, y_train)
#3. 訓練模型
clf.fit(x_train, y_train)
In [ ]:
Copied!
#4. 使用模型來做預測
y_predict = clf.predict(x_test)
#4. 使用模型來做預測
y_predict = clf.predict(x_test)
In [ ]:
Copied!
y_predict
y_predict
In [ ]:
Copied!
# 計算模型的Accuracy
clf.score(x_test, y_test)
# 計算模型的Accuracy
clf.score(x_test, y_test)