我正在通过Anaconda Navigator应用程序(版本1.1.0)运行iPython笔记本.当我想导入大熊猫时,它给了我一个奇怪的错误.我认为Anaconda应用程序包括熊猫包?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-af55e7023913> in <module>()
----> 1 import pandas as pd
/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/__init__.py in <module>()
37 import pandas.core.config_init
38
---> 39 from pandas.core.api import *
40 from pandas.sparse.api import *
41 from pandas.stats.api import *
/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/api.py in <module>()
8 from pandas.core.common import isnull, notnull
9 from pandas.core.categorical import Categorical
---> 10 from pandas.core.groupby import Grouper
11 from pandas.core.format import set_eng_float_format
12 from pandas.core.index import (Index, CategoricalIndex, Int64Index,
/Users/bertcarremans/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in <module>()
16 DataError, SpecificationError)
17 …
Run Code Online (Sandbox Code Playgroud) 我有一个带有2个分类变量的Pandas Dataframe,以及ID变量和目标变量(用于分类).我设法将分类值转换为OneHotEncoder
.这导致稀疏矩阵.
ohe = OneHotEncoder()
# First I remapped the string values in the categorical variables to integers as OneHotEncoder needs integers as input
... remapping code ...
ohe.fit(df[['col_a', 'col_b']])
ohe.transform(df[['col_a', 'col_b']])
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在DecisionTreeClassifier中使用这个稀疏矩阵?特别是当我想稍后在我的数据帧中添加一些其他非分类变量时.谢谢!
编辑 回复miraculixx的评论:我还尝试了sklearn-pandas中的DataFrameMapper
mapper = DataFrameMapper([
('id_col', None),
('target_col', None),
(['col_a'], OneHotEncoder()),
(['col_b'], OneHotEncoder())
])
t = mapper.fit_transform(df)
Run Code Online (Sandbox Code Playgroud)
但后来我得到了这个错误:
TypeError:类型不支持转换:(dtype('O'),dtype('int64'),dtype('float64'),dtype('float64')).
我有一个带有目标变量的数据集,该变量可以具有7个不同的标签。我的训练集中的每个样本都只有一个目标变量标签。
对于每个样本,我想计算每个目标标签的概率。所以我的预测将由每行7个概率组成。
在sklearn网站上,我读到了有关多标签分类的信息,但这似乎不是我想要的。
我尝试了以下代码,但是每个样本只能给我一个分类。
from sklearn.multiclass import OneVsRestClassifier
clf = OneVsRestClassifier(DecisionTreeClassifier())
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
有人对此有建议吗?谢谢!
在我的Ionic应用程序中,有一个带有卡的listView,用户可以在其上单击.点击后,他们应该看到一个包含更多细节的页面.所有内容都在pouchDB数据库中(也在Firebase中远程).生成此列表的代码如下:
<div class="card" ng-repeat="challenge in vm.challenges track by $index" ng-class="{'bounceInLeft': $index % 2 !== 0, 'bounceInRight': $index % 2 === 0}" ng-click="vm.goToChallenge(challenge)">
<div class="item item-text-wrap">
<div class="resource">
<img ng-src="{{ challenge.resources[0].image }}" ng-attr-alt="{{ challenge.resources[0].caption }}" ng-srcset="{{ challenge.resources[0].srcset }}" ng-if="challenge.resources && challenge.resources[0].type === 'img'">
<img ng-src="{{ challenge.resources[0].url | getYouTubeImage }}" ng-attr-alt="{{ challenge.resources[0].caption }}" ng-if="challenge.resources && challenge.resources[0].type === 'video'">
<i class="ion-play" ng-if="challenge.resources && challenge.resources[0].type === 'video'"></i>
</div>
<p class="category">{{ challenge.categoryName }}</p>
<p class="name">{{ challenge.name }}</p>
</div>
<div class="item footer item-divider">
<div class="row">
<div …
Run Code Online (Sandbox Code Playgroud) I have a DataFrame with an ID variable and another categorical variable. I want to create dummy variables out of the categorical variable with get_dummies.
dum = pd.get_dummies(df)
Run Code Online (Sandbox Code Playgroud)
However, this makes the ID variable disappear. And I need this ID variable later on to merge to other data sets.
Is there a way to keep other variables. In the documentation of get_dummies I could not find anything. Thanks!
我有一个具有分类和数字功能的数据集,我想在其上应用一些转换,然后是XGBClassifier。
链接到数据集:https : //www.kaggle.com/blastchar/telco-customer-churn
由于数字和分类功能的转换不同,因此我使用了sklearn_pandas及其DataFrameMapper。
要对分类特征执行一键编码,我想使用DictVectorizer。但是要使用DictVectorizer,我首先需要将数据帧转换为字典,然后尝试使用自定义转换器Dictifier进行处理。
当我运行管道时,出现错误“ builtin_function_or_method”对象是不可迭代的。有人知道什么可能导致此错误吗?
import numpy as np
import pandas as pd
from sklearn_pandas import DataFrameMapper
from sklearn_pandas import CategoricalImputer
from sklearn_pandas import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import StandardScaler
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import Pipeline
from sklearn.pipeline import FeatureUnion
from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
import xgboost as xgb
# Importing the data
df = pd.read_csv('../input/WA_Fn-UseC_-Telco-Customer-Churn.csv', na_values=' ')
X, y = df.iloc[:,1:-1], df.iloc[:,-1] …
Run Code Online (Sandbox Code Playgroud) python pipeline machine-learning scikit-learn sklearn-pandas
我正在 PyCharm 中编写一个小型 Python 游戏。我正在使用 Python 3.4 版本的 Macbook 上执行此操作。游戏会打开一个 Tkinter 窗口并向其中添加一些内容。但是,运行游戏时,它会显示非常短暂并立即关闭。
我在 Stackoverflow 上找到了一些在游戏结束时添加输入(“按关闭窗口”)的提示。确实,这确保了窗口不会立即关闭,但对于游戏来说并不实用。在游戏中,用户需要使用方向键来玩。因此在这种情况下添加 input(...) 是没有用的。如何防止窗口自动关闭?谢谢!
代码如下:
from tkinter import *
# Scherm maken
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('Bellenschieter')
c = Canvas(window,width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
# Duikboot maken
ship_id = c.create_polygon(5,5,5,25,30,15,fill='red')
ship_id2 = c.create_oval(0,0,30,30,outline='red')
SHIP_R = 15
MID_X = WIDTH/2
MID_Y = HEIGHT/2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
# Duikboot besturen
SHIP_SPD = 10
def move_ship(event):
if event.keysym == 'Up':
c.move(ship_id, 0, -SHIP_SPD) …
Run Code Online (Sandbox Code Playgroud) datetime.now().date()
在Python 3中打印提供类似2015-09-29的内容.但是,当我想比较datetime.now().date()
这个常量时,它不起作用.我尝试了以下代码片段
import datetime
if datetime.now().date() == 2015-09-29:
print('Hello')
Run Code Online (Sandbox Code Playgroud)
这会导致语法错误:令牌无效.
import datetime
if datetime.now().date() == '2015-09-29':
print('Hello')
Run Code Online (Sandbox Code Playgroud)
这将导致没有错误,但(上运行2015年9月29日这段代码时),它不打印您好要么
import datetime
if datetime.now().date() == datetime.date(2015,9,29):
print('Hello')
Run Code Online (Sandbox Code Playgroud)
这会导致错误:描述符'date'需要'datetime.datetime'对象,但收到'int'.
有人可以帮我解决这个相当简单的问题吗?谢谢!
python ×5
pandas ×3
scikit-learn ×3
python-3.x ×2
anaconda ×1
android ×1
angularjs ×1
date ×1
datetime ×1
ios ×1
javascript ×1
pipeline ×1
pycharm ×1
python-2.7 ×1
tkinter ×1