Typeerror:需要float参数,而不是dataframe [英] Typeerror: float argument required, not dataframe
问题描述
这是我的完整代码,我无法确定corr_value应该做什么。我在最后一步收到错误TypeError:float argument required,not DataFrame。任何想法都会非常有用
This is my complete code and I am not able to determine what should be done for corr_value. I am getting error "TypeError: float argument required, not DataFrame" at the last step. Any idea will be highly helpful
from sklearn.datasets import load_boston
import pandas as pd
boston = load_boston()
dataset = pd.DataFrame(data=boston.data, columns=boston.feature_names)
dataset['target'] = boston.target
print(dataset.head())
X = dataset.drop('target', axis = 1 )
Y = dataset["target"]
#Now the dataframe X has just the features that influence the target
#print the correlation matrix for dataframe X. Use '.corr()' function to compute #correlation matrix
#from the correlation matrix note down the correlation value between 'CRIM' and #'PTRATIO' and assign it to variable 'corr_value'
corr_value = X.corr()
print(corr_value.head())
import statsmodels.api as sm
statsModel = sm.OLS(Y, X)
fittedModel = statsModel.fit()
print(fittedModel.summary())
OLS回归结果
= ================================================== ===========================
Dep。变量:目标R平方:0.959
型号:OLS Adj。 R平方:0.958
方法:最小二乘法F统计:891.1
日期:星期一,2018年7月2日Prob(F-statistic):0.00
时间:22:36:03对数 - 可能性:-1523.8
编号观察:506 AIC:3074。
Df残差:493 BIC:3129。 br />
Df型号:13
协方差类型:nonrobust
=================== ================================================== =========
coef std err t P> | t | [0.025 0.975]
-------------------------------------- ----------------------------------------
CRIM - 0.0916 0.034 -2.675 0.008 -0.159 -0.024
ZN 0.0487 0.014 3.379 0.001 0.020 0.077
INDUS -0.0038 0.064 -0.059 0.953 -0.130 0.123
CHAS 2.8564 0.904 3.160 0.002 1.080 4.633
NOX -2.8808 3.359 -0.858 0.392 -9.481 3.720
RM 5.9252 0.309 19.168 0.000 5.318 6.533
年龄 - 0.0072 0.014 -0.523 0.601 -0.034 0.020
DIS -0.9680 0.196 -4.947 0.000 -1.352 -0.584
RAD 0.1704 0.067 2.554 0.011 0.039 0.302
TAX -0.0094 0.004 -2.393 0.017 -0.017 -0.002
PTRATIO -0.3924 0.110 -3.571 0.000 -0.608 -0.177
B 0.0150 0.003 5.561 0.000 0.010 0.020
LSTAT -0.4170 0.051 -8.214 0.000 -0.517 -0.317
============================ ==================================================
Omnibus:204.050 Durbin-Watson:0.999
Prob(Omnibus):0.000 Jarque-Bera( JB):1372.527
歪斜:1.609 Prob(JB):9.11e-299
峰值:10.399 Cond。 No. 8.50e + 03
==================================== ==========================================
警告:
[1]标准错误假设错误的协方差矩阵被正确指定。
[2]条件数是大,8.5e + 03。这可能表明存在强烈的多重共线性或其他数值问题。
r_squared = fitsModel.rsquared
###结束代码(约1行)
打开(output.txt,w)为text_file:
text_file.write(corr =%f \ n%corr_value)
text_file.write(rsquared =%f \ n%r_squared)
OLS Regression Results
==============================================================================
Dep. Variable: target R-squared: 0.959
Model: OLS Adj. R-squared: 0.958
Method: Least Squares F-statistic: 891.1
Date: Mon, 02 Jul 2018 Prob (F-statistic): 0.00
Time: 22:36:03 Log-Likelihood: -1523.8
No. Observations: 506 AIC: 3074.
Df Residuals: 493 BIC: 3129.
Df Model: 13
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
CRIM -0.0916 0.034 -2.675 0.008 -0.159 -0.024
ZN 0.0487 0.014 3.379 0.001 0.020 0.077
INDUS -0.0038 0.064 -0.059 0.953 -0.130 0.123
CHAS 2.8564 0.904 3.160 0.002 1.080 4.633
NOX -2.8808 3.359 -0.858 0.392 -9.481 3.720
RM 5.9252 0.309 19.168 0.000 5.318 6.533
AGE -0.0072 0.014 -0.523 0.601 -0.034 0.020
DIS -0.9680 0.196 -4.947 0.000 -1.352 -0.584
RAD 0.1704 0.067 2.554 0.011 0.039 0.302
TAX -0.0094 0.004 -2.393 0.017 -0.017 -0.002
PTRATIO -0.3924 0.110 -3.571 0.000 -0.608 -0.177
B 0.0150 0.003 5.561 0.000 0.010 0.020
LSTAT -0.4170 0.051 -8.214 0.000 -0.517 -0.317
==============================================================================
Omnibus: 204.050 Durbin-Watson: 0.999
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1372.527
Skew: 1.609 Prob(JB): 9.11e-299
Kurtosis: 10.399 Cond. No. 8.50e+03
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 8.5e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
r_squared = fittedModel.rsquared
###End code(approx 1 line)
with open("output.txt", "w") as text_file:
text_file.write("corr= %f\n"% corr_value)
text_file.write("rsquared= %f\n" % r_squared)
TypeError Traceback (most recent call last)
<ipython-input-11-b55e673c78db> in <module>()
3 ###End code(approx 1 line)
4 with open("output.txt", "w") as text_file:
----> 5 text_file.write("corr= %f\n" % corr_value)
6 text_file.write("rsquared= %f\n" % r_squared)
TypeError: float argument required, not DataFrame
我的尝试:
What I have tried:
text_file.write("corr= %f\n"
推荐答案
错误非常清楚:
corr_value
类型为DataFrame
,而%f格式要求参数的类型为float
。
即使代码中的注释也表明corr()
函数计算矩阵,你必须用它来确定相关值。
我无法回答怎么做,因为我不知道函数在做什么,矩阵是如何组织的,以及成员代表什么。
The error is quite clear:
corr_value
is of typeDataFrame
while the "%f" format requires the argument to be of typefloat
.
Even the comment in your code states that thecorr()
function computes a matrix and you have to use that to determine the correlation value.
I can't answer how to do that because I don't know what the functions are doing, how that matrix is organised, and what the members represent.
这篇关于Typeerror:需要float参数,而不是dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!