def login_judge(request): log_util.log_util(request.method) print(type(request.session.get('user'))) try: if request.method == 'GET': return render(request, 'index.html') if request.method == 'POST': if isinstance(request.session.get('user'), int): return func(request) user = pass_check(request.POST) print(user) if user >= 0: request.session['user'] = user return func(request) else: request.session.clear() d = {'message': 'IDまたはパスが違います'} return render(request, 'index.html', d) # except RequestException as e: # request.session.clear() # d = {'except': e} # return render(request, 'fail.html', d) except: request.session.clear() d = {'except': '認証失敗'} return render(request, 'fail.html', d)
def customer_type_create(): log_util.log_util(sys._getframe().f_code.co_name) data = Data.objects.all().values('x', 'y') color = [] print(range(len(data))) for i in range(len(data)): color.append(type_check(data[i]['x'], data[i]['y'])) print(i, 'とは:', data[i]['x'], data[i]['y']) for n, key in enumerate(color): Data.objects.filter(id=n + 1).update(c_type=key)
def type_judge(string): log_util.log_util(sys._getframe().f_code.co_name) if string == 'e': return tk.E elif string == 'am': return tk.AM elif string == 'd': return tk.D elif string == 'a': return tk.A else: return tk.EX
def start_preparation(): # CUSTOMERテーブルへランダムなデータを生成 customer_export() # CUSTOMERテーブルデータを基にDATAテーブルデータ生成 data_input() # DATAテーブルデータを基に同テーブルのc_typeフィールドデータ生成 customer_type_create() # USERテーブルへ管理者レベルのユーザを生成 user_input_pre() log_util.log_util('##準備完了##')
def word_logic(data, pred): log_util.log_util(sys._getframe().f_code.co_name) xy = Data.objects.all().values('id', 'x', 'y') xy = pd.DataFrame(xy, columns=['id', 'x', 'y']) xy_data = Data.objects.all().filter(c_type=pred).values('id', 'x', 'y', 'c_type') df = pd.DataFrame(xy_data, columns=['id', 'x', 'y', 'c_type']) idx = 0 val = 30 for i in df['id'].values.astype(int): if abs(data[0] - xy['x'].values[i - 1]) + abs(data[1] - xy['y'].values[i - 1]) < val: val = abs(data[0] - xy['x'].values[i - 1]) + abs(data[1] - xy['y'].values[i - 1]) idx = i word = Data.objects.all().values('word').get(id=idx) return word
def type_check(x, y): log_util.log_util(sys._getframe().f_code.co_name) if (x <= 0) and (y >= 0): typ = 'e' elif (x >= 0) and (y >= 0): typ = 'am' elif (x <= 0) and (y <= 0): typ = 'd' elif (x >= 0) and (y <= 0): typ = 'a' else: typ = 'ex' return typ
def data_logic(data): x = 0 y = 0 x += 40 - int(data.age) y += (40 - int(data.age)) * 1.75 if data.sex == "男": x -= 10 y -= 10 else: x += 10 y += 10 if data.emo == "理性的": x += 3 y -= 7 else: x -= 3 y += 7 if data.od == "攻め": x -= 11 y += 7 else: x += 11 y -= 7 if data.intention == "上昇志向": x -= 7 y -= 4 else: x += 7 y += 4 if data.personality == "個性的": x += 6 y += 1 else: x -= 6 y -= 1 if data.act == "行動派": x -= 8 y += 1 else: x += 8 y -= 1 Data.objects.update_or_create( x=x, y=y, word=data.word ) log_util.log_util("データ化完了(ID:{})".format(data.id))
def form_data_logic(data): x = 0 y = 0 x += 40 - int(data['age']) y += (40 - int(data['age'])) * 1.75 if data['sex'] == "1": x -= 10 y -= 10 else: x += 10 y += 10 if data['emo'] == "1": x += 3 y -= 7 else: x -= 3 y += 7 if data['od'] == "1": x -= 11 y += 7 else: x += 11 y -= 7 if data['intention'] == "1": x -= 7 y -= 4 else: x += 7 y += 4 if data['personality'] == "1": x += 6 y += 1 else: x -= 6 y -= 1 if data['act'] == "1": x -= 8 y += 1 else: x += 8 y -= 1 log_util.log_util(sys._getframe().f_code.co_name + "データ化完了({},{})".format(x, y)) datalist = [x, y] return datalist
def knn_logic(data): log_util.log_util(sys._getframe().f_code.co_name) xy_data = Data.objects.all().values('x', 'y') xy_data_pat = Data.objects.all().values('c_type') df = pd.DataFrame(xy_data, columns=['x', 'y']) df_pat = pd.DataFrame(data=xy_data_pat, columns=['c_type']) knn = KNeighborsClassifier(n_neighbors=5) knn.fit(df, df_pat) prediction = knn.predict(np.array([data])) prediction = prediction[0] word = word_logic(data, prediction) word = '効果のあるキーワードは『{}』でしょう'.format(word['word']) predict_str = type_judge(prediction) customer_plt(data) return [predict_str, word]
def customer_plt(data): log_util.log_util(sys._getframe().f_code.co_name) xy_data = Data.objects.all().values('x', 'y') xy = pd.DataFrame(data=xy_data, columns=['x', 'y']) df = [data['c_type'] for data in Data.objects.values('c_type')] for i, d in enumerate(df): if d == 'e': plt.scatter(xy['x'].values[i], xy['y'].values[i], c='red') elif d == 'am': plt.scatter(xy['x'].values[i], xy['y'].values[i], c='blue') elif d == 'd': plt.scatter(xy['x'].values[i], xy['y'].values[i], c='green') elif d == 'a': plt.scatter(xy['x'].values[i], xy['y'].values[i], c='yellow') else: pass plt.scatter(data[0], data[1], s=100, marker='*', c='m') plt.savefig("static/img.png") plt.close()
def session_judge(request): log_util.log_util(request.method) print(request.session.get('user')) try: if request.method == 'GET': return render(request, 'index.html') if request.method == 'POST': if isinstance(request.session.get('user'), int): return func(request) elif request.session.get('user'): d = {'except': 'セッションエラー'} return render(request, 'fail.html', d) # except RequestException as e: # request.session.clear() # d = {'except': e} # return render(request, 'fail.html', d) except: request.session.clear() d = {'except': '認証失敗'} return render(request, 'fail.html', d)
def user_input_pre(): log_util.log_util(sys._getframe().f_code.co_name) User.objects.create(name='admin', password=pass_create('admin'), level=0)
def data_input(): log_util.log_util(sys._getframe().f_code.co_name) custom = Customer.objects.all() for c in custom: data_logic(c)
def customer_csv_input(): log_util.log_util(sys._getframe().f_code.co_name) with open(r'static\customerData.csv', encoding="utf-8") as fr: reader = csv.reader(fr) dataList = [row for row in reader] return dataList