2
# 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?

3
  • Where do I write the float? Can I float the entire array? Commented Jun 27, 2018 at 18:17
  • 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? Commented Jun 27, 2018 at 18:18
  • I am making the second and third column in the array as the X and Y Commented Jun 27, 2018 at 18:22

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

any suggestions on how to clean my data?
There are multiple strategies, you could include a new binary indicator variable indicating whether the value in y is present or not, and then populate the missing y values with 0's, as suggested here: quora.com/…
Ive tried adding "filtered_data = ans[~np.isnan(ans["ki"])]"
but I get this error : TypeError: list indices must be integers or slices, not str

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.