Cursor
簡(jiǎn)介
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:
注意:
對(duì)于上面最后一張圖的中的代碼,如果直接在IDE里面運(yùn)行是不會(huì)報(bào)錯(cuò)的,但是有一句代碼
vif["VIF"] = [variance_inflation_factor(df.values, i) for i in range(df.shape[1]-1)]
是不符合多重共線性分析或者VIF的數(shù)學(xué)原理的。因?yàn)閂IF是對(duì)自變量間線性關(guān)系的分析,如果直接調(diào)用OLS;如果把OLS里面的目標(biāo)函數(shù)換成非線性方程,就是表達(dá)的非線性關(guān)系。而上面的代碼是把df.values都傳入了variance_inflation_factor函數(shù),包括了自變量和因變量,因此是不符合多重共線性分析原理的。
所以應(yīng)改成:
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太大寫不了,給出的是調(diào)GPT-2的示例代碼。
Github Copilot
官網(wǎng)
https://github.com/features/copilot
簡(jiǎn)介
- 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.
目前有個(gè)人版、商業(yè)版和教育版三種,教育版里面學(xué)生賬戶比教師賬戶的免費(fèi)功能更多,只需要一個(gè)學(xué)校郵箱和一卡通就可以認(rèn)證學(xué)生賬戶或者教師賬戶,還是很方便。
以插件方式安裝
pycharm
自動(dòng)寫代碼
以下代碼除了第一行注釋都是copilot自動(dòng)補(bǔ)全的文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-422730.html
example 1:寫一個(gè)mysql取數(shù)據(jù)的類
# 寫一個(gè)mysql取數(shù)據(jù)的類
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:寫一個(gè)多重共線性檢測(cè)的類
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)
總結(jié)
用工具自動(dòng)寫代碼的時(shí)候,最好要用實(shí)際例子或?qū)嶋H數(shù)據(jù)檢查一下,就算沒(méi)有報(bào)錯(cuò),對(duì)于數(shù)值計(jì)算最好也要debug跑一遍看它生成的代碼是否符合你的描述或者數(shù)學(xué)原理。具體原因見(jiàn)上文的注意。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-422730.html
到了這里,關(guān)于輔助編程coding的兩種工具:Github Copilot、Cursor的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!