2016-12-06 9 views
0

私は、このデータを持っている:サポートされていないオペランドのタイプ(S) - : 'STR' と 'フロート'

new.csv:

X   Y 
230.1 22.1 
44.5 10.4 
17.2 9.3 
151.5 18.5 
180.8 12.9 
8.7 7.2 
57.5 11.8 
120.2 13.2 
8.6 4.8 
199.8 10.6 
66.1 8.6 
214.7 17.4 
23.8 9.2 
97.5 9.7 
204.1 19 
195.4 22.4 
67.8 12.5 
281.4 24.4 
69.2 11.3 

と私は、線形回帰モデルを適用しようとしています。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import linear_model, cross_validation 
import random 

data = pd.read_csv('./data/new.csv', names=['X', 'Y'], header=0) 


fig = plt.figure() 
ax = plt.axes() 

x = data.loc[:,'X'].to_frame() 
y = data.loc[:,'Y'].to_frame() 

x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.3, random_state=0) 

regr = linear_model.LinearRegression() 

regr.fit(x_train, y_train) 


ax.set(xlabel='X', ylabel='Y', title='X vs Y') 
ax.scatter(x_test,y_test, alpha=0.5, cmap='viridis') 
ax.plot(x_test, regr.predict(x_test), color='red', linewidth=2) 

ここまではすべて正常に動作します。エラーバーを追加しようとする瞬間:

plt.errorbar(x_test,y_test, yerr=1, fmt='o'); 

タイトルに誤りがあります。

完全なエラーは次のとおりです。

TypeError         Traceback (most recent call last) 
<ipython-input-29-cf35bd0c650f> in <module>() 
    29 
    30 #dy=1 
---> 31 plt.errorbar(x_test,y_test, yerr=1); 

../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/pyplot.py in errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, hold, data, **kwargs) 
    2835       xlolims=xlolims, xuplims=xuplims, 
    2836       errorevery=errorevery, capthick=capthick, data=data, 
-> 2837       **kwargs) 
    2838  finally: 
    2839   ax.hold(washold) 

../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs) 
    1817      warnings.warn(msg % (label_namer, func.__name__), 
    1818         RuntimeWarning, stacklevel=2) 
-> 1819    return func(ax, *args, **kwargs) 
    1820   pre_doc = inner.__doc__ 
    1821   if pre_doc is None: 

../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs) 
    2924 
    2925   if yerr is not None: 
-> 2926    lower, upper = extract_err(yerr, y) 
    2927    # select points without upper/lower limits in y and 
    2928    # draw normal errorbars for these points 

../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in extract_err(err, data) 
    2873    # using list comps rather than arrays to preserve units 
    2874    low = [thisx - thiserr for (thisx, thiserr) 
-> 2875     in cbook.safezip(data, err)] 
    2876    high = [thisx + thiserr for (thisx, thiserr) 
    2877      in cbook.safezip(data, err)] 

../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in <listcomp>(.0) 
    2872          "dimensions as x, or 2xN.") 
    2873    # using list comps rather than arrays to preserve units 
-> 2874    low = [thisx - thiserr for (thisx, thiserr) 
    2875     in cbook.safezip(data, err)] 
    2876    high = [thisx + thiserr for (thisx, thiserr) 

TypeError: unsupported operand type(s) for -: 'str' and 'int' 
+3

トレースバック全体を投稿した場合に役立ちます。問題のデバッグに役立ちます。 – pradyunsg

+0

私にとっては完璧に動作します。しかし、あなたのデータは数値ではないようですが、 'df.dtypes'でテストしてください。次に、 'df ['X'] = df ['X'] .stype(float)'のような 'float'にキャストしてみて、値が' df ['X'] = pd.to_numeric(df ['X'] 、errors = 'coerce') '数値でない数値を' NaN'に変換するため – jezrael

+0

@jezrael:私は上記のX値とY値を使用しています。これらはすべて数値です(テスト済みでfloat64も返されます) – George

答えて

0

私が試した:

plt.errorbar(x_test.values, y_test.values, yerr=1, fmt='o'); 

をし、それが働きました!

関連する問題