Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
yl3r22
Assignment2
Commits
d0a1c734
Commit
d0a1c734
authored
2 years ago
by
yl3r22
Browse files
Options
Downloads
Patches
Plain Diff
part - B
parent
6ca43236
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Untitled7.ipynb
+208
-0
208 additions, 0 deletions
Untitled7.ipynb
with
208 additions
and
0 deletions
Untitled7.ipynb
0 → 100644
+
208
−
0
View file @
d0a1c734
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "58e43186",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0 1 2 3 4 5 \\\n",
"0 70.399324 127673.0908 -49.572308 127648.0176 -169.578319 127723.2374 \n",
"\n",
" 6 7 8 9 ... 119 120 121 122 123 \\\n",
"0 65.689611 605.91099 -57.003571 626.78553 ... 0 0 0 0 0 \n",
"\n",
" 124 125 126 127 128 \n",
"0 0 0 0 0 0 \n",
"\n",
"[1 rows x 129 columns]\n"
]
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"from sklearn import datasets\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn import svm, metrics\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import cross_val_score\n",
"\n",
"df = pd.read_csv('C:\\\\Users\\yl3r22\\Downloads\\TrainingDataMulti.csv', header= None)\n",
"df1 = pd.read_csv('C:\\\\Users\\yl3r22\\Downloads\\TestingDataMulti.csv', header= None)\n",
"\n",
"print(df.head(1))\n",
"\n",
"df_feature = df.iloc[:, :128]\n",
"df_label = df.iloc[:, 128]\n",
"cancer = datasets.load_breast_cancer()\n",
"\n",
"# Split dataset into training set and test set\n",
"X_train, X_test, y_train, y_test = train_test_split(df_feature, df_label, test_size=0.2)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "461e2292",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TrainingAccuracy: 0.7008333333333333\n"
]
}
],
"source": [
"#Create a svm Classifier\n",
"clf = svm.SVC(kernel='linear') # Linear Kernel\n",
"\n",
"#Train the model using the training sets\n",
"clf.fit(X_train, y_train)\n",
"\n",
"#Predict the response for test dataset\n",
"y_pred = clf.predict(X_test)\n",
"\n",
"print(\"TrainingAccuracy:\",metrics.accuracy_score(y_test, y_pred))\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fcda6021",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 0 2 0 0 0 1 1 2 2 1 1 1 1 1 1 0 2 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1\n",
" 1 0 1 1 0 1 0 0 1 1 1 0 0 1 1 1 1 1 1 1 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
"TrainingAccuracy: 0.9316666666666666\n"
]
}
],
"source": [
"y_result = clf.predict(df1)\n",
"print(y_result)\n",
"#Create a svm Classifier\n",
"clf1 = RandomForestClassifier(n_estimators=100, max_features=70)# Linear Kernel\n",
"\n",
"#Train the model using the training sets\n",
"clf1.fit(X_train, y_train)\n",
"\n",
"#Predict the response for test dataset\n",
"y_pred = clf1.predict(X_test)\n",
"\n",
"print(\"TrainingAccuracy:\",metrics.accuracy_score(y_test, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "897bcf3d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scores: [0.9275 0.9375 0.9475 0.94375 0.94 0.9325 ]\n",
"MeanScores: 0.938125\n"
]
}
],
"source": [
"# Evaluate the classifier\n",
"scores0 = cross_val_score(clf1, X_train, y_train, cv = 6)\n",
"\n",
"print(\"Scores:\", scores0)\n",
"print(\"MeanScores:\", np.mean(scores0))\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1a48b862",
"metadata": {},
"outputs": [],
"source": [
"test_data=pd.read_csv(\"C:\\\\Users\\yl3r22\\Downloads\\TestingDataMulti.csv\",header=None)\n",
"\n",
"# Predict testing datasets at first\n",
"# Use previous model - df1\n",
"\n",
"predictions = clf1.predict(test_data)\n",
"\n",
"# Convert data frame\n",
"prediction_df = pd.DataFrame(predictions)\n",
"\n",
"result = pd.concat([test_data,prediction_df],axis=1)\n",
"\n",
"# output the csv documentary\n",
"\n",
"result.to_csv('C:\\\\Users\\yl3r22\\Downloads\\part2.csv',index = False, header = False)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "354f7f52",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 2 2 2 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 0 0 0 0 0 0 1 1 1 1\n",
" 1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 1 1 2 1 1 1 1 1 1 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0]\n"
]
}
],
"source": [
"# Check Predictions\n",
"print(predictions)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54cd25fd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:58e43186 tags:
```
python
import
pandas
as
pd
import
numpy
as
np
from
sklearn
import
datasets
from
sklearn.model_selection
import
train_test_split
from
sklearn
import
svm
,
metrics
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.metrics
import
accuracy_score
from
sklearn.ensemble
import
RandomForestClassifier
from
sklearn.model_selection
import
cross_val_score
df
=
pd
.
read_csv
(
'
C:
\\
Users\yl3r22\Downloads\TrainingDataMulti.csv
'
,
header
=
None
)
df1
=
pd
.
read_csv
(
'
C:
\\
Users\yl3r22\Downloads\TestingDataMulti.csv
'
,
header
=
None
)
print
(
df
.
head
(
1
))
df_feature
=
df
.
iloc
[:,
:
128
]
df_label
=
df
.
iloc
[:,
128
]
cancer
=
datasets
.
load_breast_cancer
()
# Split dataset into training set and test set
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
df_feature
,
df_label
,
test_size
=
0.2
)
```
%% Output
0 1 2 3 4 5 \
0 70.399324 127673.0908 -49.572308 127648.0176 -169.578319 127723.2374
6 7 8 9 ... 119 120 121 122 123 \
0 65.689611 605.91099 -57.003571 626.78553 ... 0 0 0 0 0
124 125 126 127 128
0 0 0 0 0 0
[1 rows x 129 columns]
%% Cell type:code id:461e2292 tags:
```
python
#Create a svm Classifier
clf
=
svm
.
SVC
(
kernel
=
'
linear
'
)
# Linear Kernel
#Train the model using the training sets
clf
.
fit
(
X_train
,
y_train
)
#Predict the response for test dataset
y_pred
=
clf
.
predict
(
X_test
)
print
(
"
TrainingAccuracy:
"
,
metrics
.
accuracy_score
(
y_test
,
y_pred
))
```
%% Output
TrainingAccuracy: 0.7008333333333333
%% Cell type:code id:fcda6021 tags:
```
python
y_result
=
clf
.
predict
(
df1
)
print
(
y_result
)
#Create a svm Classifier
clf1
=
RandomForestClassifier
(
n_estimators
=
100
,
max_features
=
70
)
# Linear Kernel
#Train the model using the training sets
clf1
.
fit
(
X_train
,
y_train
)
#Predict the response for test dataset
y_pred
=
clf1
.
predict
(
X_test
)
print
(
"
TrainingAccuracy:
"
,
metrics
.
accuracy_score
(
y_test
,
y_pred
))
```
%% Output
[2 0 2 0 0 0 1 1 2 2 1 1 1 1 1 1 0 2 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1
1 0 1 1 0 1 0 0 1 1 1 0 0 1 1 1 1 1 1 1 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
TrainingAccuracy: 0.9316666666666666
%% Cell type:code id:897bcf3d tags:
```
python
# Evaluate the classifier
scores0
=
cross_val_score
(
clf1
,
X_train
,
y_train
,
cv
=
6
)
print
(
"
Scores:
"
,
scores0
)
print
(
"
MeanScores:
"
,
np
.
mean
(
scores0
))
```
%% Output
Scores: [0.9275 0.9375 0.9475 0.94375 0.94 0.9325 ]
MeanScores: 0.938125
%% Cell type:code id:1a48b862 tags:
```
python
test_data
=
pd
.
read_csv
(
"
C:
\\
Users\yl3r22\Downloads\TestingDataMulti.csv
"
,
header
=
None
)
# Predict testing datasets at first
# Use previous model - df1
predictions
=
clf1
.
predict
(
test_data
)
# Convert data frame
prediction_df
=
pd
.
DataFrame
(
predictions
)
result
=
pd
.
concat
([
test_data
,
prediction_df
],
axis
=
1
)
# output the csv documentary
result
.
to_csv
(
'
C:
\\
Users\yl3r22\Downloads\part2.csv
'
,
index
=
False
,
header
=
False
)
```
%% Cell type:code id:354f7f52 tags:
```
python
# Check Predictions
print
(
predictions
)
```
%% Output
[2 2 2 2 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 0 0 0 0 0 0 1 1 1 1
1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 1 1 2 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0]
%% Cell type:code id:54cd25fd tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment