0
$\begingroup$

I am trying to fit some data inside an algorithm, but i am getting this error:

ValueError: Found input variables with inconsistent numbers of samples: [0, 6]

How i can solve this?

Here is my code bellow:

#Import all libs
from connect_db import connect_db
import pandas as pd
from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib as mpl
import datetime
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

#Database access informations
DB_CONFIG = {
    'host': 'localhost',
    'database': 'test_db',
    'user': 'root',
    'user_pass': 'root'
}

#create a date parser function 
def dateparser(s):
    return datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')

#Initialize the classifier of the algorithm
clf = RandomForestClassifier(random_state=0)

#Initialize the connection script
connect_db = connect_db(host=DB_CONFIG['host'],
                        database=DB_CONFIG['database'],
                        user=DB_CONFIG['user'],
                        password=DB_CONFIG['user_pass'])

#Create a connection
cnx = connect_db.connect()

#Create a cursor to manipulate the database
cursor = cnx.cursor()

#Create engine for converting the csv file to a sql table.
engine = connect_db.create_engine()

mpl.rcParams['agg.path.chunksize'] = 10000

#Open the csv file and pass it to a sql table
with open(Path("databases/weather_data.csv")) as file:
    #Read the file, using a function to parse the date to correct format.
    csv_data = pd.read_csv(file, parse_dates=['Date_Time'], date_parser=dateparser)

    #Convert the file to a sql database and upload the data.
    sql_data = csv_data.to_sql(name=f"{DB_CONFIG['database']}",
                               con=engine,
                               if_exists='replace',
                               chunksize=100)
    #Read the sql table
    sql_data = pd.read_sql_table('test_db', engine)

#Rearanged the columns order, for better malleability 
sql_data = sql_data[['Location','Date_Time','Temperature_C','Humidity_pct','Wind_Speed_kmh', 'Precipitation_mm']]

#Slice the data.
y = np.array(sql_data.iloc[5])
x = np.array(sql_data.loc['Temperature_C':'Wind_Speed_kmh'])

#Split the data into train data and test data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33)

#Fit the data inside the algorithm
clf.fit(x_train, y_train)

#Test the data
y_pred = clf.predict(x_test)

#Plot and show the data for visualization purposes
plt.scatter(x_test, y_test, color='red')
plt.plot(x_test, y_pred, color='blue', linewidth=2)
plt.show()
```
$\endgroup$

1 Answer 1

0
$\begingroup$

I believe you are misusing .loc[] and .iloc[]. iloc[5] gets you the sixth row, not the sixth column. For the sixth column you need to use .iloc[:, 5]. For loc I believe .loc[:, 'Temperature_C':'Wind_Speed_kmh'] would work, but not sure. Look into pandas' documentation

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.