-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataframe_initialization.py
More file actions
75 lines (58 loc) · 2.85 KB
/
Copy pathdataframe_initialization.py
File metadata and controls
75 lines (58 loc) · 2.85 KB
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
import pandas as pd
from ast import literal_eval
import numpy as np
def createMatrix(df, key_column_name, number_matrix_columns):
matrix = np.zeros((len(df), number_matrix_columns + 1), np.int8)
for i, row in enumerate(df[key_column_name]):
for value in row:
matrix[i, value] = 1
return matrix
def createKeyColumn(df, column_name):
# get all ingredients, create keys for them and store keys of all ingredients of one recipy in a new column
df[column_name] = df[column_name].apply(literal_eval)
all_values = list(set(df[column_name].explode().tolist())) # list(set([row for row in df]))
value_dictionary = {idx + 1: item for idx, item in enumerate(all_values)}
rev_value_dictionary = {item: key for key, item in value_dictionary.items()}
def _get_keys(values):
return [rev_value_dictionary[val] for val in values]
df[f"{column_name}_keys"] = df[column_name].apply(_get_keys)
number_values = len(all_values)
return df, all_values, value_dictionary, number_values
def createRecipyBase():
# load 1000 rows of csv (restricted due to demonstration purposes)
df = pd.read_csv('data\RAW_recipes.csv', nrows=1000)
# get all ingredients, create keys for them and store keys of all ingredients of one recipy in a new column
df, all_ingredients, ingredient_dictionary, ingredient_number = createKeyColumn(df, "ingredients")
# do the same for tags
df, all_tags, tag_dictionary, tag_number = createKeyColumn(df, "tags")
'''
print(df["ingredients_keys"].head())
print(ingredient_dictionary)
print(all_ingredients)
print(list(df.columns))
print(df.dtypes)
print(max(df.id))
print(min(df.id))
'''
return df, ingredient_dictionary, ingredient_number, tag_dictionary, tag_number
# creates matrices, returns dicitonaries and adds key numbers to df
def intitialize_recipies():
dataframe, ingredient_dictionary, number_ingredients, tag_dictionary, tag_number = createRecipyBase()
i_matrix = createMatrix(dataframe, "ingredients_keys", number_ingredients)
t_matrix = createMatrix(dataframe, "tags_keys", tag_number)
dataframe.to_csv("data\prepared_recipies.csv")
np.save("data/i_matrix.npy", i_matrix)
np.save("data/t_matrix.npy", t_matrix)
np.save("data/i_dict.npy", ingredient_dictionary)
np.save("data/t_dict.npy", tag_dictionary)
return dataframe, i_matrix, t_matrix, ingredient_dictionary, tag_dictionary
if __name__ == "__main__":
testnumber = 1
if testnumber == 1:
intitialize_recipies()
elif testnumber == 2:
createRecipyBase()
elif testnumber == 3:
dataframe, ingredient_dictionary, number_ingredients, tag_dictionary, tag_number = createRecipyBase()
i_matrix = createMatrix(dataframe, "ingredients_keys", number_ingredients)
t_matrix = createMatrix(dataframe, "tags_keys", tag_number)