-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadratic.py
143 lines (93 loc) · 3.07 KB
/
Quadratic.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from random import randint
import re
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from termcolor import colored
from math import *
def quadratic_eq():
'''
quadratics equations
'''
a = randint(-10,10)
b = randint(-10,10)
c = randint(-10,10)
return f"{a}x^2 + {b}x + {c} = 0"
def non_quadratic_eq():
'''
non-quadratics equations
'''
a = randint(-10,10)
b = randint(-10,10)
c = randint(-10,10)
d = randint(-10,10)
return f"{a}x^3 + {b}x^2 + {c}x + {d} = 0"
# Generate 100 quadratics and non-quadratics equations
quadratic_equations = [quadratic_eq() for _ in range(100)]
non_quadratic_equations = [non_quadratic_eq() for _ in range(100)]
def extract_features(equation):
'''
take coefficient from the equation
'''
patt = re.match(r"(-?\d+)x\^2\s*\+\s*(-?\d+)x\s*\+\s*(-?\d+)\s*=\s*0", equation) # use regular espression to
if patt:
return [int(patt.group(1)), int(patt.group(2)), int(patt.group(3))] # give all the coeficient as a list
return None
# Convert the quadratic and non-quadratic equations into feature vectors (from regular expression)
X = []
y = []
for equation in quadratic_equations:
features = extract_features(equation)
if features:
X.append(features)
y.append(1)
for equation in non_quadratic_equations:
features = extract_features(equation)
if features:
X.append(features)
y.append(0)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a decision tree classifier and train it on the training set
modl = DecisionTreeClassifier(random_state=42)
modl.fit(X_train, y_train)
# Make predictions on the testing set and calculate accuracy
y_pred = modl.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
def accuracy_color():
if accuracy >= 1.0:
return colored(accuracy, 'green')
else:
return colored(accuracy,'red')
def is_quadratic_eq(eq, model):
features = extract_features(eq)
if features:
pred = model.predict([features])[0]
if pred == 1:
return colored(True,'green')
else:
return colored(False,'red')
else:
return colored(False,'red')
def CalcRacine(a,b,c):
delta = (b**2)-4*a*c
if delta >0:
racine1=(-(b)-sqrt(delta))/(2*a)
racine2=(-(b)+sqrt(delta))/(2*a)
return racine1, racine2, "sont solution de l'equation"
if delta==0:
return (-(b))/(2*a) , "est la solution à l'équation"
if delta<0:
return "l'equation n'admet pas de solution"
def extract_features(equation):
a , b , c = is_quadratic_eq(equation)
if a != 0 :
return (a , b , c)
else:
return None
# Train logistic regression (scikit-learn)
model = LogisticRegression()
model.fit(X, y)
print(accuracy_color())
eq = "x^2 + 5x + 2 = 0"
is_quadratic = is_quadratic_eq(eq , modl)