简体   繁体   English

将数据集中的特征添加到函数中会导致“TypeError: can't convert type 'ndarray' to numerator/denominator”

[英]Adding a feature from a dataset into a function causes “TypeError: can't convert type 'ndarray' to numerator/denominator”

The task requires you to load a feature of the diabetes dataset and write your own line of best fit for the training data.该任务要求您加载糖尿病数据集的一个特征,并为训练数据编写自己的最佳拟合线。

I have written the required line of best fit algorithm, however when trying to add the training data to it, I receive this error:我已经编写了所需的最佳拟合算法行,但是在尝试向其中添加训练数据时,我收到此错误:

"TypeError: can't convert type 'ndarray' to numerator/denominator" “类型错误:无法将类型 'ndarray' 转换为分子/分母”

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from statistics import mean

diabetes = datasets.load_diabetes()
diabetes_X = diabetes.data[:, np.newaxis, 2]

diabetes_X_train = diabetes_X[:-20] #creating the testing and training data 
diabetes_X_test = diabetes_X[-20:]

diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]

## The below code is where the issue is occurring 

xs = np.array(diabetes_X_train, dtype=np.float64)
ys = np.array([diabetes_y_train, dtype=np.float64)

##the algorithm to calculate the line of best  

def best_fit_slope_and_intercept(xs,ys):
    m = (((mean(xs)*mean(ys)) - mean(xs*ys)) /
         ((mean(xs)*mean(xs)) - mean(xs*xs)))

    b = mean(ys) - m*mean(xs)

    return m, b

m, b = best_fit_slope_and_intercept(xs,ys)

print(m,b)

I understand converting the required data into the correct format is the issue but after doing research, I am unable to find the correct way to do so.我了解将所需数据转换为正确格式是问题所在,但经过研究,我无法找到正确的方法。

All input on how to correctly concatenate or convert the training data as required is appreciated.感谢有关如何根据需要正确连接或转换训练数据的所有输入。

the best format for dealing with data is DataFrame, in my view.在我看来,处理数据的最佳格式是 DataFrame。 you could easily make your dataframe like this:您可以轻松地制作这样的数据框:

urdataframe={'headername': bestfitted}
urdataframe=pd.DataFrames(data=urdataframe)

then you can contact ur dataframes easily like :然后您可以轻松联系您的数据帧,例如:

finaldata=pd.concat((traindata,urdataframe),axis=1)

you could do all machine learning functions on DataFrames and less likely to get errors您可以在 DataFrames 上执行所有机器学习功能,并且不太可能出错

for example, if ur train data is something like: age sex sugar 42 1 120 45 0 250 32 1 98例如,如果你的火车数据是这样的:年龄性别糖 42 1 120 45 0 250 32 1 98

and your answer is like: answer yes no yes你的回答是这样的:回答是的,不是的,是的

so after contact by the code, I mentioned it would be like: age sex sugar answer 42 1 120 yes 45 0 250 no 32 1 98 yes所以在通过代码联系后,我提到它会像:年龄性别糖回答 42 1 120 是 45 0 250 否 32 1 98 是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM