-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvms_tutorial.py
More file actions
71 lines (47 loc) · 1.54 KB
/
svms_tutorial.py
File metadata and controls
71 lines (47 loc) · 1.54 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""SVMs_Tutorial.ipynb
Automatically generated by Colaboratory.
"""
from google.colab import files
uploaded = files.upload()
# Step 1: Load the data
import pandas as pd
df = pd.read_csv("diamonds.csv", index_col=0)
df.head()
# Step 2: Explore the data
df.describe()
df.info()
# Step 3: Exploratory Data Analysis and Data Wrangling
df['cut'].unique()
df['color'].unique()
df['clarity'].unique()
cut_dic = {"Fair": 1, "Good": 2, "Very Good": 3, "Premium": 4, "Ideal": 5}
color_dic = {"J": 1, "I": 2, "H": 3, "G": 4, "F": 5, "E": 6, "D": 7}
clarity_dic = {"I3": 1, "I2": 2, "I1": 3, "SI2": 4, "SI1": 5, "VS2": 6, "VS1": 7, "VVS2": 8, "VVS1": 9, "IF": 10, "FL": 11}
df['cut'] = df['cut'].map(cut_dic)
df['color'] = df['color'].map(color_dic)
df['clarity'] = df['clarity'].map(clarity_dic)
df.head()
df.info()
# Step 4: Train the model
import sklearn
from sklearn import svm, preprocessing
from sklearn.model_selection import train_test_split
X = df.drop('price', 1)
X = preprocessing.scale(X)
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(X ,y, test_size=0.3)
clf = svm.SVR(kernel='linear')
clf.fit(X_train, y_train)
confidence = clf.score(X_train, y_train)
print(confidence)
# Step 5: Prediction
for X,y in zip(X_test, y_test):
print(f"Model: {clf.predict([X])[0]}, Actual: {y}")
clf = svm.SVR(kernel='rbf')
clf.fit(X_train, y_train)
confidence = clf.score(X_train, y_train)
print(confidence)
# Step 5: Prediction
for X,y in zip(X_test, y_test):
print(f"Model: {clf.predict([X])[0]}, Actual: {y}")