如何检查 sklearn 中预测值的特征值
How can I check feature values for a predict value in sklearn
我在做一个二元分类,预测值是0和1,有什么方法可以得到一个预测值的特征值。
例如:
我有 2 个特征 'Age' 和 'Salary',目标值为 'purchased'。
购买年龄工资
19 19000 0
35 20000 0
27 30000 1
41 29000 1
65 40000 1
所以,我想知道每个测试用例结果(0 或 1)的特征值(年龄和薪水)是多少。
import pandas as pd
df = pd.read_csv('data.csv')
x = df.iloc[:,[0,1]]
y = df.iloc[:,2]
from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test =train_test_split(x,y,test_size=0.25,random_state=0)
from sklearn.linear_model import LogisticRegression
regressor = LogisticRegression()
regressor.fit(x_train,y_train)
y_pred=regressor.predict(x_test)
根据您的说明,您只想将它们放在相同的数据结构中。你可以concatenate two dataframes with pandas. But you need to put the predictions within a dataframe with the appropriate index。这是代码:
y_pred_df = pd.DataFrame(y_pred, index=x_test.index)
pd.concat([x_test, y_pred_df], axis=1)
我在做一个二元分类,预测值是0和1,有什么方法可以得到一个预测值的特征值。
例如: 我有 2 个特征 'Age' 和 'Salary',目标值为 'purchased'。 购买年龄工资 19 19000 0 35 20000 0 27 30000 1 41 29000 1 65 40000 1
所以,我想知道每个测试用例结果(0 或 1)的特征值(年龄和薪水)是多少。
import pandas as pd
df = pd.read_csv('data.csv')
x = df.iloc[:,[0,1]]
y = df.iloc[:,2]
from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test =train_test_split(x,y,test_size=0.25,random_state=0)
from sklearn.linear_model import LogisticRegression
regressor = LogisticRegression()
regressor.fit(x_train,y_train)
y_pred=regressor.predict(x_test)
根据您的说明,您只想将它们放在相同的数据结构中。你可以concatenate two dataframes with pandas. But you need to put the predictions within a dataframe with the appropriate index。这是代码:
y_pred_df = pd.DataFrame(y_pred, index=x_test.index)
pd.concat([x_test, y_pred_df], axis=1)