我跟随Siraj Raval的关于如何制作神经网络的视频系列(https://www.youtube.com/playlist?list=PL2-dafEMk2A7YdKv4XfKpfbTH5z6rEEj3),我在实际获得程序方面遇到了很多麻烦我创建的文本文档中的数据值。我想知道是否可能是因为我错过了一个依赖关系或者说它不正确
这是我在命令提示符中遇到的错误:
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 :(得分:0)
我几乎得到了解决方案。以下代码执行除打印线性回归图之外的所有内容(由于某些原因,它只是缺失了 - 如果有人能够回答那将是很棒的!)
我不得不对文本文件进行大量解析,因为前十行是乱码,因此“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()