Skip to content
Snippets Groups Projects
Commit 0d385257 authored by Akshit Bhatia's avatar Akshit Bhatia
Browse files

Upload New File

parent 379754fe
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
```
%% Cell type:code id: tags:
``` python
for filename in os.listdir('./vibrationdata/'):
file=pd.read_csv('./vibrationdata/'+filename,delimiter='\t',header=None)
print(filename)
break
```
%% Output
2021-05-17_08-46-45
%% Cell type:markdown id: tags:
for every record in quality, get the timestamp from product id in log file, and then plot from the data
%% Cell type:code id: tags:
``` python
qualityf=pd.read_csv('product_quality_log.csv')
ok=qualityf[qualityf['quality']=='OK']
nok=qualityf[qualityf['quality']=='nOK']
ok
```
%% Output
Unnamed: 0 machine_id product_id quality
0 0 Printer F0815 P3.2.500 OK
1 1 Printer F0815 P3.2.501 OK
2 2 Printer F0815 P3.2.502 OK
3 3 Printer F0815 P3.2.503 OK
4 4 Printer F0815 P3.2.504 OK
... ... ... ... ...
1611 1611 Printer F0815 P3.2.2111 OK
1614 1614 Printer F0815 P3.2.2114 OK
1618 1618 Printer F0815 P3.2.2118 OK
1619 1619 Printer F0815 P3.2.2119 OK
1620 1620 Printer F0815 P3.2.2120 OK
[1564 rows x 4 columns]
%% Cell type:code id: tags:
``` python
log=pd.read_csv('production_log.csv')
log[:2]
```
%% Output
Unnamed: 0 timestamp product_id
0 0 2021-05-17_08-12-48 P3.2.500
1 1 2021-05-17_08-12-51 P3.2.501
%% Cell type:code id: tags:
``` python
x=log[log['product_id']=='P3.2.500']['timestamp'].values[0]
x
```
%% Output
'2021-05-17_08-12-48'
%% Cell type:code id: tags:
``` python
from scipy import stats
```
%% Cell type:code id: tags:
``` python
# fig, ax = plt.subplots()
# figo, nokx = plt.subplots()
y=0
x_stdk=[]
x_stdnk=[]
y_stdk=[]
y_stdnk=[]
x_stdk2=[]
y_stdk2=[]
x_stdnk2=[]
y_stdnk2=[]
for rec in ok.itertuples():
# get prodcut id
product_id=rec.product_id
# fetch time_stamp from log
timestamp=log[log['product_id']==product_id]['timestamp'].values[0]
#get the file from data
df = pd.read_csv('./vibrationdata/'+timestamp, delimiter='\t', header=None)
x=df[0].to_numpy().mean()
x_std = stats.tstd(df[0].to_numpy())
y=df[1].to_numpy().mean()
y_std = stats.tstd(df[1].to_numpy())
x_stdk.append(x_std)
y_stdk.append(y_std)
x_stdk2.append(x_std*x_std)
y_stdk2.append(y_std*y_std)
plt.plot(x_std,y_std, color='green', linestyle='solid', linewidth = 3,
marker='o')
for rec in nok.itertuples():
# get prodcut id
product_id=rec.product_id
# fetch time_stamp from log
timestamp=log[log['product_id']==product_id]['timestamp'].values[0]
#get the file from data
df = pd.read_csv('./vibrationdata/'+timestamp, delimiter='\t', header=None)
x=df[0].to_numpy().mean()
x_std = stats.tstd(df[0].to_numpy())
x_stdnk.append(x_std)
y=df[1].to_numpy().mean()
y_std = stats.tstd(df[1].to_numpy())
y_stdnk.append(y_std)
x_stdnk2.append(x_std*x_std)
y_stdnk2.append(y_std*y_std)
plt.plot(x_std,y_std, color='red', linestyle='solid', linewidth = 3,
marker='o')
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
target_ok = ok['quality']
target_ok = target_ok.values.reshape(-1,1)
target_nok = nok['quality']
target_nok = target_nok.values.reshape(-1,1)
target = np.concatenate((target_ok,target_nok))
target
```
%% Output
array([['OK'],
['OK'],
['OK'],
...,
['nOK'],
['nOK'],
['nOK']], dtype=object)
%% Cell type:code id: tags:
``` python
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix, plot_confusion_matrix
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
import logging as logger
def clf_svm(X, y):
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=4)
clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
logger.info("training SVM")
clf.fit(x_train, y_train)
logger.info("training finished")
results = clf.predict(x_test)
plot_confusion_matrix(clf, x_test, y_test)
plt.show()
return results
x1= np.concatenate((x_stdk, x_stdnk))
x1_reshaped = x1.reshape(-1,1)
x2= np.concatenate((x_stdk2, x_stdnk2))
x2_reshaped=x2.reshape(-1,1)
x_col = np.concatenate((x1_reshaped,x2_reshaped), axis=1)
y1=np.concatenate((y_stdk, y_stdnk))
y2=np.concatenate((y_stdk2, y_stdnk2))
y1_reshaped = y1.reshape(-1,1)
y2_reshaped = y2.reshape(-1,1)
y_col=np.concatenate((y1_reshaped, y2_reshaped), axis=1)
features = np.concatenate((x_col, y_col),axis=1)
clf_svm(features,target)
```
%% Output
/home/akshit/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
return f(**kwargs)
array(['OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'], dtype=object)
%% Cell type:code id: tags:
``` python
# svm f1 score
tp=516
fp=16
tn = 19
fn = 1
prec = tp/(tp+fp)
rec = tp/(tp+fn)
f_score=2*prec*rec/(prec+rec)
f_score
```
%% Output
0.9837940896091516
%% Cell type:code id: tags:
``` python
from sklearn.ensemble import RandomForestClassifier
def clf_rf(X, y):
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=4)
clf = RandomForestClassifier(max_depth=100)
logger.info("training Random Forest")
clf.fit(x_train, y_train)
logger.info("training finished")
results = clf.predict(x_test)
plot_confusion_matrix(clf, x_test, y_test)
plt.show()
return results
x1= np.concatenate((x_stdk, x_stdnk))
x1_reshaped = x1.reshape(-1,1)
x2= np.concatenate((x_stdk2, x_stdnk2))
x2_reshaped=x2.reshape(-1,1)
x_col = np.concatenate((x1_reshaped,x2_reshaped), axis=1)
y1=np.concatenate((y_stdk, y_stdnk))
y2=np.concatenate((y_stdk2, y_stdnk2))
y1_reshaped = y1.reshape(-1,1)
y2_reshaped = y2.reshape(-1,1)
y_col=np.concatenate((y1_reshaped, y2_reshaped), axis=1)
features = np.concatenate((x_col, y_col),axis=1)
clf_rf(features, target)
```
%% Output
<ipython-input-59-3c3a156cada0>:6: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().
clf.fit(x_train, y_train)
array(['OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'nOK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK',
'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'nOK', 'OK', 'OK', 'OK', 'nOK',
'OK', 'nOK', 'nOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK',
'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'], dtype=object)
%% Cell type:code id: tags:
``` python
# random forest f1 score
tp=511
fp=11
tn = 14
fn = 1
prec = tp/(tp+fp)
rec = tp/(tp+fn)
f_score=2*prec*rec/(prec+rec)
f_score
```
%% Output
0.988394584139265
%% Cell type:markdown id: tags:
### just random experiments
%% Cell type:code id: tags:
``` python
count=0
for rec in ok.itertuples():
# get prodcut id
product_id=rec.product_id
# fetch time_stamp from log
timestamp=log[log['product_id']==product_id]['timestamp'].values[0]
#get the file from data
df = pd.read_csv('./vibrationdata/'+timestamp, delimiter='\t', header=None)
x=df[0].to_numpy().mean()
y=df[1].to_numpy().mean()
if x > -0.118 and x < -0.116 and y > -0.119 and y< -0.117:
count=count+1
plt.plot(x,y, color='green', linestyle='solid', linewidth = 3,
marker='o')
print(count)
```
%% Output
760
%% Cell type:code id: tags:
``` python
nok
```
%% Output
Unnamed: 0 machine_id product_id quality
1501 1501 Printer F0815 P3.2.2001 nOK
1503 1503 Printer F0815 P3.2.2003 nOK
1508 1508 Printer F0815 P3.2.2008 nOK
1509 1509 Printer F0815 P3.2.2009 nOK
1511 1511 Printer F0815 P3.2.2011 nOK
... ... ... ... ...
1651 1651 Printer F0815 P3.2.2151 nOK
1652 1652 Printer F0815 P3.2.2152 nOK
1653 1653 Printer F0815 P3.2.2153 nOK
1654 1654 Printer F0815 P3.2.2154 nOK
1655 1655 Printer F0815 P3.2.2155 nOK
[92 rows x 4 columns]
%% Cell type:code id: tags:
``` python
for rec in nok.itertuples():
# get prodcut id
product_id=rec.product_id
# fetch time_stamp from log
timestamp=log[log['product_id']==product_id]['timestamp'].values[0]
#get the file from data
df = pd.read_csv('./vibrationdata/'+timestamp, delimiter='\t', header=None)
x=df[0].to_numpy().mean()
y=df[1].to_numpy().mean()
plt.plot(x,y, color='red', linestyle='solid', linewidth = 3,
marker='o')
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
x=file[0].to_numpy()
y=file[1].to_numpy()
plt.scatter(x, y)
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
plt.plot(x,y)
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
sensor_data=pd.read_csv('./vibrationdata/2021-05-17_09-35-21', delimiter='\t',header=None)
sensor_data
```
%% Output
0 1
0 0.056 -0.095
1 0.120 0.012
2 -0.107 -0.063
3 -0.374 0.083
4 -0.510 -0.149
... ... ...
20475 0.037 0.015
20476 -0.010 -0.234
20477 -0.051 -0.359
20478 -0.154 -0.247
20479 -0.005 -0.137
[20480 rows x 2 columns]
%% Cell type:code id: tags:
``` python
plt.scatter(sensor_data[0],sensor_data[1])
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment