-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_SVC_machine_learning_and_testing_our_data_part_one.py
61 lines (39 loc) · 1.37 KB
/
Linear_SVC_machine_learning_and_testing_our_data_part_one.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
50
51
52
53
54
55
56
57
58
59
60
61
## Linear SVC machine learning and testing our data
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
import pandas as pd
from matplotlib import style
style.use("ggplot")
##defined a function to build our dataset
def Build_Data_Set(features = ["DE Ratio",
"Trailing P/E"]):
##loaded our data to the variable data_df
data_df = pd.DataFrame.from_csv("key_stats1.csv")
##extracted first 100 rows of the data
data_df = data_df[:100]
##we fill the X parameter with the NumPy array containing rows of features
X = np.array(data_df[features].values)
##we replace our status column with numerical data
y = (data_df["Status"]
.replace("underperform",0)
.replace("outperform",1)
.values.tolist())
##finally we return X and y
return X,y
##Analysing and visualising our data
def Analysis():
X, y = Build_Data_Set()
clf = svm.SVC(kernel="linear", C= 1.0)
clf.fit(X,y)
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(min(X[:, 0]), max(X[:, 0]))
yy = a * xx - clf.intercept_[0] / w[1]
h0 = plt.plot(xx,yy, "k-", label="non weighted")
plt.scatter(X[:, 0],X[:, 1],c=y)
plt.ylabel("Trailing P/E")
plt.xlabel("DE Ratio")
plt.legend()
plt.show()
Analysis()