-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef_user_habit_learn.py
285 lines (252 loc) · 8.1 KB
/
def_user_habit_learn.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 6 10:21:42 2020
@author: Simon
"""
from typing import List
import numpy as np
class UserHabitLearn:
"""
This class is a user habit learning class.
This class use essential machine learning to learn user habit.
It is also an API that helps data science people to sort and clean the data.
It even able to train the Vision class CNN network by its self.
"""
database = {}
def __init__(self, name: str) ->None:
"""
Initialize userhabitlearn class
arg:
name: name of the data for this time
x_contents:The x value read out from data base
y_contents:The y value read out from data base
database: This is a dictionary that storge data from the database
Returns:
None
"""
self.name = name
with open("x_data(for_training).txt", "r") as f:
#self.x_contents = f.read()
database = {"x":list(map(int, f.read().split(",")))}
#print(database)
with open("y_data(for_training).txt", "r") as f:
#self.y_contents = f.read()
database["y"] = f.read().split(",")
self.database = database
print(self.database)
def bubble_sort(numbers:List[int]) -> List[int]:
"""
this is a sort function that use bubbles sort structure
it sort a list of numbers and return a sorted list
Returns:
numbers:List[int]
"""
for i,num in enumerate(numbers):
try:
if numbers[i+1] <num:
numbers[i] = numbers[i+1]
numbers[i+1] = num
bubble_sort(numbers)
except IndexError:
pass
return numbers
def sort(self)->None:
"""
sort data autolly
arg:
x_contents:The x value read out from data base
y_contents:The y value read out from data base
database: This is a dictionary that storge data from the database
Returns:
None
"""
print(type(self))
x_value_list = self.database['x']
self.database.pop("x")
y_value_list = self.database["y"]
self.database.pop("y")
sorted_x = UserHabitLearn.bubble_sort(x_value_list)
sorted_y = UserHabitLearn.bubble_sort(y_value_list)
self.database = {"x":sorted_x}
self.database["y"] = sorted_y
def find_x_mode(self) -> int:
"""
findy mode number for y numbers
arg:
database: This is minimum dictionary that storge data from the database
x_value_list:The x value read out from data base
maximum: the maximum number in x value
minimum: the minimum number in x value
x_mode: a int that is the mode of x
Returns:
x_mode = mode of x (int)
"""
print(self.database)
x_value_list = self.database['x']
maximum = x_value_list.index(min(x_value_list))
minimum = x_value_list.index(max(x_value_list))
x_value_list.pop(maximum-1)
x_value_list.pop(minimum-1)
counts = np.bincount(x_value_list)
#返回众数
x_mode = np.argmax(counts)
return x_mode
def find_y_mode(self) -> int:
"""
findy mode number for y numbers
arg:
database: This is minimum dictionary that storge data from the database
y_value_list:The y value read out from data base
maximum: the maximum number in y value
minimum: the minimum number in y value
x_mode: a int that is the mode of y
Returns:
y_mode = mode of y (int)
"""
y_value_list = self.database["y"]
maximum = y_value_list.index(min(y_value_list))
minimum = y_value_list.index(max(y_value_list))
y_value_list.pop(maximum-1)
y_value_list.pop(minimum-1)
counts = np.bincount(y_value_list)
#返回众数
y_mode = np.argmax(counts)
return y_mode
class Calc:
"""
this is a math API that ready for use for data science learning
This class is encapsulated
set_a_b(): is the setter for to number that user want to process
"""
# 初始化
def __init__(self, a, b)->None:
"""
Initialize Calc class
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
None
"""
self.a = a
self.b = b
# 重置值
def set_a_b(self, a, b)->None:
"""
set a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
None
"""
self.a = a
self.b = b
# 加法
def __add(self) -> int:
"""
this is a add function that use for add a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
sum of a+b
"""
return self.a + self.b
# 减法
def __sub(self) -> int:
"""
this is a subtrac function that use for subtrac a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
subtrac of a+b
"""
return self.a - self.b
# 乘法
def _mul(self) -> int:
"""
this is a multiplication function that use for multipliy a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
multiplication of a+b
"""
return self.a * self.b
# 除法
def _div(self) -> int:
"""
this is a div function that use for divid a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
divid of a+b
"""
# a / b 2和3版本的除法有稍许变化
if self.b != 0:
return self.a // self.b
else:
raise ('除数为0,无法计算!')
# 加法
def get_adds(self) -> int:
"""
this is a getter function that get add a and b
arg:
a: a number use for peocess
b: aother number use for peoces
Returns:
sum of a+b
"""
return self.__add()
# 减法
def get_subs(self) -> int:
"""
this is a getter function that get subs a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
subtrac of a+b
"""
return self.__sub()
def get_muls(self) -> int:
"""
this is a getter function that get muls a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
multiplication of a+b
"""
return self._mul()
def get_divs(self) -> int:
"""
this is a getter function that get divs a and b
arg:
a: a number use for peocess
b: aother number use for peocess
Returns:
divid of a+b:
"""
return self._div()
"""
math API package
Feel free to use if you needed
01/15/2020 Simon Li
Here is how you use it
eg = Calc(2, 6)
print(eg.get_adds())
print(eg.get_subs())
eg.set_a_b(9, 6)
print(eg.get_divs())
print(eg.get_muls())
user_habit_learn.ellipse_detect()
"""
if __name__ == "__main__":
user_habit_learn = UserHabitLearn("你妈逼的")
user_habit_learn.sort()
print(user_habit_learn.find_x_mode())
print(user_habit_learn.find_y_mode())