我想学习机器学习,偶然发现了Youtube上的Siraj和他在Udacity的视频,想尝试学习一些东西。
他的视频链接: https://www.youtube.com/watch?v=vOppzHpvTiQ&index=1&list=PL2-dafEMk2A7YdKv4XfKpfbTH5z6rEEj3
在他的视频中,他有一个txt文件被导入并读取,但当我试图重新创建这个txt文件时,它无法正确读取。相反,我尝试使用相同数据创建pandas dataframe并对其执行线性回归/预测,但是我得到了下面的错误。
发现输入变量具有不一致的样本数量:[1,16],还有一些关于传递1d数组的内容,并且需要对它们进行重塑。
然后,当我尝试按照这篇文章进行重塑时:Sklearn : ValueError: Found input variables with inconsistent numbers of samples: [1, 6]
我得到了这个错误...
shapes (1, 16) and (1, 1) not aligned: 16 (dim 1) != 1 (dim 0)
以下是我的代码。我知道它可能存在语法错误,因为我对这个scklearn库还不熟悉,希望得到一些帮助。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
#DF = pd.read_fwf('BrainBodyWeight.txt')
DF = pd.DataFrame()
DF['Brain'] = [3.385, .480, 1.350, 465.00,36.330, 27.660, 14.830, 1.040, 4.190, 0.425, 0.101, 0.920, 1.000, 0.005, 0.060, 3.500 ]
DF['Body'] = [44.500, 15.5, 8.1, 423, 119.5, 115, 98.2, 5.5,58, 6.40, 4, 5.7,6.6, .140,1, 10.8]
try:
x = DF['Brain']
y = DF['Body']
x = x.tolist()
y = y.tolist()
x = np.asarray(x)
y = np.asarray(y)
body_reg = linear_model.LinearRegression()
body_reg.fit(x.reshape(-1,1),y.reshape(-1,1))
plt.scatter(x,y)
plt.plot(x,body_reg.predict(x))
plt.show()
except Exception as e:
print(e)
有人能解释一下为什么sklearn不喜欢我的输入吗????
plt.plot(x,body_reg.predict(x))
这一行中遇到了一个错误。但是可以通过在此处使用reshape(-1,1)
来进行更正。 - Vivek Kumar