import pandas as pd titanic=pd.read_csv('D:/Download/titanic-data.csv') data = pd.read_csv('D:/Download/titanic-data.csv') print(titanic.head(5)) X = titanic[['Pclass','Age','Sex']] y = titanic['Survived'] X.shape X.tail(5) X.info() mean_Age=X['Age'].mean() print(mean_Age) X['Age']=X['Age'].fillna(mean_Age) print(X.tail(5)) X['Pclass'] = X['Pclass' ].map({'1st':1, '2nd':2, '3rd':3}) X['Sex'] = X['Sex' ]. map({'female':0, 'male':1}) X. tail(5) from sklearn. preprocessing import MinMaxScaler scaler = MinMaxScaler() X_scaled = scaler.fit_transform(X) print (X_scaled) from sklearn.tree import DecisionTreeClassifier import numpy as np jack = np. array([[3, 23, 1]]) rose = np. array([[1, 20, 0]]) jack_scaled = scaler.transform (jack) rose_scaled = scaler.transform(rose) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=100) dt_clf = DecisionTreeClassifier (max_depth=2, min_samples_split=2) dt_clf.fit(X_train, y_train) print(dt_clf.predict_proba(jack_scaled) [0][1]) print (dt_clf.predict_proba(rose_scaled) [0][1])
时间: 2024-02-23 20:56:33 浏览: 115
学习pandas所使用到的数据
这是一个关于泰坦尼克号数据的代码,用于预测乘客是否生还。代码中用到了pandas库进行数据读取和处理,sklearn库中的MinMaxScaler对数据进行归一化处理,DecisionTreeClassifier对数据进行分类预测。其中,jack和rose分别代表“杰克”和“罗斯”这两个角色,他们的特征数据被输入到了模型中进行预测。
阅读全文