四个模型建模及数据分析整理(基于Titanic数据集)

目录

介绍: 

 二、数据

2.1引用数据

2.2检查缺失数据

2.2.1手动检查缺失数据

2.2.2查看某一个特征值为空数据

2.3补充缺失数据

 2.3.1盒图

 2.3.2手动用均值填补缺失数据

2.3.3手动用类别填补缺失数据

三、数据分析

3.1男女生存比例

3.2男女生存数

3.3船舱级别生存比例

 3.4船舱生存与死亡比例

3.5票价与生存关系

3.6年龄与生存关系

3.7性别年龄与生存关系

3.8性别、登口岸、年龄与生存关系

3.9性别、票价、年龄与生存关系

四、数据处理

 4.1处理性别

 4.2热图表相关性

4.3姓名处理

4.4处理家庭大小

4.5清除票

4.5处理票价

4.6数据整理

4.7用RandomForestRegressor预测年龄填补缺失数据

 五、建模

5.1LogisticRegression建模 

5.2Decision Tree建模

5.3randomforest建模

5.4Adaboost建模


介绍: 

AdaBoost(Adaptive Boosting)是一种集成学习方法,用于提高机器学习算法的准确性。它通过训练一系列弱分类器(即准确率略高于随机猜测的分类器),然后将它们组合成一个强分类器。

AdaBoost的基本思想是对训练样本进行加权,将分类错误的样本权值增大,再训练下一个分类器。经过多轮迭代,每个样本都会得到不同的权值,并且每个分类器都有一定的权重。最终,AdaBoost通过对所有分类器的加权求和,得到最终的分类结果。

在AdaBoost中,每个弱分类器的训练过程如下:

  1. 初始化样本权值为相等值。
  2. 使用权值训练一个弱分类器。
  3. 根据分类错误率计算该分类器的权重。
  4. 更新样本权值,增加分类错误的样本权值,并减小分类正确的样本权值。
  5. 重复步骤2到4,直到达到指定的弱分类器个数或达到某个条件。

最终的强分类器是将每个弱分类器的输出加权求和,并根据总体输出判断样本的分类。

AdaBoost模型有一些特点:

  1. 适用于二分类和多分类问题。
  2. 对于弱分类器的选择没有严格的限制,可以使用任何分类器作为基分类器。
  3. 在训练过程中,每个样本都被赋予不同的权重,以便更加关注错误分类的样本。
  4. 可以减少过拟合问题,因为它将注意力集中在难以分类的样本上。
  5. AdaBoost的训练速度较快,预测速度也较快。

总的来说,AdaBoost是一种强大的集成学习算法,可以通过组合多个弱分类器来提高分类的准确性。它在实际应用中取得了很好的效果,并广泛应用于各个领域。

 二、数据

2.1引用数据

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inlineimport warnings
warnings.filterwarnings('ignore')##忽略警告train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")passengerid = test.PassengerIdprint(train.info())
print('*'*80)
print(test.info())
'''结果:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):#   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  0   PassengerId  891 non-null    int64  1   Survived     891 non-null    int64  2   Pclass       891 non-null    int64  3   Name         891 non-null    object 4   Sex          891 non-null    object 5   Age          714 non-null    float646   SibSp        891 non-null    int64  7   Parch        891 non-null    int64  8   Ticket       891 non-null    object 9   Fare         891 non-null    float6410  Cabin        204 non-null    object 11  Embarked     889 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
None
********************************************************************************
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 418 entries, 0 to 417
Data columns (total 12 columns):#   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  0   PassengerId  418 non-null    int64  1   Survived     418 non-null    int64  2   Pclass       418 non-null    int64  3   Name         418 non-null    object 4   Sex          418 non-null    object 5   Age          332 non-null    float646   SibSp        418 non-null    int64  7   Parch        418 non-null    int64  8   Ticket       418 non-null    object 9   Fare         417 non-null    float6410  Cabin        91 non-null     object 11  Embarked     418 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 39.3+ KB
None
'''

2.2检查缺失数据

2.2.1手动检查缺失数据

def missing_percentage(df):#显示空缺数据total = df.isnull().sum().sort_values(ascending=False)percent = round(df.isnull().sum().sort_values(ascending=False)/len(df)*100,2)return pd.concat([total,percent],axis=1,keys=['Total','Percent'])missing_percentage(train)#检查缺失数据
'''结果:Total 	Percent
Cabin 	687 	77.10
Age 	177 	19.87
Embarked 	2 	0.22
PassengerId 	0 	0.00
Survived 	0 	0.00
Pclass 	0 	0.00
Name 	0 	0.00
Sex 	0 	0.00
SibSp 	0 	0.00
Parch 	0 	0.00
Ticket 	0 	0.00
Fare 	0 	0.00
'''missing_percentage(test)
'''结果:Total 	Percent
Cabin 	327 	78.23
Age 	86 	20.57
Fare 	1 	0.24
PassengerId 	0 	0.00
Survived 	0 	0.00
Pclass 	0 	0.00
Name 	0 	0.00
Sex 	0 	0.00
SibSp 	0 	0.00
Parch 	0 	0.00
Ticket 	0 	0.00
Embarked 	0 	0.00
'''
def percent_value_counts(df,feature):#统计一个特征值的特征类别percent = pd.DataFrame(round(df.loc[:,feature].value_counts(dropna=False,normalize=True)*100,2))total = pd.DataFrame(df.loc[:,feature].value_counts(dropna=False))total.columns=["Total"]percent.columns=['Percent']return pd.concat([total,percent],axis=1)percent_value_counts(train,'Embarked')'''结果:Total 	Percent
S 	644 	72.28
C 	168 	18.86
Q 	77 	    8.64
NaN 2 	    0.22
'''

2.2.2查看某一个特征值为空数据

train[train.Embarked.isnull()]#查看某一特征值为空的数据

2.3补充缺失数据

 2.3.1盒图

sns.set_style('darkgrid')
fig, ax = plt.subplots(figsize=(16,12),ncols=2)#前面表示图的大小,后面表示两个图ax1 = sns.boxplot(x="Embarked", y="Fare", hue="Pclass", data=train, ax = ax[0]); 
ax2 = sns.boxplot(x="Embarked", y="Fare", hue="Pclass", data=test,ax = ax[1]); ax1.set_title("Training set", fontsize=18)
ax2.set_title('Test set', fontsize=18)## Fixing Legends
leg_1 = ax1.get_legend() 
leg_1.set_title("PClass") 
legs = leg_1.texts
legs[0].set_text('upper') 
legs[1].set_text('Middle') 
legs[2].set_text('Lower')fig.show( )#可以看均值,通过均值补充空的值

train.Embarked.fillna("C",inplace=True)#填充空数据##缺失数据的Pclass为1,票价为80,根据盒图可以看出Pclass为1,票价均值为80的c
#所以用c补充数据

 2.3.2手动用均值填补缺失数据

print("Train Cabin missing:" + str(train.Cabin.isnull().sum()/len(train.Cabin))) 
#结果:Train Cabin missing:0.7710437710437711'''缺失数据量大,把训练集和测试集合并'''
survivers = train.Survived
train.drop(["Survived"],axis=1, inplace=True)all_data = pd.concat([train,test],ignore_index=False)#train和test连接起来#* Assign all the null values to N
all_data.Cabin.fillna("N", inplace=True)#先用N补充空数据percent_value_counts(all_data,"Cabin")#统计一个特征值的特征个数
'''结果:Total 	Percent
N 	1014 	77.46
C 	94 	    7.18
B 	65 	    4.97
D 	46 	    3.51
E 	41 	    3.13
A 	22 	    1.68
F 	21 	    1.60
G 	5 	    0.38
T 	1 	    0.08
'''all_data.groupby("Cabin")['Fare'].mean().sort_values()#每个舱位的票价均值
'''结果:
Cabin
G     14.205000
F     18.079367
N     19.132707
T     35.500000
A     41.244314
D     53.007339
E     54.564634
C    107.926598
B    122.383078
Name: Fare, dtype: float64
'''
def cabin_estimator(i):#"""grouping cabin feature by the first letter"""a = 0if i<16:a="G"elif i>=16 and i<27:a="F"elif i>=27 and i<38:a="T"elif i>=38 and i<47:a="A"elif i>= 47 and i<53:a="E"elif i>= 53 and i<54:a="D"elif i>=54 and i<116:a='C'else:a="B"return a#把为N的挑出来,不为N挑出来
with_N = all_data[all_data.Cabin == "N"]without_N = all_data[all_data.Cabin != "N"]##opplying cabin estimator function.
with_N['Cabin'] = with_N.Fare.apply(lambda x: cabin_estimator(x))#根据票价赋值## getting back train.
all_data = pd.concat([with_N, without_N],axis=0)#连接在一起## PassengerId helps us separate train and test.
all_data.sort_values(by ='PassengerId', inplace=True)## Separating train and test from all_data. 
train = all_data[:891]#再分训练集和测试集test = all_data[891:]#adding saved target variable with train. 
train['Survived']=survivers

2.3.3手动用类别填补缺失数据

test[test.Fare.isnull()]
'''结果:PassengerId 	Pclass 	Name 	Sex 	Age 	SibSp 	Parch 	Ticket 	Fare 	Cabin 	Embarked 	Survived
152 	1044 	3 	Storey, Mr. Thomas 	male 	60.5 	0 	0 	3701 	NaN 	B 	S 	0.0
'''missing_value = test[(test.Pclass == 3)&(test.Embarked == 'S')&(test.Sex == 'male')].Fare.mean()#求这类乘客的票价均值test.Fare.fillna(missing_value,inplace=True)#将均值赋给空

三、数据分析

3.1男女生存比例

import seaborn as sns
pal = {'male':"green", 'female':"Pink"}
sns.set(style="darkgrid")
plt.subplots(figsize = (15,8))
ax = sns.barplot(x = "Sex", y = "Survived", data=train, palette = pal,linewidth=5,order = ['female','male'],capsize = .05,)plt.title("Survived/Non-Survived Passenger Gender Distribution", fontsize = 25,loc = 'center', pad = 40)
plt.ylabel("% of passenger survived", fontsize = 15, )
plt.xlabel("Sex",fontsize = 15);

3.2男女生存数

pal = {1:"seagreen", 0:"gray"}
sns.set(style="darkgrid")
plt.subplots(figsize = (15,8))
ax = sns.countplot(x = "Sex", hue="Survived",data = train, linewidth=4, palette = pal
)## Fixing title, xlabel and ylabel
plt.title("Passenger Gender Distribution - Survived vs Not-survived", fontsize = 25, pad=40)
plt.xlabel("Sex", fontsize = 15);
plt.ylabel("# of Passenger Survived", fontsize = 15)## Fixing xticks
#labels = ['Female', 'Male']
#plt.xticks(sorted(train.Sex.unique()), labels)## Fixing legends
leg = ax.get_legend()
leg.set_title("Survived")
legs = leg.texts
legs[0].set_text("No")
legs[1].set_text("Yes")
plt.show()

 

3.3船舱级别生存比例

plt.subplots(figsize = (15,10))
sns.barplot(x = "Pclass", y = "Survived", data=train, linewidth=6,capsize = .05,errcolor='blue',errwidth = 3)
plt.title("Passenger Class Distribution - Survived vs Non-Survived", fontsize = 25, pad=40)
plt.xlabel("Socio-Economic class", fontsize = 15);
plt.ylabel("% of Passenger Survived", fontsize = 15);
names = ['Upper', 'Middle', 'Lower']
#val = sorted(train.Pclass.unique())
val = [0,1,2] ## this is just a temporary trick to get the label right. 
plt.xticks(val, names);

 

 3.4船舱生存与死亡比例

# Kernel Density Plot
fig = plt.figure(figsize=(15,8),)
## I have included to different ways to code a plot below, choose the one that suites you. 
ax=sns.kdeplot(train.Pclass[train.Survived == 0] , color='blue',shade=True,label='not survived')
ax=sns.kdeplot(train.loc[(train['Survived'] == 1),'Pclass'] , color='g',shade=True, label='survived', )
plt.title('Passenger Class Distribution - Survived vs Non-Survived', fontsize = 25, pad = 40)
plt.ylabel("Frequency of Passenger Survived", fontsize = 15, labelpad = 20)
plt.xlabel("Passenger Class", fontsize = 15,labelpad =20)
## Converting xticks into words for better understanding
labels = ['Upper', 'Middle', 'Lower']
plt.xticks(sorted(train.Pclass.unique()), labels);

3.5票价与生存关系

# Kernel Density Plot
fig = plt.figure(figsize=(15,8),)
ax=sns.kdeplot(train.loc[(train['Survived'] == 0),'Fare'] , color='blue',shade=True,label='not survived')
ax=sns.kdeplot(train.loc[(train['Survived'] == 1),'Fare'] , color='g',shade=True, label='survived')
plt.title('Fare Distribution Survived vs Non Survived', fontsize = 25, pad = 40)
plt.ylabel("Frequency of Passenger Survived", fontsize = 15, labelpad = 20)
plt.xlabel("Fare", fontsize = 15, labelpad = 20);

 

3.6年龄与生存关系

# Kernel Density Plot
fig = plt.figure(figsize=(15,8),)
ax=sns.kdeplot(train.loc[(train['Survived'] == 0),'Age'] , color='blue',shade=True,label='not survived')
ax=sns.kdeplot(train.loc[(train['Survived'] == 1),'Age'] , color='g',shade=True, label='survived')
plt.title('Age Distribution - Surviver V.S. Non Survivors', fontsize = 25, pad = 40)
plt.xlabel("Age", fontsize = 15, labelpad = 20)
plt.ylabel('Frequency', fontsize = 15, labelpad= 20);

 

3.7性别年龄与生存关系

pal = {1:"seagreen", 0:"gray"}
g = sns.FacetGrid(train,  col="Sex", row="Survived", margin_titles=True, hue = "Survived",palette=pal)
g = g.map(plt.hist, "Age", edgecolor = 'white');
g.fig.suptitle("Survived by Sex and Age", size = 25)
plt.subplots_adjust(top=0.90)

3.8性别、登口岸、年龄与生存关系

g = sns.FacetGrid(train, col="Sex", row="Embarked", margin_titles=True, hue = "Survived",palette = pal)
g = g.map(plt.hist, "Age", edgecolor = 'white').add_legend();
g.fig.suptitle("Survived by Sex and Age", size = 25)
plt.subplots_adjust(top=0.90)

3.9性别、票价、年龄与生存关系

g = sns.FacetGrid(train,hue="Survived", col ="Sex", margin_titles=True,palette=pal,)
g.map(plt.scatter, "Fare", "Age",edgecolor="w").add_legend()
g.fig.suptitle("Survived by Sex, Fare and Age", size = 25)
plt.subplots_adjust(top=0.85)

 

四、数据处理

 4.1处理性别

male_mean = train[train['Sex'] == 1].Survived.mean()female_mean = train[train['Sex'] == 0].Survived.mean()
print ("Male survival mean: " + str(male_mean))
print ("female survival mean: " + str(female_mean))print ("The mean difference between male and female survival rate: " + str(female_mean - male_mean))'''结果:
Male survival mean: 0.18890814558058924
female survival mean: 0.7420382165605095
The mean difference between male and female survival rate: 0.5531300709799203
'''# separating male and female dataframe. 
import random
male = train[train['Sex'] == 1]
female = train[train['Sex'] == 0]## empty list for storing mean sample
m_mean_samples = []
f_mean_samples = []for i in range(50):m_mean_samples.append(np.mean(random.sample(list(male['Survived']),50,)))f_mean_samples.append(np.mean(random.sample(list(female['Survived']),50,)))# Print them out
print (f"Male mean sample mean: {round(np.mean(m_mean_samples),2)}")
print (f"Female mean sample mean: {round(np.mean(f_mean_samples),2)}")
print (f"Difference between male and female mean sample mean: {round(np.mean(f_mean_samples) - np.mean(m_mean_samples),2)}")'''结果:
Male mean sample mean: 0.18
Female mean sample mean: 0.74
Difference between male and female mean sample mean: 0.56
'''
# Placing 0 for female and 
# 1 for male in the "Sex" column. 
train['Sex'] = train.Sex.apply(lambda x: 0 if x == "female" else 1)
test['Sex'] = test.Sex.apply(lambda x: 0 if x == "female" else 1)
train.describe()#数据

survived_summary = train.groupby("Survived")
survived_summary.mean().reset_index()
'''结果:Survived 	PassengerId 	Pclass 	Sex 	Age 	SibSp 	Parch 	Fare
0 	0 	447.016393 	2.531876 	0.852459 	30.626179 	0.553734 	0.329690 	22.117887
1 	1 	444.368421 	1.950292 	0.318713 	28.343690 	0.473684 	0.464912 	48.395408
'''survived_summary = train.groupby("Sex")
survived_summary.mean().reset_index()
'''结果:Sex 	PassengerId 	Pclass 	Age 	SibSp 	Parch 	Fare 	Survived
0 	0 	431.028662 	2.159236 	27.915709 	0.694268 	0.649682 	44.479818 	0.742038
1 	1 	454.147314 	2.389948 	30.726645 	0.429809 	0.235702 	25.523893 	0.188908
'''survived_summary = train.groupby("Pclass")
survived_summary.mean().reset_index()
'''结果:Pclass 	PassengerId 	Sex 	Age 	SibSp 	Parch 	Fare 	Survived
0 	1 	461.597222 	0.564815 	38.233441 	0.416667 	0.356481 	84.154687 	0.629630
1 	2 	445.956522 	0.586957 	29.877630 	0.402174 	0.380435 	20.662183 	0.472826
2 	3 	439.154786 	0.706721 	25.140620 	0.615071 	0.393075 	13.675550 	0.242363
'''pd.DataFrame(abs(train.corr()["Survived"]).sort_values(ascending=False))#相关性
'''结果:Survived
Survived 	1.000000
Sex 	0.543351
Pclass 	0.338481
Fare 	0.257307
Parch 	0.081629
Age 	0.077221
SibSp 	0.035322
PassengerId 	0.005007
'''

 4.2热图表相关性

corr = train.corr()**2
corr.Survived.sort_values(ascending = False)## heatmeap to see the correlatton between features
# Generate a mask for the upper triangle (taken from seaborn example gallery)
import numpy as np
mask = np.zeros_like(train.corr(),dtype=bool)
mask[np.triu_indices_from(mask)]  = True
sns.set_style("whitegrid")plt.subplots(figsize = (15,12))
sns.heatmap(train.corr(),annot=True,mask = mask,cmap = 'RdBu', ## in order to reverse the bar replace "RdBu" with "RdBur'linewidths=.9,linecolor="white",fmt='.2g',center = 0,square=True)
plt.title("correlations Among Features", y = 1.03,fontsize = 20, pad = 40);

4.3姓名处理

# Creating a new colomn with a 
train['name_length'] = [len(i) for i in train.Name]
test['name_length'] = [len(i) for i in test.Name]def name_length_group(size):a = ''if (size <=20):a = 'short'elif (size <=35):a = 'medium'elif (size <=45):a = 'good'else:a = 'long'return atrain['nLength_group'] = train['name_length'].map(name_length_group)
test['nLength_group'] = test['name_length'].map(name_length_group)## Here "map" is python's built-in function. 
## "map" function basically takes a function and 
## returns an iterable list/tuple or in this case series. 
## However,"map" can also be used like map(function) e.g. map(name_length_group) 
## or map(function, iterable{list, tuple}) e.g. map(name_length_group, train[feature]]). 
## However, here we don't need to use parameter("size") for name_length_group because when we 
## used the map function like ".map" with a series before dot, we are basically hinting that series 
## and the iterable. This is similar to .append approach in python. list.append(a) meaning applying append on list. ## cuts the column by given bins based on the range of name_length
#group_names = ['short', 'medium', 'good', 'long']
#train['name_len_group'] = pd.cut(train['name_length'], bins = 4, labels=group_names)
## get the title from the name
train["title"] = [i.split('.')[0] for i in train.Name]
train["title"] = [i.split(',')[1] for i in train.title]
## Whenever we split like that, there is a good change that 
#we will end up with white space around our string values. Let's check that.print(train.title.unique())
'''结果:
[' Mr' ' Mrs' ' Miss' ' Master' ' Don' ' Rev' ' Dr' ' Mme' ' Ms' ' Major'' Lady' ' Sir' ' Mlle' ' Col' ' Capt' ' the Countess' ' Jonkheer']
'''## Let's fix that
train.title = train.title.apply(lambda x: x.strip())## We can also combile all three lines above for test set here
test['title'] = [i.split('.')[0].split(',')[1].strip() for i in test.Name]## However it is important to be able to write readable code, and the line above is not so readable. ## Let's replace some of the rare values with the keyword 'rare' and other word choice of our own. 
## train Data
train["title"] = [i.replace('Ms', 'Miss') for i in train.title]
train["title"] = [i.replace('Mlle', 'Miss') for i in train.title]
train["title"] = [i.replace('Mme', 'Mrs') for i in train.title]
train["title"] = [i.replace('Dr', 'rare') for i in train.title]
train["title"] = [i.replace('Col', 'rare') for i in train.title]
train["title"] = [i.replace('Major', 'rare') for i in train.title]
train["title"] = [i.replace('Don', 'rare') for i in train.title]
train["title"] = [i.replace('Jonkheer', 'rare') for i in train.title]
train["title"] = [i.replace('Sir', 'rare') for i in train.title]
train["title"] = [i.replace('Lady', 'rare') for i in train.title]
train["title"] = [i.replace('Capt', 'rare') for i in train.title]
train["title"] = [i.replace('the Countess', 'rare') for i in train.title]
train["title"] = [i.replace('Rev', 'rare') for i in train.title]## Now in programming there is a term called DRY(Don't repeat yourself), whenever we are repeating  
## same code over and over again, there should be a light-bulb turning on in our head and make us think
## to code in a way that is not repeating or dull. Let's write a function to do exactly what we 
## did in the code above, only not repeating and more interesting.## we are writing a function that can help us modify title column
def fuse_title(feature):"""This function helps modifying the title column"""result = ''if feature in ['the Countess','Capt','Lady','Sir','Jonkheer','Don','Major','Col', 'Rev', 'Dona', 'Dr']:result = 'rare'elif feature in ['Ms', 'Mlle']:result = 'Miss'elif feature == 'Mme':result = 'Mrs'else:result = featurereturn resulttest.title = test.title.map(fuse_title)
train.title = train.title.map(fuse_title)

4.4处理家庭大小

## bin the family size. 
def family_group(size):"""This funciton groups(loner, small, large) family based on family size"""a = ''if (size <= 1):a = 'loner'elif (size <= 4):a = 'small'else:a = 'large'return a## apply the family_group function in family_size
train['family_group'] = train['family_size'].map(family_group)
test['family_group'] = test['family_size'].map(family_group)train['is_alone'] = [1 if i<2 else 0 for i in train.family_size]
test['is_alone'] = [1 if i<2 else 0 for i in test.family_size]

4.5清除票

train.drop(['Ticket'],axis=1,inplace=True)
test.drop(['Ticket'],axis=1,inplace=True)

4.5处理票价

## Calculating fare based on family size. 
train['calculated_fare'] = train.Fare/train.family_size
test['calculated_fare'] = test.Fare/test.family_sizedef fare_group(fare):"""This function creates a fare group based on the fare provided"""a= ''if fare <= 4:a = 'Very_low'elif fare <= 10:a = 'low'elif fare <= 20:a = 'mid'elif fare <= 45:a = 'high'else:a = "very_high"return atrain['fare_group'] = train['calculated_fare'].map(fare_group)
test['fare_group'] = test['calculated_fare'].map(fare_group)#train['fare_group'] = pd.cut(train['calculated_fare'], bins = 4, labels=groups)

4.6数据整理

train.drop(['PassengerId'], axis=1, inplace=True)test.drop(['PassengerId'], axis=1, inplace=True)train = pd.get_dummies(train, columns=['title',"Pclass", 'Cabin','Embarked','nLength_group', 'family_group', 'fare_group'], drop_first=False)
test = pd.get_dummies(test, columns=['title',"Pclass",'Cabin','Embarked','nLength_group', 'family_group', 'fare_group'], drop_first=False)
train.drop(['family_size','Name', 'Fare','name_length'], axis=1, inplace=True)
test.drop(['Name','family_size',"Fare",'name_length'], axis=1, inplace=True)## rearranging the columns so that I can easily use the dataframe to predict the missing age values. 
train = pd.concat([train[["Survived", "Age", "Sex","SibSp","Parch"]], train.loc[:,"is_alone":]], axis=1)
test = pd.concat([test[["Age", "Sex"]], test.loc[:,"SibSp":]], axis=1)train

4.7用RandomForestRegressor预测年龄填补缺失数据

#用RandomForestRegressor预测年龄
# Importing RandomForestRegressor
from sklearn.ensemble import RandomForestRegressor## writing a function that takes a dataframe with missing values and outputs it by filling the missing values. 
def completing_age(df):## gettting all the features except survivedage_df = df.loc[:,"Age":] temp_train = age_df.loc[age_df.Age.notnull()] ## df with age valuestemp_test = age_df.loc[age_df.Age.isnull()] ## df without age valuesy = temp_train.Age.values ## setting target variables(age) in y x = temp_train.loc[:, "Sex":].valuesrfr = RandomForestRegressor(n_estimators=1500, n_jobs=-1)rfr.fit(x, y)predicted_age = rfr.predict(temp_test.loc[:, "Sex":])df.loc[df.Age.isnull(), "Age"] = predicted_agereturn df## Implementing the completing_age function in both train and test dataset. 
completing_age(train)
completing_age(test);## Let's look at the his
plt.subplots(figsize = (22,10),)
sns.distplot(train.Age, bins = 100, kde = True, rug = False, norm_hist=False);

## create bins for age
def age_group_fun(age):"""This function creates a bin for age"""a = ''if age <= 1:a = 'infant'elif age <= 4: a = 'toddler'elif age <= 13:a = 'child'elif age <= 18:a = 'teenager'elif age <= 35:a = 'Young_Adult'elif age <= 45:a = 'adult'elif age <= 55:a = 'middle_aged'elif age <= 65:a = 'senior_citizen'else:a = 'old'return a## Applying "age_group_fun" function to the "Age" column.
train['age_group'] = train['Age'].map(age_group_fun)
test['age_group'] = test['Age'].map(age_group_fun)## Creating dummies for "age_group" feature. 
train = pd.get_dummies(train,columns=['age_group'], drop_first=True)
test = pd.get_dummies(test,columns=['age_group'], drop_first=True);

 五、建模

# separating our independent and dependent variable
X = train.drop(['Survived'], axis = 1)
y = train["Survived"]from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size = .33, random_state=0)# Feature Scaling 数据差异大,进行标准化
## We will be using standardscaler to transform
from sklearn.preprocessing import StandardScaler
std_scale = StandardScaler()## transforming "train_x"
X_train = std_scale.fit_transform(X_train)
## transforming "test_x"
X_test = std_scale.transform(X_test)## transforming "The testset"
#test = st_scale.transform(test)

5.1LogisticRegression建模 

##LogisticRegression建模# import LogisticRegression model in python. 
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_absolute_error, accuracy_score## call on the model object
logreg = LogisticRegression(solver='liblinear',penalty= 'l1',random_state = 42)## fit the model with "train_x" and "train_y"
logreg.fit(X_train,y_train)y_pred = logreg.predict(X_test)from sklearn.metrics import classification_report, confusion_matrix
# printing confision matrix
pd.DataFrame(confusion_matrix(y_test,y_pred),\columns=["Predicted Not-Survived", "Predicted Survived"],\index=["Not-Survived","Survived"] )'''结果:Predicted Not-Survived 	Predicted Survived
Not-Survived 	156 	           28
Survived 	     26 	           85
'''
from sklearn.metrics import classification_report, balanced_accuracy_score
print(classification_report(y_test, y_pred))'''结果:precision    recall  f1-score   support0       0.86      0.85      0.85       1841       0.75      0.77      0.76       111accuracy                           0.82       295macro avg       0.80      0.81      0.81       295
weighted avg       0.82      0.82      0.82       295
'''from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
#结果:0.8169491525423729

5.2Decision Tree建模

##Decision Tree建模
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import GridSearchCV, StratifiedKFold, StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
max_depth = range(1,30)
max_feature = [21,22,23,24,25,26,28,29,30,'auto']
criterion=["entropy", "gini"]param = {'max_depth':max_depth, 'max_features':max_feature, 'criterion': criterion}
grid = GridSearchCV(DecisionTreeClassifier(), param_grid = param, verbose=False, cv=StratifiedShuffleSplit(n_splits=20, random_state=15),n_jobs = -1)
grid.fit(X, y) print( grid.best_params_)
print (grid.best_score_)
print (grid.best_estimator_)
'''结果:
{'criterion': 'entropy', 'max_depth': 5, 'max_features': 30}
0.828888888888889
DecisionTreeClassifier(criterion='entropy', max_depth=5, max_features=30)
'''dectree_grid = grid.best_estimator_
## using the best found hyper paremeters to get the score. 最好的模型参数
dectree_grid.score(X,y)#结果:0.8585858585858586

5.3randomforest建模

##randomforest建模
from sklearn.model_selection import GridSearchCV, StratifiedKFold, StratifiedShuffleSplit
from sklearn.ensemble import RandomForestClassifier
n_estimators = [140,145,150,155,160];
max_depth = range(1,10);
criterions = ['gini', 'entropy'];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)parameters = {'n_estimators':n_estimators,'max_depth':max_depth,'criterion': criterions}
grid = GridSearchCV(estimator=RandomForestClassifier(max_features='auto'),param_grid=parameters,cv=cv,n_jobs = -1)
grid.fit(X,y) print (grid.best_score_)
print (grid.best_params_)
print (grid.best_estimator_)
'''结果:
0.8384328358208956
{'criterion': 'entropy', 'max_depth': 7, 'n_estimators': 160}
RandomForestClassifier(criterion='entropy', max_depth=7, max_features='auto',n_estimators=160)
'''rf_grid = grid.best_estimator_
rf_grid.score(X,y)
#结果:0.8742985409652076

5.4Adaboost建模

from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier# 创建一个决策树分类器作为基本估计器
base_estimator = DecisionTreeClassifier()# 创建AdaBoost分类器并将决策树分类器作为基本估计器
ada_boost = AdaBoostClassifier(base_estimator=base_estimator)# 使用AdaBoost分类器进行训练和预测
ada_boost.fit(X_train, y_train)predictions = ada_boost.predict(X_test)from sklearn.metrics import accuracy_scoreaccuracy_score(y_test, y_pred)
#结果:0.8169491525423729from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedShuffleSplitn_estimators = [80,100,140,145,150,160, 170,175,180,185];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
learning_r = [0.1,1,0.01,0.5]parameters = {'n_estimators':n_estimators,'learning_rate':learning_r}base_estimator = DecisionTreeClassifier()grid = GridSearchCV(AdaBoostClassifier(base_estimator= base_estimator, ## If None, then the base estimator is a decision tree.),param_grid=parameters,cv=cv,n_jobs = -1)
grid.fit(X,y) ada_grid = grid.best_estimator_
ada_grid.score(X,y)
#结果:0.9887766554433222

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/326406.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 打印日志的几种方式

java 打印日志的几种方式 Java 日志框架进化史日志门面与日志系统 Log4jslf4jLog4j2slf4jLogbackslf4j 一、先简单介绍五种 &#xff08;1&#xff09;最简单的方式&#xff0c;就是system.println.out(error) ,这样直接在控制台打印消息了&#xff1b; &#xff08;2&#xff…

淘宝京东1688商品详情API接口,搜索商品列表接口

前言 在实际工作中&#xff0c;我们需要经常跟第三方平台打交道&#xff0c;可能会对接第三方平台API接口&#xff0c;或者提供API接口给第三方平台调用。 那么问题来了&#xff0c;如果设计一个优雅的API接口&#xff0c;能够满足&#xff1a;安全性、可重复调用、稳定性、好…

iPhone 恢复出厂设置后如何恢复数据

如果您在 iPhone 上执行了恢复出厂设置&#xff0c;您会发现所有旧数据都被清除了。这对于清理混乱和提高设备性能非常有用&#xff0c;但如果您忘记保存重要文件&#xff0c;那就是坏消息了。 恢复出厂设置后可以恢复数据吗&#xff1f;是的&#xff01;幸运的是&#xff0c;…

第8课 将推流端与播放端合并为一对一音视频聊天功能

在第二章的第7课&#xff0c;我们实现了一个推流端&#xff0c;可以把音视频推送到rtmp服务器&#xff1b;在第一章的第4课&#xff0c;我们实现了一个播放器&#xff0c;可以正常播放rtmp音视频流。聪明的你应该可以想到了&#xff1a;把推流端和播放端合并在一起&#xff0c;…

迭代实现二叉树的遍历(算法村第七关黄金挑战)

迭代实现前序遍历 144. 二叉树的前序遍历 - 力扣&#xff08;LeetCode&#xff09; 题解的迭代方式 因为在递归的过程中使用了系统栈&#xff0c;所以在迭代的解法中常用 Stack 来模拟系统栈&#xff0c;来模拟递归。 首先创建一个 Stack 用来存放节点&#xff0c;此时 Sta…

SpringCloud系列篇:入门讲解Spring Cloud是什么

&#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于SpringCloud的相关操作吧 目录 &#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 一.Spring Cloud是什么 二.Spring …

哈尔滨爆火的背后有什么值得我们学习的,2024普通人如何创业/2024风口行业

这个冬天&#xff0c;“南方小土豆”带火东北冰雪游。“冰城”黑龙江哈尔滨的文旅市场异常火爆&#xff0c;元旦假期3天&#xff0c;哈尔滨市累计接待游客304.79万人次&#xff0c;实现旅游总收入59.14亿元。旅游总收入达到历史峰值。哈尔滨旅游怎么就爆火了&#xff1f;背后究…

彻底卸载 Microsoft Edge

如果您想学习如何在 Windows 10 上卸载 Microsoft Edge&#xff0c;您并不孤单。尽管目前基于 Chromium 的 Microsoft 浏览器版本比以前的 Edge 版本要好得多&#xff0c;但它仍然是一个不受欢迎的浏览器。 尽管您需要在 PC 上预装浏览器才能安装其他浏览器&#xff0c;但许多…

AcWing 853. 有边数限制的最短路—bellman-ford算法

问题链接:AcWing 853. 有边数限制的最短路 问题描述 分析 bellman-ford算法可以用来解决带负权边的最短路问题&#xff0c;这是相比于DJ算法优秀的地方&#xff0c;但是要注意的是能解决带负权边的最短路问题&#xff0c;如果负权边构成了负权回路&#xff0c;那就有可能求不出…

【InternLM】书生-浦语大模型demo搭建服务接口部署本地映射

目录 前言一、InternLM大模型介绍1-1、大模型简介1-2、InternLM大模型简介1-2-1、InternLM-7B1-2-2、InternLM-20B 二、从0开始搭建InternLM-Chat-7B 智能对话 Demo2-0、环境搭建2-1、创建虚拟环境2-2、导入所需要的包2-3、模型下载2-4、代码克隆2-5、终端运行 三、服务器接口部…

解决“由于找不到d3dx9_43.dll无法继续执行代码”的5种方法

找不到d3dx9_43.dll文件导致无法继续执行代码的问题&#xff0c;通常是由于DirectX组件缺失或损坏引起的。这个错误通常出现在运行某些游戏或应用程序时&#xff0c;它会导致程序无法正常运行。下面我将介绍5种解决找不到d3dx9_43.dll的方法。 一、d3dx9_43.dll的文件属性&…

Python从入门到精通总结规划

Python从入门到精通专栏&#xff1a;http://t.csdnimg.cn/4Lals 时光飞逝&#xff0c;转眼间我们的Python从入门到精通专栏已经接近尾声。 在这里&#xff0c;向大家表示最诚挚的感谢。感谢你们一直以来对Python学习的热情&#xff0c;以及对本专栏的持续关注和支持。 回顾过去…