在数据帧Python中不是随机选择单词

2024-10-02 08:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个stroop测试。stroop测试是一种认知测试,给一个单词一种颜色,但是这个单词的墨水每次都在变化。 例如,我们可以用墨水红色表示BLEU,在这种情况下,它是不一致的,因为BLEU(蓝色)与墨水不同。你知道吗

参与者需要说出墨水的颜色,而不是单词。所以,如果写的字和墨水不一样,就很难回答了。你知道吗

每次有新的参与者进来,我都需要一个新的单词/墨水序列。你知道吗

因此,我创建了以下代码:

import pandas as pd
import numpy as np
import random

word = ['bleu', 'rouge', 'vert', 'jaune']

ink = ['blue', 'red', 'green', 'yellow']

my_list_word = [random.choice(word) for _ in range(60)]

df = pd.DataFrame(my_list_word, columns=["word"])

my_list_ink = [random.choice(ink) for _ in range(60)]

df['ink'] = my_list_ink

df['response'] = df['ink']

df['response'] = df['response'].replace(['red'], 'rouge.jpg')
df['response'] = df['response'].replace(['yellow'], 'jaune.jpg')
df['response'] = df['response'].replace(['blue'], 'bleu.jpg')
df['response'] = df['response'].replace(['green'], 'vert.jpg')

di = dict(zip(word, ink))
df['congruent'] = (df['word'].map(di) == df['ink']).astype(int)

print df.head(5)

df(头部):

    word     ink   response  congruent
0   vert    blue   bleu.jpg          0
1   bleu    blue   bleu.jpg          1
2  rouge    blue   bleu.jpg          0
3  jaune     red  rouge.jpg          0
4   vert  yellow  jaune.jpg          0

我的问题:我需要50%的时间来回答一个一致的问题。你有办法做到这一点吗?我不明白如何为一个条件链接两列。也许,我可以翻译法语单词,所以它会是同一个单词,更容易比较。最后,我把它翻译成我的实验?你知道吗

谢谢你的帮助!你知道吗


Tags: importdfresponsemyblue单词replacelist
1条回答
网友
1楼 · 发布于 2024-10-02 08:31:07

下面的示例展示了一种使用^{}(python3.6)的可能方法;它使用ANSI colors来说明输出;它在macintosh&linux上的工作方式是一样的,但是您可能需要一个库(colorama?)在窗户上。你知道吗

import random

def get_color(color):
    """return a congruent color 50% of the time, a random color 
       taken from the rest of the colors otherwise
    """
    colors_set = set(colors)
    colors_set.remove(color)
    bag = [color]
    probabilities = [0.5]
    for c in colors_set:
        bag.append(c)
        probabilities.append(0.5 / 3)
    return colors[random.choices(bag, probabilities)[0]]

colors = {'blue': '\x1b[1;34m', 'red': '\x1b[1;31m', 'yellow': '\x1b[1;33m', 'green': '\x1b[1;32m'}
words = {'bleu': 'blue', 'rouge': 'red', 'jaune': 'yellow', 'vert': 'green'}
reset = '\x1b[0m'

for word in words:
    print(get_color(words[word]) + word + reset)

您可以用一组要显示的图像替换上面用来说明的ANSI机制。你知道吗

相关问题 更多 >

    热门问题