-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample3.py
More file actions
51 lines (41 loc) · 1.76 KB
/
sample3.py
File metadata and controls
51 lines (41 loc) · 1.76 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
#To download the dataset: wget http://www-bcf.usc.edu/~gareth/ISL/Credit.csv
#In this case, there is a credit card data named Credit.csv, which contain the Income, Limit, Rating, and so on information of users, assuame that a new user has: Imcome: 70.00, Limit:5600, Rating: 420, Cards: 3, Age, 50, Education: 14, Gender: Female, Student: No, Married: Yes, Ethnicity: Caucasian. Make a prediction of her Balance, using SVM
import numpy as np
import cv2
import csv
#define a function to replace value
def replace(l, c, X, Y):
for i,v in enumerate(l[:, c]):
if v == X:
l[i, c] = Y
#read the data to an array
reader = csv.reader(file('/YourPath/Credit.csv', 'rb'), delimiter=',')
list = list(reader)
oldArray = np.array(list)
#replace'Yes' to '1', 'No' to '0', 'Female' to '0', ' Male' to '1', 'African American' to '0', 'Asian' to '1', 'Caucasian' to '2'
replace(oldArray, 7, 'Female', 0)
replace(oldArray, 7, ' Male', 1)
replace(oldArray, 8, 'No', 0)
replace(oldArray, 8, 'Yes', 1)
replace(oldArray, 9, 'No', 0)
replace(oldArray, 9, 'Yes', 1)
replace(oldArray, 10, 'African American', 0)
replace(oldArray, 10, 'Asian', 1)
replace(oldArray, 10, 'Caucasian', 2)
#reshape the array
oldArray = oldArray[1: oldArray.shape[0], 1: oldArray.shape[1]]
#set training data
trainData = oldArray[:, 0: oldArray.shape[1] - 1].astype(np.float32)
#set response data
response = oldArray[:, oldArray.shape[1] - 1].astype(np.float32)
response = np.array([response]).T
#set svm parameters
svm_params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC, C = 3, gamma = 6 )
svm = cv2.SVM()
#svm training
svm.train(trainData,response, params=svm_params)
#set newcomer
nwcmr = np.array([70, 5600, 420, 3, 50, 14, 0, 0, 1, 2]).astype(np.float32)
#make prediction
result = svm.predict(nwcmr)
print result