修复脚本中的浮动错误(需要帮助)Python

时间:2018-06-27 18:10:17

标签: python-3.x

# 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()

存储在数组中的数据:

[['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']]

错误输出/回溯

  

runfile('C:/Users/Aidan/.spyder-py3/temp.py',   wdir ='C:/Users/Aidan/.spyder-py3')追溯(最近一次通话结束):

     

文件“”,第1行,在       运行文件('C:/Users/Aidan/.spyder-py3/temp.py',wdir ='C:/Users/Aidan/.spyder-py3')

     

文件   “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,   运行文件中的第705行       execfile(文件名,命名空间)

     

文件   “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,   第102行,在execfile中       exec(compile(f.read(),文件名,'exec'),命名空间)

     

文件“ C:/Users/Aidan/.spyder-py3/temp.py”,第32行,在       lin_regression.fit(x,y)

     

文件   “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ sklearn \ linear_model \ base.py”,   489号线,适合       copy = self.copy_X,sample_weight = sample_weight)

     

文件   “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ sklearn \ linear_model \ base.py”,   _preprocess_data中的第169行       y = np.asarray(y,dtype = X.dtype)

     

文件   “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ numpy \ core \ numeric.py”,   492行,呈数组形式       返回数组(a,dtype,copy = False,order = order)

     

ValueError:无法将字符串转换为浮点数:


如何解决浮动错误?

1 个答案:

答案 0 :(得分:0)

问题是''无法转换为浮点数。您需要先清除数据,然后再应用lin_regression.fit(x,y)