2016-09-06 72 views
1
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import style 
from sklearn.linear_model import LinearRegression 
from sklearn import preprocessing, cross_validation, svm 


df = pd.read_csv('table.csv') 
print (df.head()) 

df = df[['Price', 'Asset']] 
x = np.array(df.Price) 
y = np.array(df.Asset) 

x_train, x_test, y_train, y_test = cross_validation.train_test_split(
x, y, test_size=0.2) 


x_train = np.pad(x, [(0,0)], mode='constant') 
x_train.reshape((23,1)) 


y_train = np.pad(y, [(0,0)], mode ='constant') 
y_train.reshape((23,1)) 

np.reshape(-1, 1) 

錯誤:NumPy的重塑問題

runfile('C:/Users/HP/Documents/linear.py', wdir='C:/Users/HP/Documents') 
     Price  Asset 
0 87.585859 191 
1 87.839996 232 
2 87.309998 245 
3 88.629997 445 
4 88.379997 393 
C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: 

    DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. 
    DeprecationWarning) 
Traceback (most recent call last): 

    File "<ipython-input-124-030ffa933525>", line 1, in <module> 
    runfile('C:/Users/HP/Documents/linear.py', wdir='C:/Users/HP/Documents') 

    File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile 
    execfile(filename, namespace) 

    File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "C:/Users/HP/Documents/linear.py", line 38, in <module> 
    clf.fit(x_train, y_train) 

    File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 427, in fit 
    y_numeric=True, multi_output=True) 

    File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 520, in check_X_y 
    check_consistent_length(X, y) 

    File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 176, in check_consistent_length 
    "%s" % str(uniques)) 

ValueError: Found arrays with inconsistent numbers of samples: [ 1 23] 

我的數據框尺寸:23,2

我填補我x_train和y_train爲[23.1]因爲我得到這個初始誤差ValueError異常:發現樣本數不一致的陣列:[1 18]。 填充後我的錯誤消息:ValueError:發現樣本數量不一致的數組:[1 23]。

然後我試圖重塑它,仍然收到錯誤信息:ValueError:發現樣本數不一致的陣列:[1 23]。

我該如何解決這個問題?

+0

你想重塑什麼? 'np.reshape'不知道你想要重塑的東西。像'array.reshape((x,y))'一樣使用它。 – Ian

+0

@mwormser說,你需要在數組對象上調用'reshape',比如'x_train'和'y_train'; 'x_train.reshape((23,1))' – Dartmouth

+0

嘗試仍然收到錯誤消息:ValueError:找到的數組樣本數不一致:[1 23] – Bolajio

回答

0

如果你只想重塑從大小(x, 1)數組(1, x)可以使用np.transposenumpy.ndarray.T功能:

x_train = x_train.T 
y_train = np.transpose(y_train) 

都達到同樣的效果。

編輯:這隻適用於一維數組。使用重塑更高維數組。

如果您不給我們提供完整的回溯,它顯示錯誤發生在哪一行,我們不能幫助你更詳細。

+1

轉置和重塑** _不要做同樣的事情!嘗試'a = np.arange(6).reshape((2,3)); b = a.reshape((3,2)); c = a.T; np.all(b == c)'。請在發佈之前將它的答案寫出來! – Praveen

+0

@Praveen謝謝! – Bolajio

+0

@Praveen當然你是對的,我只想到一個維度是1的情況,我會編輯它。 – Ian