2
votes
# Python code to demonstrate SQL to fetch data.

# importing the module
import sqlite3
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from scipy.stats import chisquare

# connect withe the myTable database
connection = sqlite3.connect(r"C:\Users\Aidan\Desktop\CEP_DB.db")

# cursor object
crsr = connection.cursor()


dog= crsr.execute("Select s, ei, ki FROM cep_db_lite1_vc WHERE s IN ('d')")
ans= crsr.fetchall() 

dogData = np.array(ans)
FdogData= dogData[:, [1,2]]
x, y = FdogData[:,0], FdogData[:,1]

# Reshaping
x, y = x.reshape(-1,1), y.reshape(-1, 1)

# Linear Regression Object 
lin_regression = LinearRegression()

# Fitting linear model to the data
lin_regression.fit(x,y)

# Get slope of fitted line
m = lin_regression.coef_

# Get y-Intercept of the Line
b = lin_regression.intercept_

# Get Predictions for original x values
# you can also get predictions for new data
predictions = lin_regression.predict(x)
chi= chisquare(predictions, y)

# following slope intercept form 
print ("formula: y = {0}x + {1}".format(m, b)) 
print(chi)

# Plot the Original Model (Black) and Predictions (Blue)
plt.scatter(x, y,  color='black')
plt.plot(x, predictions, color='blue',linewidth=3)
plt.show()

Data stored in the array:

[['d' '-72.70' '3.20']

['d' '-74.81' '']

['d' '-87.60' '5.50']

['d' '-91.38' '']

['d' '-71.80' '']

['d' '-73.10' '']

['d' '-81.20' '']

['d' '-81.40' '']

['d' '-75.70' '5.70']

['d' '-83.50' '5.10']

['d' '-73.90' '']

['d' '-82.60' '']

['d' '-77.30' '']

['d' '-85.10' '']

['d' '-79.70' '']

['d' '-78.70' '']

['d' '-77.90' '']

['d' '-76.80' '']

['d' '-83.80' '']

['d' '-83.90' '']

['d' '-82.00' '4.90']

['d' '-80.00' '4.80']]

error output/traceback

runfile('C:/Users/Aidan/.spyder-py3/temp.py', wdir='C:/Users/Aidan/.spyder-py3') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Aidan/.spyder-py3/temp.py', wdir='C:/Users/Aidan/.spyder-py3')

File "C:\Users\Aidan\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Aidan\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Aidan/.spyder-py3/temp.py", line 32, in lin_regression.fit(x,y)

File "C:\Users\Aidan\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 489, in fit copy=self.copy_X, sample_weight=sample_weight)

File "C:\Users\Aidan\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 169, in _preprocess_data y = np.asarray(y, dtype=X.dtype)

File "C:\Users\Aidan\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 492, in asarray return array(a, dtype, copy=False, order=order)

ValueError: could not convert string to float:


How do I fix the float error?

1
Where do I write the float? Can I float the entire array?Aidan Darragh
Without more information, we can only guess why you are receiving the ValueError. What are the values of x and y that are being used in the failing call?David Harris
I am making the second and third column in the array as the X and YAidan Darragh

1 Answers

0
votes

The problem is '' cannot be converted to a float. You need to clean your data before applying lin_regression.fit(x,y).