辅助编程coding的两种工具:Github Copilot、Cursor

文章介绍了Cursor和GitHubCopilot这两个AI编程工具,提供了下载地址和使用技巧。Cursor是一个集成在编辑器中的AI助手,能帮助编写、修改和理解代码。GitHubCopilot则是基于OpenAICodex的代码建议工具,可在多种编程语言中提供代码补全。文章还举例说明了如何使用这两个工具,并提醒在使用自动代码生成时要注意代码的正确性和适用性,特别是在数学和统计计算中需验证生成代码的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cursor

简介

Cursor is an editor made for programming with AI. It’s early days, but right now Cursor can help you with a few things…

  • Write: Generate 10-100 lines of code with an AI that’s smarter than Copilot
  • Diff: Ask the AI to edit a block of code, see only proposed changes
  • Chat: ChatGPT-style interface that understands your current file
  • And more: ask to fix lint errors, generate tests/comments on hover, etc

下载地址:

https://www.cursor.so/
在这里插入图片描述

使用技巧:

https://twitter.com/amanrsanger

CHAT:

example 1:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意:

对于上面最后一张图的中的代码,如果直接在IDE里面运行是不会报错的,但是有一句代码

vif["VIF"] = [variance_inflation_factor(df.values, i) for i in range(df.shape[1]-1)]

是不符合多重共线性分析或者VIF的数学原理的。因为VIF是对自变量间线性关系的分析,如果直接调用OLS;如果把OLS里面的目标函数换成非线性方程,就是表达的非线性关系。而上面的代码是把df.values都传入了variance_inflation_factor函数,包括了自变量和因变量,因此是不符合多重共线性分析原理的。
所以应改成:

import pandas as pd

data = {'x1': [1, 2, 3, 4, 5],
        'x2': [2, 4, 6, 8, 10],
        'x3': [3, 6, 9, 12, 15],
        'y': [2, 4, 6, 8, 10]}

df = pd.DataFrame(data)

from statsmodels.stats.outliers_influence import variance_inflation_factor

# Get the VIF for each feature
vif = pd.DataFrame()
vif["feature"] = df.columns[:-1]
# vif["VIF"] = [variance_inflation_factor(df.values, i) for i in range(df.shape[1]-1)]
vif["VIF"] = [variance_inflation_factor(df.values[:, :-1], i) for i in range(df.shape[1]-1)]

# Print the results
print(vif)

原理解释:

def variance_inflation_factor(exog, exog_idx):
    """
    Variance inflation factor, VIF, for one exogenous variable

    The variance inflation factor is a measure for the increase of the
    variance of the parameter estimates if an additional variable, given by
    exog_idx is added to the linear regression. It is a measure for
    multicollinearity of the design matrix, exog.

    One recommendation is that if VIF is greater than 5, then the explanatory
    variable given by exog_idx is highly collinear with the other explanatory
    variables, and the parameter estimates will have large standard errors
    because of this.

    Parameters
    ----------
    exog : {ndarray, DataFrame}
        design matrix with all explanatory variables, as for example used in
        regression
    exog_idx : int
        index of the exogenous variable in the columns of exog

    Returns
    -------
    float
        variance inflation factor

    Notes
    -----
    This function does not save the auxiliary regression.

    See Also
    --------
    xxx : class for regression diagnostics  TODO: does not exist yet

    References
    ----------
    https://en.wikipedia.org/wiki/Variance_inflation_factor
    """
    k_vars = exog.shape[1]
    exog = np.asarray(exog)
    x_i = exog[:, exog_idx]
    mask = np.arange(k_vars) != exog_idx
    x_noti = exog[:, mask]
    r_squared_i = OLS(x_i, x_noti).fit().rsquared
    vif = 1. / (1. - r_squared_i)
    return vif

example 2:

在这里插入图片描述

在这里插入图片描述
GPT-4太大写不了,给出的是调GPT-2的示例代码。

Github Copilot

官网

https://github.com/features/copilot

简介

  • GitHub Copilot uses the OpenAI Codex to suggest code and entire functions in real-time, right from your editor.
  • Trained on billions of lines of code, GitHub Copilot turns natural language prompts into coding suggestions across dozens of languages.
  • Don’t fly solo
    Developers all over the world use GitHub Copilot to code faster, focus on business logic over boilerplate, and do what matters most: building great software.
  • Focus on solving bigger problems
    Spend less time creating boilerplate and repetitive code patterns, and more time on what matters: building great software. Write a comment describing the logic you want and GitHub Copilot will immediately suggest code to implement the solution.
  • Get AI-based suggestions, just for you
    GitHub Copilot shares recommendations based on the project’s context and style conventions. Quickly cycle through lines of code, complete function suggestions, and decide which to accept, reject, or edit.
  • Code confidently in unfamiliar territory
    Whether you’re working in a new language or framework, or just learning to code, GitHub Copilot can help you find your way. Tackle a bug, or learn how to use a new framework without spending most of your time spelunking through the docs or searching the web.

在这里插入图片描述
目前有个人版、商业版和教育版三种,教育版里面学生账户比教师账户的免费功能更多,只需要一个学校邮箱和一卡通就可以认证学生账户或者教师账户,还是很方便。

以插件方式安装

pycharm

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

自动写代码

以下代码除了第一行注释都是copilot自动补全的

example 1:写一个mysql取数据的类

# 写一个mysql取数据的类
import pymysql
class Mysql:
    def __init__(self,host,user,password,database,port=3306):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.conn = pymysql.connect(host=self.host,user=self.user,password=self.password,database=self.database,port=self.port)
        self.cursor = self.conn.cursor()
    def fetchone(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()
    def fetchall(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()
    def close(self):
        self.cursor.close()
        self.conn.close()
    def commit(self):
        self.conn.commit()
    def rollback(self):
        self.conn.rollback()
    def execute(self,sql):
        self.cursor.execute(sql)
        self.commit()
    def executemany(self,sql,values):
        self.cursor.executemany(sql,values)
        self.commit()

example 2:写一个多重共线性检测的类

from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
class Multicollinearity:
    def __init__(self,df):
        self.df = df
    def get_vif(self):
        vif = pd.DataFrame()
        vif["VIF Factor"] = [self.get_vif_score(i) for i in range(self.df.shape[1])]
        vif["features"] = self.df.columns
        return vif
    def get_vif_score(self,i):
        y = self.df.iloc[:,i]
        x = self.df.drop(self.df.columns[i],axis=1)
        lr = LinearRegression()
        lr.fit(x,y)
        r2 = r2_score(y,lr.predict(x))
        return 1/(1-r2)

总结

用工具自动写代码的时候,最好要用实际例子或实际数据检查一下,就算没有报错,对于数值计算最好也要debug跑一遍看它生成的代码是否符合你的描述或者数学原理。具体原因见上文的注意

对于希望找到类似于Cursor的服务,尤其是在中国境内遇到访问困难的情况下,可以考虑以下几个选项: 1. GitHub Copilot GitHub Copilot 是一个由GitHub和OpenAI合作推出的AI结对编程工具。该工具直接集成到了Visual Studio Code编辑器中以及其他一些IDE里,可以根据注释、函数名以及已有的代码来推测出接下来应该写的代码。 2. Tabnine Tabnine是一个通用的代码补全工具,适用于多种编程语言及开发环境。这款工具有免费版和付费的专业版本可供选择,能提供上下文感知的代码预测,从而提高编写代码的速度和准确性。 3. Kite Kite是一款专为Python设计的人工智能驱动型代码完成助手,不过也扩展支持了其他几种流行的语言。Kite可以在本地安装并且不需要互联网连接即可工作,在保护隐私的同时提供了高效的代码提示和服务。 4. Sourcegraph Sourcegraph 提供了一个企业级的代码搜索平台,可以帮助团队更有效地查找、理解和修复代码。虽然不是纯粹意义上的代码生成功具,但其强大的搜索能力有助于加速开发过程。 5. AI Coding Assistant Tools (各种AI编码助理工具) 随着人工智能的发展,市场上出现了许多新的AI编码助理工具,这些工具可能没有像Cursor那样广泛知名,但在特定领域或某些方面可能会更加出色。探索这类新兴产品也可能带来意想不到的好结果。 为了获得最佳体验,建议尝试不同的服务以确定哪一个最符合个人需求和技术栈的要求。此外,持续关注开源社区和其他开发者分享的信息也是发现新资源的有效途径之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山高月小 水落石出

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值