Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

4
get_dummies(Pandas)和OneHotEncoder(Scikit-learn)之间的优缺点是什么?
我正在学习将机器学习分类器将分类变量转换为数字的不同方法。我遇到了这种pd.get_dummies方法,sklearn.preprocessing.OneHotEncoder()我想看看它们在性能和用法上有何不同。 我发现关于如何使用教程OneHotEncoder()上https://xgdgsc.wordpress.com/2015/03/20/note-on-using-onehotencoder-in-scikit-learn-to-work-on-categorical-features/自该sklearn文档对该功能的帮助不是很大。我有一种感觉,我做得不正确...但是 有人能解释一下使用pd.dummiesover的利弊sklearn.preprocessing.OneHotEncoder()吗?我知道这OneHotEncoder()为您提供了一个稀疏矩阵,但除此之外,我不确定该如何使用以及该pandas方法有什么好处。我使用效率低下吗? import pandas as pd import numpy as np from sklearn.datasets import load_iris sns.set() %matplotlib inline #Iris Plot iris = load_iris() n_samples, m_features = iris.data.shape #Load Data X, y = iris.data, iris.target D_target_dummy = dict(zip(np.arange(iris.target_names.shape[0]), iris.target_names)) DF_data = pd.DataFrame(X,columns=iris.feature_names) DF_data["target"] = pd.Series(y).map(D_target_dummy) #sepal length (cm) sepal width (cm) …

3
是否可以输入提示lambda函数?
当前,在Python中,可以如下提示类型的函数的参数和返回类型: def func(var1: str, var2: str) -> int: return var1.index(var2) 这表示该函数采用两个字符串,并返回一个整数。 但是,此语法与lambda高度混淆,如下所示: func = lambda var1, var2: var1.index(var2) 我试图在参数和返回类型上都添加类型提示,但我想不出一种不会引起语法错误的方法。 是否可以输入提示lambda函数?如果没有,是否有计划使用类型提示lambda,或者有什么原因(除了明显的语法冲突)为何?
83 python  lambda 

4
python中int()的违反直觉的行为
在文档中明确指出int(number)是地板类型转换: int(1.23) 1 当且仅当字符串是整数文字时,int(string)才返回int。 int('1.23') ValueError int('1') 1 有什么特殊原因吗?我发现在一种情况下功能处于下限,而在另一种情况下则没有功能。
83 python 

3
Python中unicode()和encode()函数的用法
我在对路径变量进行编码并将其插入SQLite数据库时遇到问题。我试图用无济于事的encode(“ utf-8”)函数解决此问题。然后,我使用unicode()函数为我提供unicode类型。 print type(path) # <type 'unicode'> path = path.replace("one", "two") # <type 'str'> path = path.encode("utf-8") # <type 'str'> strange path = unicode(path) # <type 'unicode'> 最终我获得了unicode类型,但是当path变量的类型为str时,仍然出现相同的错误 sqlite3.ProgrammingError:除非使用可以解释8位字节串的text_factory(如text_factory = str),否则不得使用8位字节串。强烈建议您改为将应用程序切换为Unicode字符串。 您能帮我解决此错误,并解释encode("utf-8")和unicode()功能的正确用法吗?我经常为此而斗争。 编辑: 此execute()语句引发错误: cur.execute("update docs set path = :fullFilePath where path = :path", locals()) 我忘记更改具有相同问题的fullFilePath变量的编码,但是现在我很困惑。我应该只使用unicode()还是encode(“ utf-8”)还是两者都使用? 我不能用 fullFilePath = …


5
在Python中跨平台/ dev / null
我正在使用以下代码在Linux / OSX上为Python库隐藏stderr,但我不控制默认情况下写入stderr的Python库: f = open("/dev/null","w") zookeeper.set_log_stream(f) 是否有一个简单的跨平台替代/ dev / null?理想情况下,它不会消耗内存,因为这是一个长期运行的过程。
83 python 

5
Python中的双精度浮点值?[关闭]
已关闭。这个问题需要更加集中。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅通过编辑此帖子来关注一个问题。 2个月前关闭。 改善这个问题 是否有比浮点精度更好的数据类型?


5
插入后如何更新Mongo文件?
假设我插入了文档。 post = { some dictionary } mongo_id = mycollection.insert(post) 现在,假设我要添加一个字段并对其进行更新。我怎么做?这似乎不起作用..... post = mycollection.find_one({"_id":mongo_id}) post['newfield'] = "abc" mycollection.save(post)

3
Django:模型表格“对象没有属性'cleaned_data'”
我正在尝试为我的一门课做搜索表格。表单的模型为: from django import forms from django.forms import CharField, ModelMultipleChoiceField, ModelChoiceField from books.models import Book, Author, Category class SearchForm(forms.ModelForm): authors = ModelMultipleChoiceField(queryset=Author.objects.all(),required=False) category = ModelChoiceField (queryset=Category.objects.all(),required=False) class Meta: model = Book fields = ["title"] 我正在使用的视图是: from django.shortcuts import render_to_response, redirect, get_object_or_404 from django.template import RequestContext from books.models import Book,Author from …




1
declarative_base()和db.Model有什么区别?
Flask-SQLAlchemy插件的快速入门教程指导用户创建继承db.Model该类的表模型,例如 app = Flask(__main__) db = SQLAlchemy(app) class Users(db.Model): __tablename__ = 'users' ... 但是,SQLAlchemy教程和bottle-SQLAlchemy README都建议表模型继承从Base实例化declarative_base()。 Base = declarative_base() class Users(Base): __tablename__ = 'users' ... 这两种方法有什么区别?

6
Python中的嵌套函数
像这样的Python代码可以给我们带来什么好处或启示: class some_class(parent_class): def doOp(self, x, y): def add(x, y): return x + y return add(x, y) 我在一个开源项目中发现了这一点,它在嵌套函数内部做了一些有用的事情,但是除了调用它之外,什么也没做。(可以在此处找到实际的代码。)为什么有人会这样编码?在嵌套函数内部而不是外部普通函数中编写代码是否有好处或副作用?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.