从csv获取熊猫系列

时间:2017-06-01 21:22:46

标签: python pandas machine-learning scikit-learn sklearn-pandas

我是机器学习的新手,我目前正在玩MNIST机器学习,使用RandomForestClassifier。

我使用sklearn和熊猫。 我有一个训练CSV数据集。

import pandas as pd
import numpy as np
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

train = pd.read_csv("train.csv")
features = train.columns[1:]
X = train[features]
y = train['label']

user_train = pd.read_csv("input.csv")
user_features = user_train.columns[1:]
y_train = user_train[user_features]
user_y = user_train['label']


X_train, X_test, y_train, y_test = model_selection.train_test_split(X/255.,y,test_size=1,random_state=0)

clf_rf = RandomForestClassifier()
clf_rf.fit(X_train, y_train)
y_pred_rf = clf_rf.predict(X_test)
acc_rf = accuracy_score(y_test, y_pred_rf)

print("pred : ", y_pred_rf)
print("random forest accuracy: ",acc_rf)

我有当前的代码,效果很好。它需要训练集,分割并采用一个元素进行测试,并进行预测。

我现在想要的是使用来自输入的测试数据,我有一个名为" input.csv"的新csv,我想预测这个csv中的值。

如何用输入数据替换model_selection.train_test_split? 我确信反应非常明显,我找不到任何东西。

1 个答案:

答案 0 :(得分:2)

代码的以下部分未使用

user_train = pd.read_csv("input.csv")
user_features = user_train.columns[1:]
y_train = user_train[user_features]
user_y = user_train['label']

如果input.csv具有与train.csv相同的结构,您可能需要:

  • 训练分类器并在input.csv数据集的分割上进行测试:(请参考http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html以了解如何设置测试大小)

    input_train = pd.read_csv("input.csv")    
    input_features = user_train.columns[1:]    
    input_data = user_train[input_features]    
    input_labels = user_train['label']
    data_train, data_test, labels_train, labels_test = model_selection.train_test_split(input_data/255.,input_labels,test_size=1,random_state=0)
    
    clf_rf = RandomForestClassifier()
    clf_rf.fit(data_train, labels_train)
    labels_pred_rf = clf_rf.predict(data_test)
    acc_rf = accuracy_score(labels_test, labels_pred_rf)
    
  • 在整个input.csv文件中测试以前训练过的分类器

    input_train = pd.read_csv("input.csv")    
    input_features = user_train.columns[1:]    
    input_data = user_train[input_features]    
    input_labels = user_train['label']
    
    labels_pred_rf = clf_rf.predict(input_data)
    acc_rf = accuracy_score(input_labels, labels_pred_rf)