Skip to content

Instantly share code, notes, and snippets.

View gavi's full-sized avatar
🎯
Focusing

Gavi Narra gavi

🎯
Focusing
View GitHub Profile
@gavi
gavi / iris.py
Created July 17, 2017 20:06
Basic scikit learn with IRIS data
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
import coremltools
iris = datasets.load_iris()
clf = RandomForestClassifier()
clf.fit(iris.data, iris.target_names[iris.target])
#Test to see if it is working correctly
print(list(clf.predict(iris.data[:3])))
print(list(clf.predict([[ 5.1,3.5,1.4,0.2]])))
@gavi
gavi / CodingKeysExample.swift
Created July 15, 2017 16:57
CodingKeys Enum for renaming JSON keys
struct GeocodingResult:Codable{
struct Geometry:Codable{
struct Location:Codable{
let lat:Float
let lng:Float
}
let location:Location
}
let formattedAddress:String
let geometry:Geometry
@gavi
gavi / CodableNestedStructures.swift
Last active June 13, 2025 19:47
Parsing Nested JSON Structure in Swift
import Foundation
let geoResult="""
{
"results": [
{
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
@gavi
gavi / JSONDecode.swift
Created July 15, 2017 16:19
JSON Decoding in Swift Basic
let inputData = """
{"x":10,"y":10,"moment":"2017-06-08T21:36:37Z"}
""".data(using: .utf8)!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let obj=try? decoder.decode(Point.self, from: inputData)
if let point=obj{
print(point.x)
}
@gavi
gavi / JSONEncode.swift
Created July 15, 2017 16:16
JSON Encoding in Swift Basic Example
import Foundation
struct Point:Codable{
var x:Float
var y:Float
var moment:Date
}
let p=Point(x:10,y:10,moment:Date())
let x=JSONEncoder()
extension String{
static func randomEmoji()->String{
let range = [UInt32](0x1F601...0x1F64F)
let ascii = range[Int(drand48() * (Double(range.count)))]
let emoji = UnicodeScalar(ascii)?.description
return emoji!
}
}
@gavi
gavi / NLP.swift
Created June 8, 2017 13:09
NSLinguisticTagger updates in Foundation for Swift 4
import Foundation
var str = """
This is some text that needs to be processed. I do not know how fast this runs?
日本,
Лорем ипсум долор сит амет, перпетуа урбанитас ин про, проприае цонсететур ид сит
"""
let tagger=NSLinguisticTagger(tagSchemes: [.lemma, .language, .lexicalClass], options:0 )
tagger.string=str
//: Playground - noun: a place where people can play
import UIKit
import MapKit
import PlaygroundSupport
class Container:UIView{
let mapView:MKMapView
override init(frame: CGRect) {
@gavi
gavi / async.swift
Last active December 27, 2015 01:16
Running asynchronous code in Xcode Playground
import Foundation
import XCPlayground
// Updated for Swift 2.0
let url:NSURL!=NSURL(string: "http://objectgraph.com")
NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
print(error)
print(response)
}.resume()
print("This line is printed first as the network call is not complete")
@gavi
gavi / geocoder.swift
Created June 13, 2015 19:57
Semaphore based NSURLSession for Command Line GeoCoder
import Foundation
func BlockingGet(url:String)->String?{
let sema=dispatch_semaphore_create(0)
let url:NSURL!=NSURL(string:url)
var output:String?
NSURLSession.sharedSession().dataTaskWithURL(url) {
data, response, error in
output = NSString(data: data!, encoding: NSUTF8StringEncoding) as String?
dispatch_semaphore_signal(sema)