Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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
Python 如何生成带有特定条目的Kaggle提交CSV文件?_Python_Pandas_Machine Learning_Scikit Learn_Kaggle - Fatal编程技术网

Python 如何生成带有特定条目的Kaggle提交CSV文件?

Python 如何生成带有特定条目的Kaggle提交CSV文件?,python,pandas,machine-learning,scikit-learn,kaggle,Python,Pandas,Machine Learning,Scikit Learn,Kaggle,我是机器学习的初学者,我正试图通过卡格尔的巨大问题来学习。我已经完成了我的代码,并且获得了0.78的准确分数,但是现在我需要生成一个CSV文件,其中包含418个条目+一个标题行,但是idk知道如何操作 这是我应该制作的一个例子: PassengerId,Survived 892,0 893,1 894,0 Etc. 数据来自我的test\u预测 这是我的代码: import pandas as pd from sklearn.tree import DecisionTreeClassi

我是机器学习的初学者,我正试图通过卡格尔的巨大问题来学习。我已经完成了我的代码,并且获得了0.78的准确分数,但是现在我需要生成一个CSV文件,其中包含418个条目+一个标题行,但是idk知道如何操作

这是我应该制作的一个例子:

PassengerId,Survived
 892,0
 893,1
 894,0
 Etc.
数据来自我的
test\u预测

这是我的代码:

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

"""Assigning the train & test datasets' adresses to variables"""
train_path = "C:\\Users\\Omar\\Downloads\\Titanic Data\\train.csv"
test_path = "C:\\Users\\Omar\\Downloads\\Titanic Data\\test.csv"

"""Using pandas' read_csv() function to read the datasets
and then assigning them to their own variables"""
train_data = pd.read_csv(train_path)
test_data = pd.read_csv(test_path)

"""Using pandas' factorize() function to represent genders (male/female)
with binary values (0/1)"""
train_data['Sex'] = pd.factorize(train_data.Sex)[0]
test_data['Sex'] = pd.factorize(test_data.Sex)[0]

"""Replacing missing values in the training and test dataset with 0"""
train_data.fillna(0.0, inplace = True)
test_data.fillna(0.0, inplace = True)

"""Selecting features for training"""
columns_of_interest = ['Pclass', 'Sex', 'Age']

"""Dropping missing/NaN values from the training dataset"""
filtered_titanic_data = train_data.dropna(axis=0)

"""Using the predictory features in the data to handle the x axis"""
x = filtered_titanic_data[columns_of_interest]

"""The survival (what we're trying to find) is the y axis"""
y = filtered_titanic_data.Survived

"""Splitting the train data with test"""
train_x, val_x, train_y, val_y = train_test_split(x, y, random_state=0)

"""Assigning the DecisionClassifier model to a variable"""
titanic_model = DecisionTreeClassifier()

"""Fitting the x and y values with the model"""
titanic_model.fit(train_x, train_y)

"""Predicting the x-axis"""
val_predictions = titanic_model.predict(val_x)

"""Assigning the feature columns from the test to a variable"""
test_x = test_data[columns_of_interest]

"""Predicting the test by feeding its x axis into the model"""
test_predictions = titanic_model.predict(test_x)

"""Printing the prediction"""
print(val_predictions)

"""Checking for the accuracy"""
print(accuracy_score(val_y, val_predictions))

"""Printing the test prediction"""
print(test_predictions)
这个怎么样:

submission = pd.DataFrame({ 'PassengerId': test_data.passengerid.values, 'Survived': test_predictions })
submission.to_csv("my_submission.csv", index=False)
这个怎么样:

submission = pd.DataFrame({ 'PassengerId': test_data.passengerid.values, 'Survived': test_predictions })
submission.to_csv("my_submission.csv", index=False)


问题是什么?你的解决方案有什么不足之处?它做什么或不做什么是不正确的?您是否遇到错误/异常?
如何使用Python生成带有特定条目的CSV文件?
请阅读并遵循帮助文档中的发布指南,正如您创建此帐户时建议的那样。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。我已经编辑了问题。通常会向您提供示例提交文件。如果您将其作为数据帧,那么只需执行
提交['surved']=test\u预测
。下一行将从熊猫的数据帧创建csv文件
submission.to_csv('filename.csv',index=False)
问题是什么?你的解决方案有什么不足之处?它做什么或不做什么是不正确的?您是否遇到错误/异常?
如何使用Python生成带有特定条目的CSV文件?
请阅读并遵循帮助文档中的发布指南,正如您创建此帐户时建议的那样。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。我已经编辑了问题。通常会向您提供示例提交文件。如果您将其作为数据帧,那么只需执行
提交['surved']=test\u预测
。下一行将从熊猫的数据帧创建csv文件<代码>提交.to_csv('filename.csv',index=False)如何将其限制为418个条目?请尝试
测试数据.passengerid.values[:418]
测试预测[:418]
谢谢。我接受了你的问题,并投了赞成票。如果你认为这是一个问得很好的问题,你能给我一票吗?@OnurOzbek谢谢。当然,我已经这样做了。在那之前你投了反对票。现在这等于null…我如何将其限制为418个条目?请尝试
test\u data.passengerid.values[:418]
test\u预测[:418]
谢谢。我接受了你的问题,并投了赞成票。如果你认为这是一个问得很好的问题,你能给我一票吗?@OnurOzbek谢谢。当然,我已经这样做了。在那之前你投了反对票。所以现在这等于零。。。