-
Notifications
You must be signed in to change notification settings - Fork 0
/
approach1.py
92 lines (66 loc) · 2.8 KB
/
approach1.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
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LinearRegression
from keras_preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
tfidf_vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
def output_regression(cluster_train_path):
model = Sequential()
model.add(Dense(1, input_dim=X.shape[1]))
model.compile(optimizer='adam', loss='mean_squared_error')
df = pd.read_csv(cluster_train_path)
attr1 = df["TITLE_DES"]
attr2 = df["TITLE_BUL"]
Y = df["PRODUCT_LENGTH"]
attr1_tfidf = tfidf_vectorizer.fit_transform(attr1)
attr2_tfidf = tfidf_vectorizer.transform(attr2)
max_len = 128
# Extracting the vectors
attr1_vectors = attr1_tfidf.toarray()
attr2_vectors = attr2_tfidf.toarray()
if len(attr1_vectors) <= 128:
attr1_vectors = pad_sequences(attr1_vectors, maxlen=max_len, padding='post')
if len(attr2_vectors) <= 128:
attr2_vectors = pad_sequences(attr2_vectors, maxlen=max_len, padding='post')
attr1_vectors = np.array(attr1_vectors)
attr2_vectors = np.array(attr2_vectors)
X = np.column_stack((attr1_vectors, attr2_vectors))
model.fit(X, Y, epochs=10)
return model
li = []
y_submission = np.zeros(len(test_df.get_group(-1)))
result = pd.DataFrame(columns=["PRODUCT_ID", "PRODUCT_LENGTH"])
for group in test_df.groups:
if group == -1:
y_submission = np.zeros(len(test_df.get_group(-1)))
li.append(y_submission)
attr3 = test_df.get_group(-1)["PRODUCT_ID"]
else:
df = test_df.get_group(group)
attr1 = df["TITLE_DES"]
attr2 = df["TITLE_BUL"]
attr3 = df["PRODUCT_ID"]
attr1_tfidf = tfidf_vectorizer.transform(attr1)
attr2_tfidf = tfidf_vectorizer.transform(attr2)
max_len = 128
attr1_vectors = attr1_tfidf.toarray()
attr2_vectors = attr2_tfidf.toarray()
if len(attr1_vectors) > 128:
attr1_vectors = attr1_vectors[:128]
else:
attr1_vectors = pad_sequences(attr1_vectors, maxlen=max_len, padding='post')
if len(attr2_vectors) > 128:
attr2_vectors = attr2_vectors[:128]
else:
attr2_vectors = pad_sequences(attr2_vectors, maxlen=max_len, padding='post')
attr1_vectors = np.array(attr1_vectors)
attr2_vectors = np.array(attr2_vectors)
X_test = np.column_stack((attr1_vectors, attr2_vectors))
url = "train_" + str(group) + ".csv"
model = output_regression(url)
y_submission = model.predict(X_test)
li.append(y_submission)
new_df = pd.DataFrame({"PRODUCT_ID": attr3, "PRODUCT_LENGTH": y_submission})
result = pd.concat([result, new_df])