I built a robot in Feishu, and I asked ChatGPT to help write the algorithm

I. Introduction

Environment:
System: Windows 11 64-bit
Python version: Python 3.9

Note: This article does not talk about how to realize it, but only about the effect of realization and some thoughts. If you are interested, consider configuring related robots.

Let’s ask ChatGPT two questions first:
1. What is ChatGPT?
Let's take a look at ChatGPT's definition of itself:
image.png

ChatGPT is a chatbot technology based on natural language processing (NLP), which can help users communicate with chatbots in natural language. ChatGPT uses deep learning technology to simulate human natural language communication, thereby realizing the chatbot function of natural language processing (NLP).

2. What can ChatGPT do?
Let's take a look at ChatGPT's definition of itself:
image.png

After trying it out, I have a strong feeling - ChatGPT is really powerful!
It is like an all-around player. It can help me understand some unknown fields and help me answer some very simple questions in other fields. As long as you have questions, it can take the trouble to answer them for you.
If you are learning knowledge in a certain field and encounter a problem, ask it, and it may also give you an answer or some inspiration.
What surprises me even more is that it can also help me write algorithms, which greatly improves my learning efficiency, I love it so much! Next, three small examples of algorithms are introduced:

Note: This article only tells stories, not principles and operations

2. Let ChatGPT help me write the algorithm

The following is a brief description of the three algorithms ChatGPT wrote for me.

2.1 Dichotomy

image.png

def binary_search(list, item):
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1
    return None

my_list = [1, 3, 5, 7, 9]

print(binary_search(my_list, 3))  # => 1
print(binary_search(my_list, -1)) # => None

I remember that when I first wrote the dichotomy algorithm , I encountered a pitfall, that is, I didn’t know how to deal [0, n-1]with the boundary value of slaves. Later, I used it to [-1, n]solve the problem perfectly. Today, AI taught me that it turned out to be a new When assigning low and high variables, add 1 and subtract 1, get it! ! !

2.2 Apriori calculation

Apriori algorithm is a data mining algorithm for discovering association rules. Its basic idea is: if an itemset is a frequent itemset, then all its subsets are also frequent itemsets. The Apriori algorithm finds frequent itemsets in an iterative manner, and its basic steps are as follows:

  1. Construct candidate itemsets from the original data and calculate the support of each itemset;
  2. Filter out frequent itemsets according to the threshold of support;
  3. Build association rules from frequent itemsets;
  4. Effective association rules are filtered out according to the threshold of confidence.

Next, ask ChatGPT how to implement the algorithm in Python.
image.png
ChatGPT used Python to help me write the Apriori algorithm code, and also brought a data set, just use it and run it.
If you want to use your own dataset, just replace the dataset in the following code, it’s not too easy!
The complete code provided by ChatGPT is as follows:

# > Python实现Apriori算法
# -*- coding:utf-8 -*-

# 导入相关库
import pandas as pd
import numpy as np

# 加载数据集
dataset = [['牛奶', '面包', '尿布', '啤酒', '鸡蛋'],
           ['牛奶', '尿布', '啤酒', '可乐'],
           ['面包', '牛奶', '尿布', '啤酒'],
           ['面包', '牛奶', '尿布', '可乐']]

# 将数据集转换为DataFrame格式
df = pd.DataFrame(dataset)

# 定义Apriori算法函数
def apriori(data, min_support=0.5):
    # 将数据转换为集合格式
    C1 = create_C1(data)
    # 将数据集转换为二维列表
    D = list(map(set, data))
    # 构建初始候选项集C1
    L1, support_data = scan_D(D, C1, min_support)
    # 构建完整的候选项集Ck
    L = [L1]
    k = 2
    while (len(L[k-2]) > 0):
    Ck = apriori_gen(L[k-2], k)
    Lk, supK = scan_D(D, Ck, min_support)
    support_data.update(supK)
    L.append(Lk)
    k += 1
    return L, support_data

    # 构建初始候选项集C1
    def create_C1(data):
    C1 = []
    for transaction in data:
    for item in transaction:
    if not [item] in C1:
    C1.append([item])
    C1.sort()
    return list(map(frozenset, C1))

    # 扫描数据集,从C1生成L1
    def scan_D(D, Ck, min_support):
    ss_cnt = {
    
    }
    for tid in D:
    for can in Ck:
    if can.issubset(tid):
    if can not in ss_cnt:
    ss_cnt[can] = 1
else:
    ss_cnt[can] += 1
    num_items = float(len(D))
    ret_list = []
    support_data = {
    
    }
    for key in ss_cnt:
    support = ss_cnt[key] / num_items
    if support >= min_support:
    ret_list.insert(0, key)
    support_data[key] = support
    return ret_list, support_data

    # 根据频繁项集的集合进行合并
    def apriori_gen(Lk, k):
    ret_list = []
    len_Lk = len(Lk)
    for i in range(len_Lk):
    for j in range(i+1, len_Lk):
    L1 = list(Lk[i])[:k-2]
    L2 = list(Lk[j])[:k-2]
    L1.sort()
    L2.sort()
    if L1 == L2:
    ret_list.append(Lk[i] | Lk[j])
    return ret_list

    # 调用Apriori算法
    L, support_data = apriori(dataset, min_support=0.5)

    # 打印结果
    print("频繁项集:", L)
    print("所有候选项集的支持度信息:", support_data) 

2.3 Perceptron

This one is a little more complicated, and the returned code seems to be a bit problematic. I can't run it directly with Python 3.9.
As long as it is the formatted part %!s(MISSING), %!f(MISSING), %!((MISSING)self.weights, self.bias), %!d(MISSING)and %!a(MISSING)nd_perception.predict([1, 1])).
code show as below:

# > Python实现感知机
# 感知机
import numpy as np

# 定义激活函数
def sign(x):
    if x >= 0:
        return 1
    else:
        return -1

# 定义感知机
class Perceptron(object):
    def __init__(self, input_num, activator):
        self.activator = activator
        # 权重向量初始化为0
        self.weights = [0.0 for _ in range(input_num)]
        # 偏置项初始化为0
        self.bias = 0.0

    def __str__(self):
        return 'weights\t:%!s(MISSING)\nbias\t:%!f(MISSING)\n' %!((MISSING)self.weights, self.bias)

    # 输入向量,计算感知机的输出
    def predict(self, input_vec):
        # 把input_vec[x1,x2,x3...]和weights[w1,w2,w3,...]打包在一起
        # 变成[(x1,w1),(x2,w2),(x3,w3),...]
        # 然后利用map函数计算[x1*w1, x2*w2, x3*w3]
        # 最后利用reduce求和
        return self.activator(
            np.dot(input_vec, self.weights) + self.bias)

    # 输入训练数据:一组向量、与每个向量对应的label;以及训练轮数,根据训练数据调整权重
    def train(self, input_vecs, labels, iteration, rate):
        for i in range(iteration):
            self._one_iteration(input_vecs, labels, rate)

    # 一次迭代,把所有的训练数据过一遍
    def _one_iteration(self, input_vecs, labels, rate):
        # 把输入和输出打包在一起,成为样本的列表[(input_vec, label), ...]
        # 而每个训练样本是(input_vec, label)
        samples = zip(input_vecs, labels)
        # 对每个样本,按照感知机规则更新权重
        for (input_vec, label) in samples:
            # 计算感知机在当前权重下的输出
            output = self.predict(input_vec)
            # 更新权重
            self._update_weights(input_vec, output, label, rate)

    # 按照感知机规则更新权重
    def _update_weights(self, input_vec, output, label, rate):
        # 把input_vec[x1,x2,x3,...]和weights[w1,w2,w3,...]打包在一起
        # 变成[(x1,w1),(x2,w2),(x3,w3),...]
        # 然后利用感知器规则更新权重
        delta = label - output
        self.weights = list(map(
            lambda x_w: x_w[1] + rate * delta * x_w[0],
            zip(input_vec, self.weights)))
        # 更新bias
        self.bias += rate * delta

def f(x):
    return 1 if x > 0 else 0

def get_training_dataset():
    # 构建训练数据
    # 输入向量列表
    input_vecs = [[1,1], [0,0], [1,0], [0,1]]
    # 期望的输出列表,注意要与输入一一对应
    # [1,1] -> 1, [0,0] -> 0, [1,0] -> 0, [0,1] -> 0
    labels = [1, 0, 0, 0]
    return input_vecs, labels

def train_and_perceptron():
    # 创建感知器,输入参数个数为2(因为and是二元函数),激活函数为f
    p = Perceptron(2, f)
    # 训练,迭代10轮, 学习速率为0.1
    input_vecs, labels = get_training_dataset()
    p.train(input_vecs, labels, 10, 0.1)
    #返回训练好的感知器
    return p

if __name__ == '__main__':
    # 训练and感知器
    and_perception = train_and_perceptron()
    # 打印训练获得的权重
    print(and_perception)
    # 测试
    print('1 and 1 = %!d(MISSING)' %!a(MISSING)nd_perception.predict([1, 1]))
    print('0 and 0 = %!d(MISSING)' %!a(MISSING)nd_perception.predict([0, 0]))
    print('1 and 0 = %!d(MISSING)' %!a(MISSING)nd_perception.predict([1, 0]))
    print('0 and 1 = %!d(MISSING)' %!a(MISSING)nd_perception.predict([0, 1]))

These are a bit beyond my cognition, I asked ChatGPT python %!d 是什么意思, it said "%!!(MISSING)d is a format string in Python, it means to replace the variable with its corresponding decimal integer." How to answer
more !?
image.png
Then use the information it provides to ask, %!!(MISSING)d 怎么使用and the information it replies seems to be a bit problematic:

  • %!n(MISSING)umbershould be%!(MISSING)number
  • "%!d(MISSING)" %!(MISSING)10Can't run directly, this is invalid syntaximage.png

image.png
I checked the official website, and there is no relevant information about formatting + missing, and I couldn't find it directly on Baidu. Let's just assume it's a bug. Modify the formatting part, and you can run normally. After the modification, the specific code is as follows:

# > Python实现感知机
# 感知机
import numpy as np

# 定义激活函数
def sign(x):
    if x >= 0:
        return 1
    else:
        return -1

# 定义感知机
class Perceptron(object):
    def __init__(self, input_num, activator):
        self.activator = activator
        # 权重向量初始化为0
        self.weights = [0.0 for _ in range(input_num)]
        # 偏置项初始化为0
        self.bias = 0.0

    def __str__(self):
        # return 'weights\t:%!s(MISSING)\nbias\t:%!f(MISSING)\n' %!((MISSING)self.weights, self.bias)
        return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias)
    # 输入向量,计算感知机的输出
    def predict(self, input_vec):  #input_vec相当于X
        # 把input_vec[x1,x2,x3...]和weights[w1,w2,w3,...]打包在一起
        # 变成[(x1,w1),(x2,w2),(x3,w3),...]
        # 然后利用map函数计算[x1*w1, x2*w2, x3*w3]
        # 最后利用reduce求和
        return self.activator(np.dot(input_vec, self.weights) + self.bias)

    # 输入训练数据:一组向量、与每个向量对应的label;以及训练轮数,根据训练数据调整权重
    def train(self, input_vecs, labels, iteration, rate):
        for i in range(iteration):
            self._one_iteration(input_vecs, labels, rate)

    # 一次迭代,把所有的训练数据过一遍
    def _one_iteration(self, input_vecs, labels, rate):
        # 把输入和输出打包在一起,成为样本的列表[(input_vec, label), ...]
        # 而每个训练样本是(input_vec, label)
        samples = zip(input_vecs, labels)
        # 对每个样本,按照感知机规则更新权重
        for (input_vec, label) in samples:
            # 计算感知机在当前权重下的输出
            output = self.predict(input_vec)
            # 更新权重
            self._update_weights(input_vec, output, label, rate)

    # 按照感知机规则更新权重
    def _update_weights(self, input_vec, output, label, rate):
        # 把input_vec[x1,x2,x3,...]和weights[w1,w2,w3,...]打包在一起
        # 变成[(x1,w1),(x2,w2),(x3,w3),...]
        # 然后利用感知器规则更新权重
        delta = label - output   #实际值-预测值
        self.weights = list(map(
            lambda x_w: x_w[1] + rate * delta * x_w[0],  #速率*(实际值-预测值)*x值
            zip(input_vec, self.weights)))
        # 更新bias
        self.bias += rate * delta

def f(x):
    return 1 if x > 0 else 0

def get_training_dataset():
    """
    input_vecs相当于X_train
    labels相当于y_train
    :return:
    """
    # 构建训练数据
    # 输入向量列表
    input_vecs = [[1,1], [0,0], [1,0], [0,1]]
    # 期望的输出列表,注意要与输入一一对应
    # [1,1] -> 1, [0,0] -> 0, [1,0] -> 0, [0,1] -> 0
    labels = [1, 0, 0, 0]
    return input_vecs, labels

def train_and_perceptron():
    # 创建感知器,输入参数个数为2(因为and是二元函数),激活函数为f
    p = Perceptron(2, f)
    # 训练,迭代10轮, 学习速率为0.1
    input_vecs, labels = get_training_dataset()
    p.train(input_vecs, labels, 10, 0.1)
    #返回训练好的感知器
    return p

if __name__ == '__main__':
    # 训练and感知器
    and_perception = train_and_perceptron()
    # 打印训练获得的权重
    print(and_perception)
    # 测试
    # print('1 and 1 = %!d(MISSING)' %!(MISSING)and_perception.predict([1, 1]))
    # print('0 and 0 = %!d(MISSING)' %!(MISSING)and_perception.predict([0, 0]))
    # print('1 and 0 = %!d(MISSING)' %!(MISSING)and_perception.predict([1, 0]))
    # print('0 and 1 = %!d(MISSING)' %!(MISSING)and_perception.predict([0, 1]))
    print('1 and 1 = %d' % and_perception.predict([1, 1]))
    print('0 and 0 = %d' % and_perception.predict([0, 0]))
    print('1 and 0 = %d' % and_perception.predict([1, 0]))
    print('0 and 1 = %d' % and_perception.predict([0, 1]))

3. Summary

I have to say, ChatGPT is awesome! Otherwise, it would not be so popular. For unknown fields, we often have all kinds of "stupid" questions, and maybe some are embarrassed to ask tutors or experts, for fear that the questions are too ignorant and will be "disliked". Moreover, there are endless question marks in the mind, and it takes a long time for others to answer them one by one. ChatGPT can solve most of our similar novice questions and help us get started in related knowledge fields to a certain extent.

From the above description, maybe parents who have been parents will have a more "painful" understanding. Children will have all kinds of whimsical ideas in their heads around the age of 4. Maybe ChatGPT will be more suitable to satisfy children's various needs. All kinds of curiosity, of course, this is just an idea, it needs to be verified~~

Guess you like

Origin blog.csdn.net/qq_45476428/article/details/128276734