Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b69d36b

Browse files
Add files via upload
1 parent 5141dbc commit b69d36b

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

‎LDA.ipynb‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 10,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#import Library\n",
10+
"import numpy as np\n",
11+
"import pandas as pd\n",
12+
"import ssl\n",
13+
"ssl._create_default_https_context = ssl._create_unverified_context\n",
14+
"from sklearn.model_selection import train_test_split\n",
15+
"from sklearn.preprocessing import StandardScaler\n",
16+
"from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA\n",
17+
"from sklearn.ensemble import RandomForestClassifier\n",
18+
"from sklearn.metrics import confusion_matrix\n",
19+
"from sklearn.metrics import accuracy_score"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 11,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"#import data using url\n",
29+
"url = \"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\"\n",
30+
"names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']\n",
31+
"dataset = pd.read_csv(url, names=names)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 12,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"\n",
41+
"X = dataset.iloc[:, 0:4].values\n",
42+
"y = dataset.iloc[:, 4].values"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 13,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"#spilt data\n",
52+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 14,
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"#scalling data\n",
62+
"sc = StandardScaler()\n",
63+
"X_train = sc.fit_transform(X_train)\n",
64+
"X_test = sc.transform(X_test)"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 15,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"#LDA Model\n",
74+
"lda = LDA(n_components=1)\n",
75+
"X_train = lda.fit_transform(X_train, y_train)\n",
76+
"X_test = lda.transform(X_test)"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 16,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"#rendom Forest classifier\n",
86+
"classifier = RandomForestClassifier(max_depth=2, random_state=0)\n",
87+
"\n",
88+
"classifier.fit(X_train, y_train)\n",
89+
"y_pred = classifier.predict(X_test)"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 17,
95+
"metadata": {},
96+
"outputs": [
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"[[11 0 0]\n",
102+
" [ 0 13 0]\n",
103+
" [ 0 0 6]]\n",
104+
"Accuracy1.0\n"
105+
]
106+
}
107+
],
108+
"source": [
109+
"#confusion matrix\n",
110+
"cm = confusion_matrix(y_test, y_pred)\n",
111+
"print(cm)\n",
112+
"print('Accuracy' + str(accuracy_score(y_test, y_pred)))"
113+
]
114+
}
115+
],
116+
"metadata": {
117+
"kernelspec": {
118+
"display_name": "Python 3",
119+
"language": "python",
120+
"name": "python3"
121+
},
122+
"language_info": {
123+
"codemirror_mode": {
124+
"name": "ipython",
125+
"version": 3
126+
},
127+
"file_extension": ".py",
128+
"mimetype": "text/x-python",
129+
"name": "python",
130+
"nbconvert_exporter": "python",
131+
"pygments_lexer": "ipython3",
132+
"version": "3.7.7"
133+
}
134+
},
135+
"nbformat": 4,
136+
"nbformat_minor": 4
137+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /