2017-02-08 3 views
0

ニューラルネットワーク(https://www.youtube.com/playlist?list=PL2-dafEMk2A7YdKv4XfKpfbTH5z6rEEj3)の作成方法については、Siraj Ravalのビデオシリーズに従っています。実際に私が作成したテキストドキュメントからデータ値を取得するには、多くの問題があります。私があれば、私は依存性がないか、正しくここPythonでポイント文書のテキスト文書を参照するにはどうすればよいですか?

それを呼んでいたことも思っていた私は、コマンドプロンプトで取得していますエラーです:

C:\Users\Liam\Desktop>py NeuralNets.py 
Traceback (most recent call last): 
    File "NeuralNets.py", line 7, in <module> 
    x_values = dataframe[['Brain']] 
    File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2053, in __getitem__ 
    return self._getitem_array(key) 
    File "D:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2097, in _getitem_array 
    indexer = self.ix._convert_to_indexer(key, axis=1) 
    File "D:\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1230, in _convert_to_indexer 
    raise KeyError('%s not in index' % objarr[mask]) 
KeyError: "['Brain'] not in index" 

と、ここで私が使用しているコードです:

import pandas as pd 
from sklearn import linear_model 
import matplotlib.pyplot as plt 

# read data 
dataframe = pd.read_fwf('brain_body.txt') 
x_values = dataframe[['Brain']] 
y_values = dataframe[['Body']] 

# train model on data 
body_reg = linear+model.LinearRegression() 
body_reg.fit(x_values, y_values) 

# visualize results 
plt.scatter(x_values, y_values) 
plt.plot(x_values, body_reg.predict(x_values)) 
plt.show() 

私はこの問題の答えが本当に明白な場合、私は申し訳ありませんので、私は合計n00bだと考慮してください。受け入れられたすべての助けと提案、ありがとう!

+0

「dataframe ['Brain'] 'を使ってみてください。 – vendaTrout

+0

@vendaTrout良い提案ですが、さらに多くのエラーが発生しました。私は別のものだと思いますが、それ以上のものです。 – Aedirnian

+0

それらを共有すると、回答を編集して貼り付けることができます。 – vendaTrout

答えて

0

私はほとんど解決策を得ました。次のコードは、線形回帰プロットを出力する以外はすべて行います(これは何らかの理由で欠落していますが、誰かがそれに答えることができれば!)

テキストファイルの解析を最初から10行は不気味だったので、 "skiprows = 10"、これはおそらく.rtfであり、Macでは.txtではないからです。

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

datafram = pd.read_fwf('/Users/sarahhernandez/Desktop/body_brain.rtf', delim_whitespace = True, names = ["Brain", "Body"] , usecols = ["Brain", "Body"], skiprows = 10) 

# Only used one bracket to extract data 
x = datafram['Brain'] 
y = datafram['Body'] 

# When trying to plot, I got a deprecation warning, fixed by reshaping 
x.values.reshape(1,-1) 
y.values.reshape(1,-1) 

# Converted to list and removed last value in for x and y which were "}" and NAN, respectively 
xlist = list(x) 
ylist = list(y) 
xlist.pop() 
ylist.pop() 

#Reshape lists yet again through numpy 
xvec = np.array(xlist).reshape(1,-1) 
xvec = xvec.astype(np.float) 
yvec = np.array(ylist).reshape(1,-1) 

body_reg = linear_model.LinearRegression() 
body_reg.fit(xvec, yvec) 

# Plot data, I'm just getting the scatter plot though, not the linear regression line 
plt.scatter(xvec,yvec) 
plt.plot(xvec, body_reg.predict(xvec)) 
plt.show() 
関連する問題