簡體   English   中英

我有這個錯誤:IndexError: index 40 is out of bounds for axis 0 with size 40,我無法理解錯誤的根源。 請

[英]i have this error: IndexError: index 40 is out of bounds for axis 0 with size 40, i can't understand the fount of the error. pls

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

data = pd.read_csv("student-mat.csv"  , sep=";")
data = data[["age", "traveltime" , "studytime" ,"failures" , "G1" , "G2" , "G3"]]

predict = "age"

x = np.array(data.drop([predict], 1))
y = np.array(data[predict])

x_train , x_test , y_train , y_test =  sklearn.model_selection.train_test_split(x , y, test_size= 0.1)

linear=linear_model.LinearRegression()
linear.fit(x,y)

acc = linear.score(x_test , y_test)
print(acc)

print('co: \n', linear.coef_)
print('intercept: \n', linear.intercept_)

predictions = linear.predict(x_train)

for x in range(len(predictions)):
    print(predictions[x], x_test[x], y_test[x])

style.use("ggplot")
pyplot.scatter(data["age"] , data["failures"])
pyplot.xlabel("traveltime")
pyplot.ylabel("studytime")
pyplot.show()

伙計們,我在互聯網上搜索了很多關於此錯誤的信息,但我沒有在此行找到任何錯誤:print(predictions[x], x_test[x], y_test[x]), 我不能了解錯誤來自

這里的問題是預測有 355 個條目,因為您正在訓練集上進行預測。 預測測試集,然后您的代碼將正常工作。

predictions = linear.predict(x_test)

for x in range(len(predictions)):
    print(predictions[x], x_test[x], y_test[x])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM