top of page

Grasshopper to CSV file using a SVM example

Steps followed in this notebook:

 

1. Import CSV file

2. Selecting features and labels

3. ML or data analysis

4. Export results to CSV

​

You can download the notebook version here:

​

​

 

You can download the Grasshopper script using the link here:

​

Gh to CSV using a SVM example - Gh script

​

 

Code

​

import numpy as np
import pandas as pd
import os
​
Import CSV file
​
# Your CSV file name here
csv_file_name = "test file.csv"
 
current_path = os.getcwd()
csv_filepath = os.path.join(current_path, csv_file_name)
 
# print(csv_filepath)
​
# Option 1: Import using Numpy
my_data_numpy = np.genfromtxt(csv_filepath, delimiter=',')
print(my_data_numpy[0:5])
print('\n \t Without first row of empty labels \n')
print(my_data_numpy[1:5])
​
[[     nan      nan     nan      nan]
 [0.542188 0.452487 0.      0.      ]
 [0.808325 0.634651 0.      0.      ]
 [1.331997 0.536045 0.      0.      ]
 [3.970094 0.116322 0.      0.      ]]
​
   Without first row of empty labels
​
[[0.542188 0.452487 0.      0.      ]
 [0.808325 0.634651 0.      0.      ]
 [1.331997 0.536045 0.      0.      ]
 [3.970094 0.116322 0.      0.      ]]
​
# Option 2: Import using Pandas (preferred)
my_data_pandas = pd.read_csv(csv_filepath)
print(my_data_pandas[0:5])
​
         x         y z is it inside?
0 0.542188  0.452487 0             0
1 0.808325  0.634651 0             0
2 1.331997  0.536045 0             0
3 3.970094  0.116322 0             0
4 3.218009 -0.587934 0             0
​
# Pandas also has its own display, which appears when not using print()
my_data_pandas.head(5)
​
​
Selecting features and labels
​
# Here you can select your features from the pandas dataframe, and also select the labels for prediction
features = list(zip(my_data_pandas['x'], my_data_pandas['y']))
 
prediction_labels = list(my_data_pandas['is it inside?']) # convert to a simple list for sklearn
 
print(features[0:5])
print(prediction_labels[0:5])
​
[(0.542188, 0.452487), (0.808325, 0.634651), (1.331997, 0.536045), (3.970094, 0.116322), (3.218009, -0.587934)] [0, 0, 0, 0, 0]
​
from sklearn.model_selection import train_test_split
 
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(features, prediction_labels, test_size=0.3) # 70% training and 30% test
​
​
ML or data analysis
​
# ML input here...

 
# SVM example
 
from sklearn import svm

 
clf = svm.SVC(kernel='rbf', C=1000)  # note: try rbf and C = 1000
clf.fit(X_train, y_train)
 
y_pred = clf.predict(X_test)
 
from sklearn.metrics import accuracy_score
 
print('Accuracy is currently: ', accuracy_score(y_test, y_pred))
​
Accuracy is currently: 0.9946666666666667
​
my_data_pandas['new predicted values'] = clf.predict(features)
print(my_data_pandas['is it inside?'].value_counts())
print(my_data_pandas['new predicted values'].value_counts())
​
0 1987
1  513
Name: is it inside?, dtype: int64
0 1991
1 509
Name: new predicted values, dtype: int64
​
​
Export Results to CSV
​
# Results export
df_out = my_data_pandas # edit output pandas dataframe here
out_suffix = 'python output' # name to put after new file

 
output_file_name = csv_file_name[:-4] + '_' + out_suffix + '.csv' # This takes the original csv name and appends the suffix name. The [:-4] takes the whole name, except the last 4 characters (.csv)
 
print('Your new output file, is in the same folder with the name: ', output_file_name)

 
# The output file path
csv_out_filepath = os.path.join(current_path, output_file_name)
 
# creating csv at file path from the df_out specificed pandas dataframe
df_out.to_csv(csv_out_filepath, index=False)
​
Your new output file, is in the same folder with the name: test file_python output.csv
bottom of page