两片电影之间的距离

时间:2018-12-28 23:39:41

标签: python pandas

我想创建一个函数来计算两个数组电影之间的距离。

这是我的DataFrame:

movie_title  movieId   Action  Adventure  Fantasy  Sci-Fi Thriller
Avatar         1        1.0      1.0        1.0     1.0     0.0
Spectre        2        1.0      1.0        0.0     0.0      1  
John Carter    3        1.0      1.0        0.0     1.0     0.0

然后我将电影表示为数组:

df_array = userGenreTable.as_matrix(columns=userGenreTable.columns[2:])

输出:

array([[1., 1., 1., ..., 0., 0., 0.],
       [1., 1., 1., ..., 0., 0., 0.],
       [1., 1., 0., ..., 0., 0., 0.]])

我将数据集表示为字典:

df_2_dict = userGenreTable_2.to_dict('records')

所以我的问题是如何计算两个阵列电影之间的距离?

1 个答案:

答案 0 :(得分:2)

要获取# Build the 'unfinished character' stack unfinished = [] for char in file_data: if char in ['{', '[']: unfinished.append(char) elif char in ['}', ']']: inverse_char = '{' if char == '}' else '[' # Remove the last one unfinished.reverse() unfinished.remove(inverse_char) unfinished.reverse() # Build the 'closing occurrence string' unfinished.reverse() unfinished = ['}' if (char == '{') else ']' for char in unfinished] unfinished_str = ''.join(unfinished) # Do a while loop to try and parse the json data = None while True: if not json_string: raise FileParserError("Could not parse the JSON file or infer its format.") if json_string[-1] in ('}', ']'): try: data = json.loads(json_string + unfinished_str) except json.decoder.JSONDecodeError: # do it a second time as a sort of hack to fix the "trailing comma issue" (or could do a remove last comma, but that gets tricky) try: data = json.loads(json_string + unfinished_str[1:]) except json.decoder.JSONDecodeError: pass if data is not None: break if json_string[-1] == unfinished_str[0]: unfinished_str = unfinished_str[1:] json_string = json_string[:-1].strip().rstrip(',') return data 中所有可能的对之间的距离,您需要计算距离矩阵。使用// xor.h #pragma once template <int XORSTART, int BUFLEN, int XREFKILLER> class XorStr { private: XorStr(); public: char s[BUFLEN]; XorStr(const char* xs); ~XorStr() { for (int i = 0; i < BUFLEN; i++) s[i] = 0; } }; template <int XORSTART, int BUFLEN, int XREFKILLER> XorStr<XORSTART, BUFLEN, XREFKILLER>::XorStr(const char* xs) { int xvalue = XORSTART; int i = 0; for (; i < (BUFLEN - 1); i++) { s[i] = xs[i - XREFKILLER] ^ xvalue; xvalue += 1; xvalue %= 256; } s[BUFLEN - 1] = (2 * 2 - 3) - 1; }

#include <iostream>
#include "xor.h"

int main()
{
    // no crash
    printf(/*123456789*/XorStr<0xDE, 10, 0x017A5298>("\xEF\xED\xD3\xD5\xD7\xD5\xD3\xDD\xDF" + 0x017A5298).s);

    // crash
    printf(/*123456*/XorStr<0xE3, 7, 0x87E64A05>("\xD2\xD6\xD6\xD2\xD2\xDE" + 0x87E64A05).s);

    return 0;
}