值错误:预期的 2D 数组,而是获得 1D 数组:用于矩阵?



我收到以下错误,不确定为什么?值错误:预期的 2D 数组,而是获得 1D 数组: 我使用的数据集是 https://catalog.data.gov/dataset/demographic-statistics-by-zip-code-acfc9

我以为一旦它在数据帧中就已经转换为矩阵。特别是因为绘图数据正确显示。

任何帮助将不胜感激。

#implmenting KNN in python
import pandas as pd
import numpy as np
import operator
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import time
start_time = time.time()
#1) Ingest the data via one of the provided formats.
print("Getting csv")
###################################
#2.) Create a data structure to store the data.
data = pd.read_csv(r"C:Userstrave.DESKTOP-KM5AM0UDesktopUNIT 5Demographic_Statistics_By_Zip_Code.csv", usecols = ["COUNT PARTICIPANTS", "PERCENT RECEIVES PUBLIC ASSISTANCE"])
#msft.plot("JURISDICTION NAME", ["COUNT PUBLIC ASSISTANCE TOTAL", "COUNT PARTICIPANTS"], subplots=True)
# print("Entering data to datfram only 3 columns")
# print ("The number of rows is")
# print (len(data.index))
# print ("The number of columns is")
# print (len(data.columns))
print(data.head)
#################
print("running plots")
#scikit-learn
#####################################
#MATPLOT FOR REGRESSION LINE DATA
Par = data.iloc[:, 0]
Per = data.iloc[:, 1]
# Count_P = data[]
# Public_Aid = data[]

plt.title('Number of people to public aid percentage')
plt.xlabel('#Participant')
plt.ylabel('% Public aid')
plt.plot (Par, Per, 'k.')
plt.axis([.1,200,.1,1])
plt.grid(True)
print("plots complete")

###################################
print("sklearn running")
from  sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit (X=Par, y=Per)
print('SKLEARN MODULE COMPLETE')
#########################################
####making prediction
print("running prediction")
p = model.predict ([[78]]) [0][0]
print (round(p,2))


# STOP MY TIMER
print ("My program took", time.time() - start_time, "to run")

您可以修改X(在您的情况下Par( -

从:

Par = data.iloc[:, 0]

自:

Par = data.iloc[:, [0]]

用于拟合模型的y是一维的,模型的预测结果也是如此。

如果要从唯一值x进行预测,可以尝试:

p = model.predict([[78]])[0]

p 将是 1D,所以你只需要获取索引 0 一次

相关内容

  • 没有找到相关文章

最新更新