-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitanic_train.py
123 lines (88 loc) · 2.87 KB
/
titanic_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge
from sklearn import preprocessing, svm, linear_model
from sklearn.preprocessing import StandardScaler, LabelEncoder, OrdinalEncoder
from sklearn.cluster import KMeans
#from sklearn.metrics import r2_score
#from sklearn.model_selection import train_test_split
#from sklearn.cluster import MeanShift
import matplotlib.pyplot as plt
def category_conversion(frame):
columns = frame.columns.values
for col in columns:
text_values = {}
def convert_to_int(val):
return text_values[val]
#if frame[col]dtype != np.int64 or frame[col]dtype != np.float64:
# content = frame[col].tolist()
data = pd.read_csv('train.csv')
#print(data)
#plt.scatter(data['Fare'], data['Survived'])
#plt.show()
data['Age'].fillna(data['Age'].mean(), inplace = True)
data['Cabin'].fillna('Unknown', inplace = True)
data['Embarked'].fillna('S', inplace = True)
print(data.describe())
#scaler = StandardScaler()
encode = OrdinalEncoder()
#print(data)
X = np.array(data.drop(['PassengerId', 'Survived', 'Name'], 1))
y = np.array(data['Survived'])
print(np.shape(X))
#print(X[:, 9])
X = encode.fit_transform(X)
X = preprocessing.scale(X)
print(X[0])
print('****')
print(data.corr(method = 'pearson'))
X_train = X[: -20]
#y_train = y[: -20]
X_test = X[-20 :]
#y_test = y[-20 :]
clf = KMeans(n_clusters = 2)
#print(clf.cluster_centers_)
#clf = svm.SVC(kernel = 'linear', C = 1.0, gamma = 10)
y = clf.fit_predict(X_train)
#clf.cluster_centers_[0]
#or i in range:
# plt.plot(clf.cluster_centers_[0, i], clf.cluster_centers_[1, i])
#for i in clf.cluster_centers_:
# plt.plot(clf.cluster_centers_[: , 0], clf.cluster_centers_[0, :])
#print(clf.cluster_centers_[i], data.loc['Survived'][i])
#plt.scatter(center[])
plt.scatter(X_train, X_test)
plt.show()
for val in range(len(X_test)):
print(X[:][val], 'will (s)he survive?', y[val])
#print(clf.cluster_centers_())
confidence = clf.score(X_test)
print('confidence: ', confidence)
#plt.scatter(data['Fare'], data['Survived'])
#plt.show()
#X = np.array(data.drop(['Serial No.', 'Chance of Admit '], 1))
#y = np.array(data['Chance of Admit '])
#X = preprocessing.scale(X)
#data = scaler.fit(data)
"""
X_train = X[: -20]
y_train = y[: -20]
X_test = X[-20 :]
y_test = y[-20 :]
print(X_train)
print('*****')
print(y_train)
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
#clf = linear_model.SGDRegressor(alpha = 0.0001, tol = 0.0001)
#clf = linear_model.LinearRegression()
#clf = linear_model.Ridge(alpha = 0.001, normalize = False)
clf = svm.LinearSVR()
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
#r2_confidence = r2_score(y_test, clf.predict(X_test))
print('confidence: ', confidence)
#print('confidence: ', r2_confidence)
print('Admit prediction: ', clf.predict(X_test))
plt.scatter(data['CGPA'], data['Chance of Admit '])
plt.show()
"""