Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据规范化后如何使用K-最近邻(KNN)模型进行预测(Python)_Python_Scikit Learn_Classification_Data Science_Nearest Neighbor - Fatal编程技术网

数据规范化后如何使用K-最近邻(KNN)模型进行预测(Python)

数据规范化后如何使用K-最近邻(KNN)模型进行预测(Python),python,scikit-learn,classification,data-science,nearest-neighbor,Python,Scikit Learn,Classification,Data Science,Nearest Neighbor,我在Python Module=Scikitlearn中创建了一个KNN模型,使用三个变量年龄、距离、旅行津贴作为预测变量,目的是使用它们预测目标变量旅行方法的结果 在构建模型时,我必须对三个预测变量(年龄、距离、旅行津贴)的数据进行标准化。与不规范化数据相比,这提高了模型的准确性 现在我已经建立了模型,我想做一个预测。但是,我如何输入预测变量来进行预测,因为模型已经在标准化数据上进行了训练 我想输入KNN.predict[[302000,40]]来执行年龄=30的预测;距离=2000;津贴=4

我在Python Module=Scikitlearn中创建了一个KNN模型,使用三个变量年龄、距离、旅行津贴作为预测变量,目的是使用它们预测目标变量旅行方法的结果

在构建模型时,我必须对三个预测变量(年龄、距离、旅行津贴)的数据进行标准化。与不规范化数据相比,这提高了模型的准确性

现在我已经建立了模型,我想做一个预测。但是,我如何输入预测变量来进行预测,因为模型已经在标准化数据上进行了训练

我想输入KNN.predict[[302000,40]]来执行年龄=30的预测;距离=2000;津贴=40。但由于数据已经标准化,我想不出一个方法来实现这一点。我使用以下代码规范化数据:
X=preprocessing.StandardScaler.fitX.transformX.astypefloat

实际上,答案隐藏在您提供的代码中

安装preprocessing.StandardScaler实例后,它会记住如何缩放数据。试试这个

scaler = preprocessing.StandardScaler().fit(X)
# scaler is an object that knows how to normalize data points
X_normalized = scaler.transform(X.astype(float))
# used scalar to normalize the data points in X
# Note, this is what you have done, just in two steps. 
# I just capture the scaler object 
#
# ... Train your model on X_normalized
#
# Now predict
other_data = [[30,2000,40]]
other_data_normalized = scaler.transform(other_data)
KNN.predict(other_data_normalized)
请注意,我以相同的方式使用了scaler.transform两次


请参见

嗨,迈克尔-这很好用,谢谢!我不知道Python还记得数据规范化的方式。