Reputation: 23
I am trying this NCAA basketball prediction program and I keep getting this error:
Traceback (most recent call last):
File "/mnt/chromeos/removable/JACKS JUNK/Chatbot_2/sports_predict.py", line 17, in <module>
X_train, X_test, y_train, y_test = train_test_split(X, y)
File "/home/jackmdavis06/.local/lib/python3.5/site-packages/sklearn/model_selection/_split.py", line 2116, in train_test_split
arrays = indexable(*arrays)
File "/home/jackmdavis06/.local/lib/python3.5/site-packages/sklearn/utils/validation.py", line 237, in indexable
check_consistent_length(*result)
File "/home/jackmdavis06/.local/lib/python3.5/site-packages/sklearn/utils/validation.py", line 212, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [2258, 4148]
This is my code:
import pandas as pd
from sportsreference.ncaab.teams import Teams
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
FIELDS_TO_DROP = ['away_points', 'home_points', 'date', 'location',
'losing_abbr', 'losing_name', 'winner', 'winning_abbr',
'winning_name', 'home_ranking', 'away_ranking']
teams = Teams()
dataset = pd.read_csv('data.csv')
X = dataset.drop(FIELDS_TO_DROP, 1).dropna().drop_duplicates()
y = dataset[['home_points', 'away_points']].values
X_train, X_test, y_train, y_test = train_test_split(X, y)
parameters = {'bootstrap': False,
'min_samples_leaf': 3,
'n_estimators': 50,
'min_samples_split': 10,
'max_features': 'sqrt',
'max_depth': 6}
model = RandomForestRegressor(**parameters)
model.fit(X_train, y_train)
print(model.predict(X_test).astype(int), y_test)
I followed the guide on this website:
https://towardsdatascience.com/predict-college-basketball-scores-in-30-lines-of-python-148f6bd71894
I tweaked the code a little bit to make it work faster, so I tried running the original code and the original code only and I got the same exact error. Please help! Thanks!
Upvotes: 1
Views: 86
Reputation: 15568
You dropped nulls and duplicates for X, but not y.
If you print(X.shape[0], len(y))
, you will see that they have different values.
You should do something like:
#...
dataset = pd.read_csv('data.csv')
# drop nulls and dublicates
# use fields to keep for your analysis both features and target
# e.g. FIELDS_TO_KEEP = ['a', 'b' ...]
dataset = dataset[FIELDS_TO_KEEP].dropna().drop_duplicates()
# get your feature X, target y
X = dataset[FIELDS_THAT_ARE_FEATURES]
y = dataset[['home_points', 'away_points']]
# ...
Upvotes: 1