-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
647 lines (535 loc) · 19.3 KB
/
process.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#encoding=utf8
import csv
import numpy as np
import time
import pickle
# 保存 str_id => int_id 映射关系
enterprise_id_mapping = {}
# 保存 int_id => str_id 映射关系
enterprise_str_mapping_predict = {}
# 保存 industry_code 'Char' => 'int' 映射关系
industry_code_mapping = {}
village_exceptions = ["幸福村","中关村","天使村","西柳村",'天马村','工业村']
developedCities = ["北京","上海","深圳",'广州']
productKeywords = []
def serialize(obj, filename):
with open(filename, 'wb') as f:
pickle.dump(obj, f)
def deSerialize(filename):
with open(filename , 'rb') as f:
obj = pickle.load(f)
return obj
# 加载csv文件,并返回数据 data, m 是数据行数
def loadCSVfilePredict(filename,encoding='gbk'):
data = []
label = []
m = 0
isfirst = 1
csv_file = csv.reader(open(filename,'r',encoding=encoding,errors="ignore"))
for line in csv_file:
if (isfirst == 1):
label.append(line)
isfirst = 0
else:
if (line[2] != '' ):
data.append(line)
m += 1
return [data, label, m]
# 加载csv文件,并返回数据 data, m 是数据行数
def loadCSVfile(filename,encoding='gbk'):
data = []
label = []
m = 0
isfirst = 1
csv_file = csv.reader(open(filename,'r',encoding=encoding,errors="ignore"))
for line in csv_file:
if (isfirst == 1):
label.append(line)
isfirst = 0
else:
if (line[3] != '' ):
data.append(line)
m += 1
return [data, label, m]
# 将data 保存到指定文件
def saveCSVfile(data,filename):
out = open(filename, 'w', newline='')
csv_write = csv.writer(out, dialect='excel')
for i in range(0,len(data)):
csv_write.writerow(data[i])
# 转置矩阵
def trans(m):
a = [[] for i in m[0]]
for i in m:
for j in range(len(i)):
a[j].append(i[j])
return a
# 增加 3 维度: 个人股东的数量, 普通企业股东数量,投资机构股东数量
def add_partner_info(data, filename):
[p_data,p_label,p_m] = loadCSVfile(filename,'gbk')
for i in range(0,len(data)):
data[i].append(0)
data[i].append(0)
data[i].append(0)
for d in p_data:
enterprise_id = d[0]
partner_type = d[2]
try:
int_id = enterprise_id_mapping[enterprise_id]
if partner_type == '1':
data[int_id][-3] += 1
elif partner_type == '2':
data[int_id][-2] += 1
elif partner_type == '3':
data[int_id][-1] += 1
except:
continue
# 增加 3 维度: 个人股东的数量, 普通企业股东数量,投资机构股东数量
def add_question_partner_info(data, filename):
[p_data,p_label,p_m] = loadCSVfile(filename,'gbk')
for i in range(0,len(data)):
data[i].append(0)
data[i].append(0)
data[i].append(0)
for d in p_data:
enterprise_id = d[0]
partner_type = d[2]
try:
int_id = enterprise_id_mapping[enterprise_id]
if partner_type == '1':
data[int_id][-3] += 1
elif partner_type == '2':
data[int_id][-2] += 1
elif partner_type == '3':
data[int_id][-1] += 1
except:
continue
# 增加 3 维度: 专利的数量
def add_patent_info(data, filename):
[p_data, p_label, p_m] = loadCSVfile(filename,'gbk')
for i in range(0, len(data)):
# 每一行增加3 个维度: 不同种类的专利数量
data[i].append(0) # -3
data[i].append(0) # -2
data[i].append(0) # -1
for d in p_data:
enterprise_id = d[0]
patent_type = d[3]
try:
int_id = enterprise_id_mapping[enterprise_id]
if patent_type == '1':
data[int_id][-3] += 1
elif patent_type == '2':
data[int_id][-2] += 1
elif patent_type == '3':
data[int_id][-1] += 1
except:
continue
# 增加 3 维度: 专利的数量
def add_question_patent_info(data, filename):
add_patent_info(data, filename)
# 增加 4 维度: 历史的各类投资的数量
def add_invest_info(data, filename):
[p_data, p_label, p_m] = loadCSVfile(filename,'gbk')
for i in range(0, len(data)):
# 每一行增加3 个维度: 不同种类的投资数量
data[i].append(0) # -4
data[i].append(0) # -3
data[i].append(0) # -2
data[i].append(0) # -1
for d in p_data:
enterprise_id = d[0]
invest_level = d[4]
try:
int_id = enterprise_id_mapping[enterprise_id]
invest_time = d[2]
isValidInvestment = getTimeStampIntervalFromAToB(invest_time, '2010/6/30')
if isValidInvestment > 0:
if invest_level == '1':
data[int_id][-4] += 1
elif invest_level == '2':
data[int_id][-3] += 1
elif invest_level == '3':
data[int_id][-2] += 1
elif invest_level == '4':
data[int_id][-1] += 1
except:
continue
# 增加 4 维度: 历史的各类投资的数量
def add_question_invest_info(data, filename):
[p_data, p_label, p_m] = loadCSVfile(filename)
for i in range(0, len(data)):
# 每一行增加3 个维度: 不同种类的投资数量
data[i].append(0) # -4
data[i].append(0) # -3
data[i].append(0) # -2
data[i].append(0) # -1
for d in p_data:
enterprise_id = d[0]
invest_level = d[4]
try:
int_id = enterprise_id_mapping[enterprise_id]
invest_time = d[2]
isValidInvestment = getTimeStampIntervalFromAToB(invest_time, '2010/6/30')
if isValidInvestment > 0:
if invest_level == '1':
data[int_id][-4] += 1
elif invest_level == '2':
data[int_id][-3] += 1
elif invest_level == '3':
data[int_id][-2] += 1
elif invest_level == '4':
data[int_id][-1] += 1
except:
continue
# 增加 6 维度: 历史的各类判决的数量
def add_judgement_info(data, filename):
p_data = []
m = 0
isfirst = 1
csv_file = csv.reader(open(filename, 'r', encoding='gbk',errors="ignore"))
for line in csv_file:
if (isfirst == 1):
isfirst = 0
else:
p_data.append(line)
m += 1
for i in range(0, len(data)):
# 每一行增加3 个维度: 不同种类的投资数量
data[i].append(0) # -6
data[i].append(0) # -5
data[i].append(0) # -4
data[i].append(0) # -3
data[i].append(0) # -2
data[i].append(0) # -1
for d in p_data:
enterprise_id = d[0]
judgement_type = d[3]
try:
int_id = enterprise_id_mapping[enterprise_id]
if judgement_type == '1':
data[int_id][-6] += 1
elif judgement_type == '2':
data[int_id][-5] += 1
elif judgement_type == '3':
data[int_id][-4] += 1
elif judgement_type == '4':
data[int_id][-3] += 1
elif judgement_type == '5':
data[int_id][-2] += 1
elif judgement_type == '6':
data[int_id][-1] += 1
except:
continue
# 增加 6 维度: 历史的各类判决的数量
def add_question_judgement_info(data, filename):
p_data = []
m = 0
isfirst = 1
csv_file = csv.reader(open(filename, 'r', encoding='gbk',errors="ignore"))
for line in csv_file:
if (isfirst == 1):
isfirst = 0
else:
p_data.append(line)
m += 1
for i in range(0, len(data)):
# 每一行增加3 个维度: 不同种类的投资数量
data[i].append(0) # -6
data[i].append(0) # -5
data[i].append(0) # -4
data[i].append(0) # -3
data[i].append(0) # -2
data[i].append(0) # -1
for d in p_data:
enterprise_id = d[0]
judgement_type = d[3]
try:
int_id = enterprise_id_mapping[enterprise_id]
if judgement_type == '1':
data[int_id][-6] += 1
elif judgement_type == '2':
data[int_id][-5] += 1
elif judgement_type == '3':
data[int_id][-4] += 1
elif judgement_type == '4':
data[int_id][-3] += 1
elif judgement_type == '5':
data[int_id][-2] += 1
elif judgement_type == '6':
data[int_id][-1] += 1
except:
continue
# 考虑关键字的影响,权重 1 维度
def getKeywordWeight(product):
keywords = {"共享": 0.6122, "人工智能": 0.9285, "机器人": 0.8219, "分布式": 1.0, "区块链": 1.0, "互联网": 0.5792, "系统集成": 0.4,
"跨境": 0.6875, "智能制造": 0.6667, "教育": 0.5344, "新能源汽车": 0.9091, "VR": 0.8620, "养老": 0.8571}
weight = 0.5
for (k, v) in keywords.items():
if k in product:
if v > weight:
weight = v
return weight
# 将原始数据在索引数组 位置的数据 转换成处理后的数据
def transform_data(primitive_data, indexes):
data = []
for i in indexes:
# 将类型由str转换成float
t = primitive_data[:, i]
t = [float(x) for x in t]
# 把若干个列表合成一个大二维列表
data.append(t)
# 转置矩阵
data = trans(data)
data = np.array(data)
return data
# 获得时间 B - A 的时间戳之差, 再 scaling 处理
def getTimeStampIntervalFromAToB(start, end='2016/6/30'):
scale = 10000
timeStart = time.strptime(start, "%Y/%m/%d")
timestampStart = int(time.mktime(timeStart))
timeEnd = time.strptime(end, "%Y/%m/%d")
timestampEnd = int(time.mktime(timeEnd))
return (timestampEnd - timestampStart) / scale
# 获得索引数组,规则: 选中的chooseArr 索引 + 某位置开始剩下所有的索引
def get_index_array(len, chooseArr, the_rest_index):
indexes = []
indexes += chooseArr
for i in range(the_rest_index,len):
indexes.append(i)
return indexes
def processTrainingData():
# 增加企业信息,及初始化
industry_code_mapping = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11,
'L': 12,'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'null': 20}
[data,label, m] = loadCSVfile('data/train/enterprise.csv','gbk')
per_line = ['tag1','tag2','tag3','tag4']
label[0] += per_line
col = 0
# 添加有用数据到 data 中
for i in range(0,m):
per_line = [0,0,0,0]
# 增加tag 4 维度
tag = data[i][5]
tag = tag.split(",")
for j in range(0,len(tag)):
if (tag[j] != ''):
# print('train')
# print(tag)
per_line[int(tag[j]) - 1] = 1
# 增加企业的注册时间维度
registered_time = data[i][2]
timestamp = getTimeStampIntervalFromAToB(registered_time)
per_line.append(timestamp)
# 增加企业代码维度 20 维度
for ins in range(0, 20):
per_line.append(0)
industry_code = data[i][7]
try:
industry_index = industry_code_mapping[industry_code] + 3
per_line[industry_index] = 1
except:
# 如'' , 'NULL' 等异常情况
per_line[-1] = 1
# 增加关键字权重 1 维度
# per_line.append(0)
# keyword = data[i][6]
# weight = getKeywordWeight(keyword)
# per_line[-1] = weight
# 增加产品有无介绍 1 维度
# per_line.append(0)
# productDesc = data[i][6]
# if productDesc!='':
# per_line[-1] += 1
# 增加 地区, 市区/县级/村级
per_line.append(0)
per_line.append(0)
per_line.append(0)
address = data[i][4]
if "村" in address and isExistsInArray(address,village_exceptions) == 0 and isExistsInArray(address,developedCities) == 0:
# print(address)
per_line[-1] += 1
elif "县" in address and isExistsInArray(address,village_exceptions) == 0 and isExistsInArray(address,developedCities) == 0:
per_line[-2] += 1
elif "市" in address:
per_line[-3] += 1
# if "村" in address and isExistsInArray(address,village_exceptions) == 0:
# print(address)
# per_line[-1] += 1
# elif "县" in address:
# per_line[-2] += 1
# elif "市" in address:
# per_line[-3] += 1
# 增加企业员工 1 维度
# employees_num = data[i][8]
# if employees_num == '':
# employees_num = 0
# per_line.append(employees_num)
# 增加企业员工 2 维度
per_line.append(0)
per_line.append(0)
employees_num = data[i][8]
# print(employees_num)
if employees_num == '':
employees_num = 0
else:
per_line[-1] += np.float64(employees_num)
per_line[-2] += 1
# 添加到每一行的数据项中
data[i] += per_line
enterprise_id_mapping[data[i][0]] = col
data[i][0] = col
col += 1
# 使用partner 表信息
add_partner_info(data, 'data/train/partner.csv')
# 使用patent 表信息
add_patent_info(data, 'data/train/patent.csv')
# 使用invest 表信息
add_invest_info(data, 'data/train/invest.csv')
# 使用judgement 表信息
add_judgement_info(data, 'data/train/judgement.csv')
m = np.array(data) # 转换为numpy.array
# 把所需要的信息列举出来
indexes = get_index_array(len(m[0]),[0,1,3,9],10)
print(indexes)
# 并将类型由str转换成float
data = transform_data(m, indexes)
# 打印列的数量,以第一行为例
print(len(data[0,:]))
print(data[0,:])
# 保存有用的信息
saveCSVfile(data, "all.csv")
def isExistsInArray(element,arr):
for i in range(0,len(arr)):
if arr[i] in element:
return 1
return 0
def processPredictData():
# 增加企业信息,及初始化
enterprise_id_mapping.clear()
industry_code_mapping = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11,
'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'null': 20}
[data, label, m] = loadCSVfilePredict('data/question/enterprise.csv','utf8')
per_line = ['tag1', 'tag2', 'tag3', 'tag4']
label[0] += per_line
col = 0
# 添加有用数据到 data 中
for i in range(0, m):
per_line = [0, 0, 0, 0]
# 增加tag 4 维度
tag = data[i][4]
tag = tag.split(",")
for j in range(0, len(tag)):
if (tag[j] != ''):
# print('test')
# print(tag)
per_line[int(tag[j]) - 1] = 1
# 增加企业的注册时间 1 维度
registered_time = data[i][1]
timestamp = getTimeStampIntervalFromAToB(registered_time)
per_line.append(timestamp)
# 增加企业代码维度 20 维度
for ins in range(0, 20):
per_line.append(0)
industry_code = data[i][6]
try:
industry_index = industry_code_mapping[industry_code] + 3
per_line[industry_index] = 1
except:
# 如'' , 'NULL' 等异常情况
per_line[-1] = 1
# 增加关键字权重 1 维度
# per_line.append(0)
# keyword = data[i][6]
# weight = getKeywordWeight(keyword)
# per_line[-1] = weight
# 增加产品有无介绍 1 维度
# per_line.append(0)
# productDesc = data[i][5]
# if productDesc != '':
# per_line[-1] += 1
# productDesc = data[i][5]
#
# for i in range(0,len(productKeywords)):
# if productKeywords[i] in productDesc:
# per_line.append(1)
# else:
# per_line.append(0)
# 增加 地区, 市区/县级/村级
per_line.append(0)
per_line.append(0)
per_line.append(0)
address = data[i][3]
if "村" in address and isExistsInArray(address,village_exceptions) == 0 and isExistsInArray(address,developedCities) == 0:
# print(address)
per_line[-1] += 1
elif "县" in address and isExistsInArray(address,village_exceptions) == 0 and isExistsInArray(address,developedCities) == 0:
per_line[-2] += 1
elif "市" in address:
per_line[-3] += 1
# if "村" in address and isExistsInArray(address,village_exceptions) == 0:
# per_line[-1] += 1
# elif "县" in address:
# per_line[-2] += 1
# elif "市" in address:
# per_line[-3] += 1
# 增加企业员工 2 维度
per_line.append(0)
per_line.append(0)
employees_num = data[i][7]
if employees_num == '':
employees_num = 0
else:
per_line[-1] += np.float64(employees_num)
per_line[-2] += 1
# 添加到每一行的数据项中
data[i] += per_line
enterprise_id_mapping[data[i][0]] = col
enterprise_str_mapping_predict[col] = data[i][0]
data[i][0] = col
col += 1
# 使用partner 表信息
add_question_partner_info(data, 'data/question/partner.csv')
# 使用patent 表信息
add_question_patent_info(data, 'data/question/patent.csv')
# 使用invest 表信息
add_question_invest_info(data, 'data/question/invest.csv')
# 使用judgement 表信息
add_question_judgement_info(data, 'data/question/judgement.csv')
m = np.array(data) # 转换为numpy.array
# 把所需要的信息列举出来
indexes = get_index_array(len(m[0]), [0, 2, 8], 9)
print(indexes)
print(data[0])
# 并将类型由str转换成float
data = transform_data(m, indexes)
# 打印列的数量,以第一行为例
print(len(data[0, :]))
print(data[2, :])
# 保存有用的信息
saveCSVfile(data, "all_question.csv")
serialize(enterprise_id_mapping, "id_mapping.binary")
def getAllProducts():
[data, label, m] = loadCSVfile('data/train/enterprise.csv','gbk')
products = []
out = open('products.txt', 'w')
for i in range(0, m):
product = data[i][6]
products.append(product)
out.write(products[i] + '\n')
def processProductKeywords():
f = open('keywords.txt', 'r',encoding='gbk')
keyword = f.readline()
while keyword != '':
keyword = keyword.split(' ')[0]
productKeywords.append(keyword)
keyword = f.readline()
f.close()
print(productKeywords)
if __name__ == '__main__':
#getAllProducts()
#processProductKeywords()
# 处理 training data 成 all.csv
processTrainingData()
# 处理 question data 成 all_question.csv + id_mapping.binary
processPredictData()