Как я могу решить для предсказанных вероятностей, используя классификатор Knn? - PullRequest
0 голосов
/ 23 апреля 2020

Я использую классификатор KNN для набора данных и пытаюсь найти прогнозируемые вероятности для каждого результата прогноза, и я не уверен, как go узнать об этом. Я не нашел много на этом топи c. Код, который я использую:

import numpy as np ##Import necassary packages
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import *
from sklearn import preprocessing
from sklearn import neighbors
from sklearn.metrics import *
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
from sklearn.cluster import MiniBatchKMeans 
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix

url2="http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" #Reading in Data from a freely and easily available source on the internet
Adult = pd.read_csv(url2, header=None, skipinitialspace=True) #Decoding data by removing extra spaces in cplumns with skipinitialspace=True
##Assigning reasonable column names to the dataframe
Adult.columns = ["age","workclass","fnlwgt","education","educationnum","maritalstatus","occupation",  
                 "relationship","race","sex","capitalgain","capitalloss","hoursperweek","nativecountry",
                 "less50kmoreeq50kn"]
Adult.loc[Adult.loc[:, "race"] == "Amer-Indian-Eskimo", "race"] = "Other" #consolidating catagorical data in the race column

Adult.loc[:,"race"].value_counts().plot(kind='bar') #plotting the consolidated catagorical data in the race column
plt.title('race after consolidation')
plt.show()

Adult.loc[:, "White"] = (Adult.loc[:, "race"] == "White").astype(int) #One hot encoding the catagorical/creating new categorical data in the race column
Adult.loc[:, "Black"] = (Adult.loc[:, "race"] == "Black").astype(int)
Adult.loc[:, "Asian-Pac-Islander"] = (Adult.loc[:, "race"] == "Asian-Pac-Islander").astype(int)
Adult.loc[:, "Other"] = (Adult.loc[:, "race"] == "Other").astype(int)

Adult.loc[:,"Other"] #Verifying One-hot encoding for Other column

Adult = Adult.drop("race", axis=1) #removing the obsolete column "race"

Minage = min(Adult.loc[:,"age"])  #MinMax normilizing the age column
Maxage = max(Adult.loc[:,"age"])
MinMaxage = (Adult.loc[:,"age"] - Minage)/(Maxage - Minage)

df2 = pd.DataFrame() #creating a dataframe to plot the normilized data
df2.loc[:,0] = Adult.loc[:, "White"] #filling the data frame
df2.loc[:,1] = NormZ1

df2.loc[:,1] = MinMaxage #assigning new columns for df2
df2.loc[:,2] = Adult.loc[:,"hoursperweek"]

df2.columns = ["White","MinMaxage","hoursperweek"] #labeling the columns for df2

df2.head() #checkiung new dataframe

X = np.array(df2.drop(["hoursperweek"], 1)) #choosing the expert label to predict and not including the label in the X array
y = np.array(df2["hoursperweek"])

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2) #splittting the data into training and testing data
clf = neighbors.KNeighborsClassifier() #assigning K neighbors classifier
clf.fit(X_train, y_train) #fitting the data for X_train and y_train
accuracy = clf.score(X_test, y_test) #finding the accuracy of the prediction
print("accuracy rate with age MinMax Normilized")
print(accuracy)
print ('predictions for test set with age MinMax Normilized:') #showing results
print(clf.predict(X_test)) 
print ('actual class values with age MinMax Normilized:')
print(y_test)

Я загрузил фактический результат и прогнозируемый результат в новый фрейм данных и хотел бы добавить 3-й столбец в новый фрейм данных с прогнозируемыми вероятностями для каждой строки , но я не уверен, как решить для них в python. Есть ли способ решить для предсказанных вероятностей для каждого результата? Я хотел бы использовать предсказанные вероятности для матрицы путаницы и кривой RO C.

Ответы [ 2 ]

0 голосов
/ 05 мая 2020

из указанного выше кода, я удалил

df2.loc[:,1] = NormZ1

и повторно запустил код, использовал синтаксис

print(clf.predict_proba(X_test))

смог получить вероятности в форме (6513 , 93)

0 голосов
/ 23 апреля 2020

вы можете попробовать clf.predict_boba(X_test), чтобы получить вероятность предсказания. источник

...