Python 3.x 如何循环浏览项目并运行和绘制scikit模型?

Python 3.x 如何循环浏览项目并运行和绘制scikit模型?,python-3.x,pandas,for-loop,plot,scikit-learn,Python 3.x,Pandas,For Loop,Plot,Scikit Learn,我从比赛中获得了一些有趣的用户数据。我知道运动员们计划什么时候完成比赛,我也知道他们什么时候真正完成比赛(除了一些其他的东西)。目的是了解运动员何时迟到。我想为每个运动员运行一个支持向量机,并绘制决策边界 以下是我的工作: import numpy as np import pandas as pd from sklearn import svm from mlxtend.plotting import plot_decision_regions import matplotlib.pyplot

我从比赛中获得了一些有趣的用户数据。我知道运动员们计划什么时候完成比赛,我也知道他们什么时候真正完成比赛(除了一些其他的东西)。目的是了解运动员何时迟到。我想为每个运动员运行一个支持向量机,并绘制决策边界

以下是我的工作:

import numpy as np
import pandas as pd
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'User': np.random.random_integers(low=1, high=4, size=50),
                    'Planned_End': np.random.uniform(low=-5, high=5, size=50),
                   'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
                   'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit Support Vector Machine Classifier
X = df[['Planned_End', 'Actual_End']]
y = df['Late']

clf = svm.SVC(decision_function_shape='ovo')

for i, y in df['User']:
    clf.fit(X, y)
    ax = plt.subplot()
    fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)
    plt.title(lab)
plt.show()
我得到了以下错误:
TypeError:'numpy.int64'对象不可编辑
——也就是说,我无法循环遍历该列

我想这可以归结为numpy数据格式?我如何解决这个问题?

试试iteritems()

试试iteritems()


您的
User
系列包含
numpy.int64
对象,因此您只能使用:

for y in df['User']:
而且你不会在任何地方使用
i

至于代码的其余部分,这将产生一些解决方案,请相应地编辑:

import numpy as np
import pandas as pd
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'User': np.random.random_integers(low=1, high=4, size=50),
                    'Planned_End': np.random.uniform(low=-5, high=5, size=50),
                   'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
                   'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit Support Vector Machine Classifier
X = df[['Planned_End', 'Actual_End']].as_matrix()
y = df['Late']

clf = svm.SVC(decision_function_shape='ovo')


y = df['User'].values
clf.fit(X, y)
ax = plt.subplot()
fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)
plt.title('lab')
plt.show()

您的
User
系列包含
numpy.int64
对象,因此您只能使用:

for y in df['User']:
而且你不会在任何地方使用
i

至于代码的其余部分,这将产生一些解决方案,请相应地编辑:

import numpy as np
import pandas as pd
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'User': np.random.random_integers(low=1, high=4, size=50),
                    'Planned_End': np.random.uniform(low=-5, high=5, size=50),
                   'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
                   'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit Support Vector Machine Classifier
X = df[['Planned_End', 'Actual_End']].as_matrix()
y = df['Late']

clf = svm.SVC(decision_function_shape='ovo')


y = df['User'].values
clf.fit(X, y)
ax = plt.subplot()
fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)
plt.title('lab')
plt.show()

非常感谢。到目前为止我还不太明白。我在df['Responsible User']中尝试了您的方法并
为I。Itterrows():
两个返回都
'Series'对象没有属性“Itterrows”
抱歉,我的错误,请尝试iteritems()。好的,谢谢!我想我快到了。我想我需要先将数据帧转换为
np.array
?我发现错误
y必须是1D NumPy数组
。谢谢!到目前为止我还不太明白。我在df['Responsible User']中尝试了您的方法并
为I。Itterrows():
两个返回都
'Series'对象没有属性“Itterrows”
抱歉,我的错误,请尝试iteritems()。好的,谢谢!我想我快到了。我想我需要先将数据帧转换为
np.array
?我发现错误
y必须是1D NumPy数组
。谢谢!我来自一个稍有不同的技术层,只是简单地使用了我知道的语法并进行了尝试——显然没有成功。我想我在使用
y
和`
I
时犯了一个很大的错误。我只想在运动员中循环-实际上是
numpy.int64
并为每个运动员运行clf。我怎么也做不到。还有什么建议吗?谢谢-这已经帮了大忙了。然而,我实际上是在为每个运动员单独制作一个情节。也就是说,我正在尝试循环通过
df['Athelte']
并为每个运动员运行
clf()
plt.subplot()
(本例中为四名)。依赖变量仍然应该是
df['Late']
,因此
y
。我的思路类似于
df.groupby('athleter')。apply(clf.fit(X,y))
谢谢!我现在明白了!非常感谢。我来自一个稍有不同的技术层,只是简单地使用了我知道的语法并进行了尝试——显然没有成功。我想我在使用
y
和`
I
时犯了一个很大的错误。我只想在运动员中循环-实际上是
numpy.int64
并为每个运动员运行clf。我怎么也做不到。还有什么建议吗?谢谢-这已经帮了大忙了。然而,我实际上是在为每个运动员单独制作一个情节。也就是说,我正在尝试循环通过
df['Athelte']
并为每个运动员运行
clf()
plt.subplot()
(本例中为四名)。依赖变量仍然应该是
df['Late']
,因此
y
。我的思路类似于
df.groupby('athleter')。apply(clf.fit(X,y))
谢谢!我现在明白了!