svm Case 2019.6.7 machine learning

1. Case: Decision Tree inherited the case data, with the same height and weight to define fat or thin. Following text file (7.SVM.txt), three columns each represent a height (m), weight (kg) and fat or thin (thin / fat).

2. Question: There are two people, one of whom height 1.6m, weight 30kg, another height 1.6m, weight 300kg, I ask each fat or thin it?

3. Data Documentation: 7.SVM.txt, as follows.

1.5 40 thin
1.5 50 fat
1.5 60 fat
1.6 40 thin
1.6 50 thin
1.6 60 fat
1.6 70 fat
1.7 50 thin
1.7 60 thin
1.7 70 fat
1.7 80 fat
1.8 60 thin
1.8 70 thin
1.8 80 fat
1.8 90 fat
1.9 80 thin
1.9 90 fat

4.Sampe code:

#coding: utf-8
import numpy as np
import scipy as sp
from sklearn import svm
from sklearn.cross_validation import train_test_split
import matplotlib.pyplot as plt

# ---- contain information on
the Data = []
Labels = []
with Open ( "7.svm.txt") AS ifile:
for Line in ifile:
tokens = line.strip () Split ( '').
The Data. append ([float (tk) for tk in tokens [: - 1]]) # height and weight data read
labels.append (tokens [-1]) # read fat or thin

# The data is put in array
X = np.array (Data)
Labels = np.array (Labels)
Y = np.zeros (labels.shape)

# Label is converted to 0/1, thin represents 0, representative of fat. 1
Y [Labels == 'FAT'] =. 1

# Training model, extracting feature
# Parameters: linear representatives are Linear Model
CLF = svm.SVC (Kernel = 'Linear')
clf.fit (X, Y)

---- # forecast and outputs the result
Print clf.predict ([[1.6, 30]])
Print clf.predict ([[1.6, 300]])

5. Results:
[0.05]

Source: CSDN
Original: https://blog.csdn.net/u011775523/article/details/52759452
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/weixin_43732462/article/details/91125743