我试图在以CSV格式存储的数据集上实现逻辑回归,然而,尽管实现这一点完全是网上的一个例子,显然我的数据还没有转换成一种格式,它可以在数字上工作。
我通常只使用c ++/java,所以所有这些python语法和处理这些数据集的函数对我来说相当混乱。
任何帮助都将不胜感激。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
def calc_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass == 1:
return 37
elif Pclass == 2:
return 29
else:
return 24
else:
return Age
def driverMain():
train = pd.read_csv('/Users/krishanbansal/Downloads/LogisticRegression-master/titanic_train.csv')
test = pd.read_csv('/Users/krishanbansal/Downloads/LogisticRegression-master/titanic_test.csv')
sns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap='viridis')
train['Age'] = train[['Age','Pclass']].apply(calc_age,axis=1)
test['Age'] = test[['Age','Pclass']].apply(calc_age,axis=1)
sex = pd.get_dummies(train['Sex'],drop_first=True)
embark = pd.get_dummies(train['Embarked'],drop_first=True)
train.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)
train = pd.concat([train,sex,embark],axis=1)
train.head()
train.drop(['male','Q','S'],axis=1,inplace=True)
sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')
X_train, X_test, y_train, y_test = train_test_split(train.drop('Survived',axis=1),train['Survived'], test_size=0.20,random_state=101)
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)
predictions = logmodel.predict(X_test)
print(classification_report(y_test,predictions))
print("Accuracy:",metrics.accuracy_score(y_test, predictions))
if __name__ == '__main__':
driverMain()
1条答案
按热度按时间nqwrtyyt1#
这个错误可能来自于你的数据集中的一些值是用科学记数法写的,例如
1E17
.在python中你通常会用1e17
来写科学记数法,所以解释器可能在存储数据时没有为你做浮点数的转换。您可能需要检查数据集并在需要时进行适当的转换。